module Control.ThreadPool
( threadPool
, threadPoolIO
) where
import Control.Monad (forever, forM_)
import Control.Concurrent (forkIO)
import Control.Concurrent.Chan
threadPool :: Int -> (a -> b) -> IO (Chan a, Chan b)
threadPool nr mutator = do
input <- newChan
output <- newChan
forM_ [1..nr] $
\_ -> forkIO (forever $ do
i <- readChan input
o <- return $! mutator i
writeChan output o)
return (input, output)
threadPoolIO :: Int -> (a -> IO b) -> IO (Chan a, Chan b)
threadPoolIO nr mutator = do
input <- newChan
output <- newChan
forM_ [1..nr] $
\_ -> forkIO (forever $ do
i <- readChan input
o <- mutator i
writeChan output o)
return (input, output)