Skip to content

Instantly share code, notes, and snippets.

@eborden
Created October 13, 2017 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eborden/3eeb31f263fa28219c993792fae92f43 to your computer and use it in GitHub Desktop.
Save eborden/3eeb31f263fa28219c993792fae92f43 to your computer and use it in GitHub Desktop.
Immortal chans
#!/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