18 Dec 2012 19:03
Race Condition in threads
mukesh tiwari <mukeshtiwari.iiitm <at> gmail.com>
2012-12-18 18:03:04 GMT
2012-12-18 18:03:04 GMT
Hello All
I have two questions.
1. I wrote this code to create 10 simultaneous threads. Could some one please tell me if this is correct or not ? I have two questions.
incr_count :: MVar () -> MVar Int -> IO ()
incr_count m n = ( forM_ [ 1..10000 ] $ \_ -> modifyMVar_ n ( return . ( + 10 ) ) ) >> putMVar m ()
main :: IO()
main = do
count <- newMVar 0
list <- forM [1..10] $ \_ -> newEmptyMVar
forM_ list $ \var -> forkIO . incr_count var $ count
forM_ list $ \var -> takeMVar var
val <- takeMVar count
print val
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe <at> haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
RSS Feed