
Hi all, I have a question about IO in a STM transaction. I want to make an atomic algorithm that does the following: 1. read an IORef 2. write some changes to the value you get from 1 3. save the IORef again Currently, I did this, roughly like this: doSomething :: IORef [s] -> s -> IO () doSomething ref s = atomically ( do { ss <- unsafeIOToSTM (readIORef s); do something with ss; unsafeIOToSTM (writeIORef s ss); return () } ) Since I read that STM uses a revertable log, I was wondering what happens with the IO actions, when a rerun on the atomic part is performed... I guess it will just redo them? Is this the unsafe-part of unsafeIOToSTM, or are there more caveats? Regards, Robert

On 4/13/05, Robert van Herk
Hi all,
I have a question about IO in a STM transaction.
I want to make an atomic algorithm that does the following:
1. read an IORef 2. write some changes to the value you get from 1 3. save the IORef again
Currently, I did this, roughly like this:
doSomething :: IORef [s] -> s -> IO () doSomething ref s = atomically ( do { ss <- unsafeIOToSTM (readIORef s); do something with ss; unsafeIOToSTM (writeIORef s ss); return () } )
Since I read that STM uses a revertable log, I was wondering what happens with the IO actions, when a rerun on the atomic part is performed... I guess it will just redo them? Is this the unsafe-part of unsafeIOToSTM, or are there more caveats?
You probably wanna use TVars instead of IORefs. I would suggest reading the STM paper at research.microsoft.com/~simonpj/papers/stm/stm.ps, if you haven't already. -- Friendly, Lemmih

You probably wanna use TVars instead of IORefs. I would suggest reading the STM paper at research.microsoft.com/~simonpj/papers/stm/stm.ps, if you haven't already.
ah, I read about them, and than forgot all about them again :-). Guess will have to read about 'em again. Thanks! Robert
participants (2)
-
Lemmih
-
Robert van Herk