title: Haskell: How does 'atomicModifyIORef' work?
body: <p>Can someone explain how <a href="http://hackage.haskell.org/packages/archive/base/latest/doc/html/Data-IORef.html#v%3aatomicModifyIORef"><code>atomicModifyIORef</code></a> works? In particular:</p>
  
  <p>(1) Does it wait for a lock, or optimistically try and retry if there's contention (like <code>TVar</code>).<br>
  (2) Why is the signature of <a href="http://hackage.haskell.org/packages/archive/base/latest/doc/html/Data-IORef.html#v%3aatomicModifyIORef"><code>atomicModifyIORef</code></a> different to the signature of <a href="http://hackage.haskell.org/packages/archive/base/latest/doc/html/Data-IORef.html#v%3aatomicModifyIORef"><code>modifyIORef</code></a>? In particular, what is this extra variable <code>b</code>?</p>
  
  <p><b>Edit:</b> I think I've figured out the answer to (2), in that <code>b</code> is a value to be extracted (this can be empty if not needed). In a single threaded program, knowing the value is trivial, but in a multithreaded program, one may want to know what the previous value was at the time of the function being applied. I assume this is why <code>modifyIORef</code> doesn't have this extra return value (as such usages of <code>modifyIORef</code> with this return value probably should use <code>atomicModifyIORef</code> anyway. I'm still interested in the answer to (1) though.</p>