IO and STM
Lemmih
lemmih at gmail.com
Wed Apr 13 13:17:05 EDT 2005
On 4/13/05, Robert van Herk <rherk at cs.uu.nl> wrote:
> 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
More information about the Glasgow-haskell-users
mailing list