The Revenge of Finalizers

Simon Marlow simonmar at
Thu Oct 17 10:32:45 EDT 2002

> Simon Marlow wrote:
> [snip]
> > Don't you run into a problem even if the two threads use the same
> > ordering?  Suppose
> > 
> >   - thread 1 does the atomicModifyIORef, and gets preempted before
> >     doing the seq
> >   - thread 2 does its own atomicModifyIORef, and the seq.  Thread 2
> >     gets an inconsistent view of the IORefs.
> No.  At the time of the seq in thread 2, the thunk (call it Thunk 2)
> returned by the second atomicModifyIORef will refer to the 
> thunk (Thunk 1)
> put in there by the first atomicModifyIORef.  The seq will cause the
> unsafePerformIO from the second atomicModifyIORef to be 
> evaluated.  This
> will come to the "if" statement which will demand the value 
> of Thunk 1.
> This will in turn provoke the evaluation of Thunk1.  This will cause
> the action of thread 1's simpleToggle2 to be evaluated to completion.

Ok, so it relies crucially on the fact that the 'if' causes evaluation.
(you might have written it to be more lazy, so that the pair was
returned immediately without evaluating contents1 first, for example).

In that case, yes I agree it is horrible :)


More information about the FFI mailing list