STM and unsafePerformIO
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
> *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
to force evaluation of tSid before doing anything else.
More information about the Glasgow-haskell-users