I find TVar's quite easy to work with even though MVar's appear a little simpler, while TVar's a little more featureful.
So my question is pretty simple, what condition do I want to go to MVar rather than TVar? I suppose anytime I don't need transactional update I can use an MVar, but in what way does that benefit me?
MVar
TVar in some casesTVar
IORef
IO writes/updatesTVar
There is not really a hard and fast rule for when to use MVar or TVar.
If the resource I'm guarding will ever be "missing" (as opposed to empty, consider Nothing vs Just mempty), then MVar often makes the most sense. If I will need to perform atomic blocks of modifications to the resource, then TVar is most suitable.
TVars are safer but slower.
MVars can deadlock, but are much, much more efficient.
More efficient still is IORef and atomicModifyIORef (CAS), but that's highly restricted in what you can do with it.
It's really a safety over performance trade off. TVars are fully general, very safe. Everything else is less so, on a decreasing scale.
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