Skip to content

Instantly share code, notes, and snippets.

@sordina
Created May 24, 2011 17:38
Show Gist options
  • Save sordina/989220 to your computer and use it in GitHub Desktop.
Save sordina/989220 to your computer and use it in GitHub Desktop.
Example of closable channels using forkJoin and random sleeps.
import Closable
import ForkJoin
import Control.Concurrent
import System.Random
import Control.Monad
main = do
c <- newChan :: IO (Chan (Maybe String))
forkIO $ getChanWhileJust c >>= mapM_ print
forkJoin [ foo c "test_a.txt"
, foo c "test_b.txt"]
writeChan c Nothing
foo c f = do
text <- fmap lines (readFile f)
rands <- fmap (randomRs (0::Float, 0.5)) newStdGen
zipWithM_ (bar c) rands text
bar c x y = do
threadDelay . floor . (* 10^6) $ x
writeChan c $ Just y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment