The Revenge of Finalizers

Simon Marlow simonmar at
Thu Oct 17 09:58:33 EDT 2002

> > I don't know how to achieve the same goal with
> > atomicModifyIORef.
> I do.  To modify ioRef1 and ioRef2 "simultaneously", write
> atomicModifyIORef ioRef1 (\ contents1 -> unsafePerformIO 
> ioRef2 (\ contents2 -> blah blah))
> The actual modification will take place when the result or 
> contents of ioRef1 or ioRef2 get evaluated. 

I don't understand how this works.  Unless I'm missing something, you'll
see the contents of ioRef1 at the point at which the first
atomicModifyIORef takes place, but the contents of ioRef2 from the time
at which the unsafePerformIO is evaluated, which is some unspecified
time later.  

Also another modification might come along before the first
unsafePerformIO is evaluated, and it might see the "new" value of ioRef1
but the old value of ioRef2, if it doesn't evaluate the value of ioRef1
early enough.

Nevertheless, I believe that the addition of atomicModifyIORef will let
us write System.Random and Data.Dynamic in a safe way - both libraries
have only a single global IORef to worry about.


More information about the FFI mailing list