Last active
October 7, 2017 11:30
-
-
Save kazu-yamamoto/cde22cf569e7e78a179d30a9982b1433 to your computer and use it in GitHub Desktop.
Concurrent hello world in Haskell
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
module Main (main) where | |
import Control.Concurrent | |
main :: IO () | |
main = do | |
codechan <- newEmptyMVar | |
wait <- newEmptyMVar | |
myid <- myThreadId | |
putStrLn $ "I'm " ++ show myid | |
cid <- forkIO $ child codechan wait | |
let code = putStrLn ("Hello, " ++ show cid ++ "!") | |
putMVar codechan code | |
takeMVar wait | |
child :: MVar (IO ()) -> MVar () -> IO () | |
child codechan wait = do | |
myid <- myThreadId | |
putStrLn $ "I'm " ++ show myid | |
code <- takeMVar codechan | |
code | |
putMVar wait () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment