
2009/5/27 Bertram Felgenhauer
I wrote:
Krzysztof Skrzętnicki wrote:
The code for modifying the counter: (\ msg -> atomicModifyIORef ioref (\ cnt -> (cntMsg cnt msg,())))
atomicModifyIORef does not force the new value of the IORef. If the previous contents of the IORef is x, the new contents will be a thunk,
(\ cnt -> (cntMsg cnt msg,())) x
Sorry, it's slightly worse than that. The contents becomes
sel_0 (\ cnt -> (cntMsg cnt msg, ())) x
where sel_0 is basically an RTS internal version of fst.
Instead of reading the new value of the IORef, you could also force the old one:
atomicModifyIORef ioref (\ cnt -> (cntMsg cnt msg, msg)) >>= (return $!)
Thanks for the tip, although it seems tricky to get it right. I wonder why there is no strict version of atomicModifyIORef? Dually there might be a strict version of IORef datatype. Best regards Christopher Skrzętnicki