[Haskell-cafe] simple stm question

Brandon Moore brandon_m_moore at yahoo.com
Tue Mar 15 01:03:47 CET 2011


>I recently upgraded to ghc 7.0.2 from 6.12.3, along with the Haskell platform, 
>and noticed that the following code no longer works as expected:
>
>waitFor tvar = atomically $ do
>    count <- readTVar tvar
>    check (count == 0)
>
>worker tchan tvar = loop
>   where loop = do
>           putStrLn "checking"
>           finished <- atomically $ isEmptyTChan tchan
>           threadDelay 50000
>           if finished
>              then atomically $ do val <- readTVar tvar
>                                   writeTVar tvar $ (subtract 1) val
>              else (atomically $ readTChan tchan) >> loop
>
>test = do
>  tchan <- newTChanIO
>  pure $ forM_ [1..5] $ writeTChan tchan -- THIS LINE 
>  tvar <- newTVarIO 1
>  forkIO $ worker tchan tvar
>  waitFor tvar
>  putStrLn "DONE"

You did not include your imports, but I assume that's Control.Applicative.pure.
If so, that line should never do anything. pure for IO is the same as return -
you are never running the STM action that line calculates. It's the
same as if you had written

>test = do
>  tchan <- newTChanIO
>  let neverGetsUsed = forM_ [1..5] $ writeTChan tchan -- THIS LINE 
>  tvar <- newTVarIO 1
>  forkIO $ worker tchan tvar
>  waitFor tvar
>  putStrLn "DONE"

Brandon



      



More information about the Haskell-Cafe mailing list