Safe Haskell | None |
---|---|
Language | Haskell98 |
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 extraWorkerWhileBlocked
.
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.
Synopsis
- 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 ()
Executing actions
parallel_ :: [IO a] -> IO () Source #
Execute the given actions in parallel on the global thread pool.
Users of the global pool must call stopGlobalPool
from the main thread at the end of their program.
See also parallel_
.
parallelE_ :: [IO a] -> IO [Maybe SomeException] Source #
Execute the given actions in parallel on the global thread pool, reporting exceptions explicitly.
Users of the global pool must call stopGlobalPool
from the main thread at the end of their program.
See also parallelE_
.
parallel :: [IO a] -> IO [a] Source #
Execute the given actions in parallel on the global thread pool, returning the results in the same order as the corresponding actions.
Users of the global pool must call stopGlobalPool
from the main thread at the end of their program.
See also parallel
.
parallelE :: [IO a] -> IO [Either SomeException a] Source #
Execute the given actions in parallel on the global thread pool, returning the results in the same order as the corresponding actions and reporting exceptions explicitly.
Users of the global pool must call stopGlobalPool
from the main thread at the end of their program.
See also parallelE
.
parallelInterleaved :: [IO a] -> IO [a] Source #
Execute the given actions in parallel on the global thread pool, returning the results in the approximate order of completion.
Users of the global pool must call stopGlobalPool
from the main thread at the end of their program.
See also parallelInterleaved
.
parallelInterleavedE :: [IO a] -> IO [Either SomeException a] Source #
Execute the given actions in parallel on the global thread pool, returning the results in the approximate order of completion and reporting exceptions explicitly.
Users of the global pool must call stopGlobalPool
from the main thread at the end of their program.
See also parallelInterleavedE
.
parallelFirst :: [IO (Maybe a)] -> IO (Maybe a) Source #
Run the list of computations in parallel, returning the result of the first thread that completes with (Just x), if any.
Users of the global pool must call stopGlobalPool
from the main thread at the end of their program.
See also parallelFirst
.
parallelFirstE :: [IO (Maybe a)] -> IO (Maybe (Either SomeException a)) Source #
Run the list of computations in parallel, returning the result of the first thread that completes with (Just x), if any, and reporting any exception explicitly.
Users of the global pool must call stopGlobalPool
from the main thread at the end of their program.
See also parallelFirstE
.
Global pool management
globalPool :: Pool Source #
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.
stopGlobalPool :: IO () Source #
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 main
.
See also stopPool
.
extraWorkerWhileBlocked :: IO a -> IO a Source #
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.
See also extraWorkerWhileBlocked
.
Advanced global pool management
spawnPoolWorker :: IO () Source #
Internal method for adding extra unblocked threads to a pool if one of the current
worker threads is going to be temporarily blocked. Unrestricted use of this is unsafe,
so we reccomend that you use the extraWorkerWhileBlocked
function instead if possible.
See also spawnPoolWorkerFor
.
killPoolWorker :: IO () Source #
Internal method for removing threads from a pool after one of the threads on the pool
becomes newly unblocked. Unrestricted use of this is unsafe, so we reccomend that you use
the extraWorkerWhileBlocked
function instead if possible.
See also killPoolWorkerFor
.