STM and unsafePerformIO
rturk at science.uva.nl
Wed Aug 3 07:05:25 EDT 2005
On Wed, Aug 03, 2005 at 12:50:54PM +0200, Robert van Herk wrote:
> Hello All,
> 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?
It probably dies not because of unsafePerformIO per se, but
because STM doesn't understand nested transactions, and
unsafePerformIO here results in a nested transaction. Using the
following main works for me, as it forces both "atomically"'s to
be evaluated sequentially:
main = tSid `seq` atomically tickSessionID
P.S. Could you find out (and fix) what inserts those spurious *'s in your code?
Nobody can be exactly like me. Even I have trouble doing it.
More information about the Glasgow-haskell-users