Created
November 7, 2013 05:21
-
-
Save AlexanderAA/7349395 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
module Fork ( | |
main | |
) where | |
import Control.Concurrent (forkIO, forkOS, ThreadId, threadDelay) | |
import Control.Monad (mapM, void) | |
import System.Random (getStdRandom, randomR) | |
second :: Int | |
second = 1000000 --microseconds | |
action :: String -> IO () | |
action message = do | |
delay <- getStdRandom (randomR (0, 5*second)) | |
threadDelay delay | |
putStrLn $ (show delay) ++ " * " ++ message | |
main :: IO () | |
main = do | |
let messages = map (("M" ++) . show) [1..] | |
let actions = map (forkOS . action) messages | |
let workUnit = take 10 actions | |
result <- sequence workUnit | |
print result | |
--threadDelay (10*second) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment