Created
October 13, 2017 01:19
-
-
Save eborden/3eeb31f263fa28219c993792fae92f43 to your computer and use it in GitHub Desktop.
Immortal chans
This file contains hidden or 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
#!/usr/bin/env stack | |
-- stack --resolver lts-9.8 script | |
module Main where | |
import Data.Functor (void) | |
import Data.Foldable (traverse_, for_) | |
import Data.Traversable (for) | |
import Control.Concurrent (forkIO, readChan, writeChan, newChan, getNumCapabilities) | |
import Control.Immortal (create, wait, Thread) | |
main :: IO () | |
main = do | |
numThreads <- getNumCapabilities | |
chan <- newChan | |
void . forkIO . for_ [1..120000 :: Int] $ writeChan chan . show | |
waitImmortalFor_ ['a'..toEnum $ 96 + numThreads] $ \tid _ -> do | |
val <- readChan chan | |
putStrLn $ tid:val | |
waitImmortalFor_ :: Traversable t => t a -> (a -> Thread -> IO ()) -> IO () | |
waitImmortalFor_ xs action = do | |
threads <- for xs $ \x -> create $ action x | |
traverse_ wait threads |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment