STM and unsafePerformIO
Simon Marlow
simonmar at microsoft.com
Wed Aug 3 07:02:11 EDT 2005
On 03 August 2005 11:51, Robert van Herk wrote:
> I think I've read somewhere that STM doesn't like unsafePerformIO.
> However, I would like to use a global STM variable. Something like
> this:
>
> *module* Main *where*
> import GHC.Conc
> import System.IO.Unsafe
>
> tSid = unsafePerformIO (atomically (newTVar 0))
>
> tickSessionID :: STM Int
> tickSessionID =
> *do* sid <- readTVar tSid
> writeTVar tSid (sid + 1)
> return sid
>
> main = atomically tickSessionID
>
> But, when I try this, the evaluation of main causes a segmentation
> fault. Is there a workaround for this bug?
You're ok as long as the global variable doesn't get evaluated inside
another atomically. The problem is that nested atomically isn't
supported; the RTS gets confused.
For example, you could say
main = do
evaluate tSid
...
to force evaluation of tSid before doing anything else.
Cheers,
Simon
More information about the Glasgow-haskell-users
mailing list