[Haskell-cafe] STM semantics

Paolino paolo.veronelli at gmail.com
Sat Oct 24 06:11:58 EDT 2009


I have a doubt that this code works like I want because actual STM
implementation exposes its effects

import Control.Concurrent
import Control.Concurrent.STM

main = do
    c <- atomically $ newTChan :: IO (TChan ())
    r <- atomically $ newTVar False
    forkIO $ do
        atomically $ do
            r0 <- readTVar r
            case r0 of
                True -> return ()
                False -> readTChan c
        myThreadId >>= killThread
    threadDelay 1000000
    atomically (writeTVar r True)

The thread stops on readTChan, but exits when I change the TVar, which
happens before the readTChan.

Should I trust this is the correct STM behaviour , and will not change
in different implementations ?

thanks

paolino


More information about the Haskell-Cafe mailing list