Created
August 25, 2012 19:32
-
-
Save DeTeam/3469872 to your computer and use it in GitHub Desktop.
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.Chan | |
import System.Random | |
delayedOutput :: String -> Chan String -> IO () | |
delayedOutput m c = do | |
gen <- newStdGen | |
let (t, _) = randomR (1, 10) gen | |
-- delay in microseconds | |
threadDelay (t * 1000000) | |
-- display message and leave then | |
putStrLn m | |
writeChan c "done" | |
return () | |
main :: IO () | |
main = do | |
-- channel for communication, waiting | |
c <- newChan | |
putStrLn "Fight!" | |
forkIO $ delayedOutput "Finish him!" c | |
-- here we're blocked for thread to finish | |
readChan c | |
return () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment