Parallelism combinators with an implicit global thread-pool.
The most basic example of usage is:
main = parallel_ [putStrLn "Echo", putStrLn " in parallel"] >> stopGlobalPool
Make sure that you compile with
-threaded and supply
+RTS -N2 -RTS
to the generated Haskell executable, or you won't get any parallelism.
If you plan to allow your worker items to block, then you should read the documentation for
The Control.Concurrent.ParallelIO.Local module provides a more general interface which allows explicit passing of pools and control of their size. This module is implemented on top of that one by maintaining a shared global thread pool with one thread per capability.
- parallel_ :: [IO a] -> IO ()
- parallelE_ :: [IO a] -> IO [Maybe SomeException]
- parallel :: [IO a] -> IO [a]
- parallelE :: [IO a] -> IO [Either SomeException a]
- parallelInterleaved :: [IO a] -> IO [a]
- parallelInterleavedE :: [IO a] -> IO [Either SomeException a]
- parallelFirst :: [IO (Maybe a)] -> IO (Maybe a)
- parallelFirstE :: [IO (Maybe a)] -> IO (Maybe (Either SomeException a))
- globalPool :: Pool
- stopGlobalPool :: IO ()
- extraWorkerWhileBlocked :: IO a -> IO a
- spawnPoolWorker :: IO ()
- killPoolWorker :: IO ()
Global pool management
The global thread pool. Contains as many threads as there are capabilities.
Users of the global pool must call
stopGlobalPool from the main thread at the end of their program.
In order to reliably make use of the global parallelism combinators,
you must invoke this function after all calls to those combinators have
finished. A good choice might be at the end of
Wrap any IO action used from your worker threads that may block with this method: it temporarily spawns another worker thread to make up for the loss of the old blocked worker.