Created
July 10, 2012 04:35
-
-
Save aristotle9/3081035 to your computer and use it in GitHub Desktop.
MVar in Haskell Concurrent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Control.Concurrent | |
import Control.Concurrent.MVar | |
import Control.Monad.Fix (fix) | |
--seg:: MVar Int -> MVar Int -> Int -> IO () | |
seg mself mnext n loop = do | |
mn <- takeMVar mself | |
putStrLn $ show mn | |
putMVar mnext n | |
loop | |
main = do | |
let ls = [0..9] | |
mselfs@(m0:mx) <- sequence [newEmptyMVar| _ <- ls] | |
let mnexts = mx ++ [m0] | |
let fs = [forkIO $ fix $ seg mself mnext $ (n + 1) `mod` 10| (mself, mnext, n) <- zip3 mselfs mnexts ls] | |
sequence_ fs | |
putStrLn "Main Thread." | |
let print029 = do | |
putMVar m0 0 | |
takeMVar m0 | |
print029 | |
return () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment