[Haskell-cafe] Race Condition in threads
mukesh tiwari
mukeshtiwari.iiitm at gmail.com
Tue Dec 18 19:03:04 CET 2012
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 ?
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
2. I am trying to create race condition which makes the variable in
inconsistent state. Could some one please tell me how to achieve this ? I
look at IORef but it does not have function like modifyMVar_.
Mukesh Tiwari
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20121218/86e102dd/attachment.htm>
More information about the Haskell-Cafe
mailing list