RE: The Revenge of Finalizers
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. Cheers, Simon
Simon Marlow wrote:
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.
Perhaps we're talking about different sorts of atomicModifyIORef? I am now thinking of atomicModifyIORef as having the type IORef a -> (a -> (a,b)) -> IO b Thus when it is called the effect is to construct (1) a thunk of type (a,b) containing the application of the function to the thunk previously in the IORef; (2) a thunk containing fst $ the thunk from (1) of type (a,b), which is put in the IORef; (3) a thunk containing snd $ the thunk from (1), to be returned. Subsequent evaluation of the result of atomicModifyIORef will force the thunks in (1) and (3) to be evaluated, meaning the unsafePerformIO gets evaluated.
participants (2)
-
George Russell -
Simon Marlow