a9gz-q
Last Updated: February 25, 2016
·
1.243K
· deteam
Db600aa753575ecc15d0a857e7e3a121

Haskell Async lib

I was trying to create a thread and wait in main thread, but it was a little bit complex ( https://gist.github.com/3469872 ): had to create an MVar or Chan to know when thread is done.

Here's better way using Control.Concurrent.Async

import Control.Concurrent
import Control.Concurrent.Async
import System.Random

delayedOutput :: String -> IO ()
delayedOutput m = do
  gen <- newStdGen
  let (t, _) = randomR (1, 10) ge
  -- delay in microseconds
  threadDelay (t * 1000000)
  -- display message
  putStrLn m
  return ()

main :: IO ()
main = do
  putStrLn "Fight!"
  a <- async (delayedOutput "Finish him!")
  wait a
  return ()
Say Thanks
Respond

2 Responses
Add your response

2782
35bd4c153de79ba272de5624ebc6cd1b

https://gist.github.com/3469872 this link not avaliable at the moment

over 1 year ago ·
2786
Db600aa753575ecc15d0a857e7e3a121

Parser's greedy :) https://gist.github.com/3469872 is opening fine

over 1 year ago ·