Skip to content

Instantly share code, notes, and snippets.

@aristotle9
Created July 10, 2012 04:35
Show Gist options
  • Save aristotle9/3081035 to your computer and use it in GitHub Desktop.
Save aristotle9/3081035 to your computer and use it in GitHub Desktop.
MVar in Haskell Concurrent
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