I'm having a little trouble understanding the basic difference between the IORef type and the MVar type in Haskell. Can someone help me out with this? They appear to solve the same problem. MVar seems to be targeted at multithreading, but IORef has the atomicModifyIORef function.
Thanks!
MVar
is, like you said, targeted at multithreading while IORef
can be used both as mutable variable in a single threaded program or as a synchronization construct in a multithreaded program.
IORef
can be used together with atomicModifyIORef
to get compare-and-swap (CAS) behavior: writers and readers can synchronize on a single pure value, stored in the IORef
. Readers use readIORef
to read a value and writers use atomicModifyIORef
to write a value. Note that atomicModifyIORef
doesn't let writers perform any side effects inside the critical section (i.e. they can only use a pure function when atomically changing the value).
MVar
allows you to implement arbitrary critical sections (using withMVar
), that may contain side effects. They can also be used just like an IORef
(as described in the previous paragraph), but at a higher cost.
If you want an intuition for what kind of semantic IORef
implements its the same as the CAS semantics Rich Hickey describes in a talk on Clojure's concurrency model: http://www.infoq.com/presentations/Are-We-There-Yet-Rich-Hickey
Edit: In addition, you cannot run into deadlocks using IORef
(but there still might be contention, causing retries).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With