If I share an IORef
among multiple threads, and use atomicModifyIORef
to write to it:
atomicModifyIORef ref (\_ -> (new, ()))
Is it safe to read the value with plain old readIORef
? Or is there a chance readIORef
will return the old value in another thread after atomicModifyIORef
has modified it?
I think that's what the documentation implies:
atomicModifyIORef acts as a barrier to reordering. Multiple atomicModifyIORef operations occur in strict program order. An atomicModifyIORef is never observed to take place ahead of any earlier (in program order) IORef operations, or after any later IORef operations.
I just want to be sure.
you really should use TVar instead IORef
-- why?That's the whole motivation for TVars after all
- the motivation for IORef is the same – Ziegfeld