Search code examples
multithreadinghaskellconcurrencystm

Haskell: TMVar vs MVar


I want a small operation where one thread adds to a shared state some value, while another thread takes the value out and prints it. Here are two versions using TMVar and MVar respectively. The TMVar version is not working somehow, it keeps printing out the first value. What is the problem in the STM first version? How to fix the first TMVar version to make it work?

import Control.Concurrent (forkIO, takeMVar,newEmptyMVar,putMVar)
import Control.Monad (forM_, replicateM_)
import Control.Concurrent.STM (atomically, readTMVar, putTMVar, newEmptyTMVarIO)

n=10

main = do

    mvar<- newEmptyTMVarIO

    forkIO $ do
        forM_ [1..n] $ \x-> atomically $ do
            putTMVar mvar $! x

    replicateM_  n $ do
        a<- atomically $ readTMVar mvar
        print $ show a


main2 = do

    mvar<- newEmptyMVar

    forkIO $ do
        mapM_ (\x-> putMVar mvar x) [1..n]

    replicateM_  n $ do
        a<- takeMVar mvar
        print $ show a

Solution

  • You're using readTMVar, which just looks at what's in the TMVar. I imagine you mean to use takeTMVar to give the other thread a chance to put something new in it.