threads-0.5.1.8: Fork threads and wait for their result

Copyright | (c) 2010-2012 Bas van Dijk & Roel van Dijk
License | BSD3 (see the file LICENSE)
Maintainer | Bas van Dijk <v.dijk.bas@gmail.com> , Roel van Dijk <vandijk.roel@gmail.com>
Safe Haskell | Trustworthy
Language | Haskell2010

Control.Concurrent.Thread.Group

Contents

- Forking threads

Description

This module extends `Control.Concurrent.Thread` with the ability to wait for a group of threads to terminate.

This module exports equivalently named functions from `Control.Concurrent`, (`GHC.Conc`), and `Control.Concurrent.Thread`. Avoid ambiguities by importing this module qualified. May we suggest:

    import Control.Concurrent.Thread.Group ( ThreadGroup )
    import qualified Control.Concurrent.Thread.Group as ThreadGroup ( ... )

Synopsis

- data ThreadGroup
- new :: IO ThreadGroup
- nrOfRunning :: ThreadGroup -> STM Int
- wait :: ThreadGroup -> IO ()
- waitN :: Int -> ThreadGroup -> IO ()
- forkIO :: ThreadGroup -> IO a -> IO (ThreadId, IO (Result a))
- forkOS :: ThreadGroup -> IO a -> IO (ThreadId, IO (Result a))
- forkOn :: Int -> ThreadGroup -> IO a -> IO (ThreadId, IO (Result a))
- forkIOWithUnmask :: ThreadGroup -> ((forall b. IO b -> IO b) -> IO a) -> IO (ThreadId, IO (Result a))
- forkOnWithUnmask :: Int -> ThreadGroup -> ((forall b. IO b -> IO b) -> IO a) -> IO (ThreadId, IO (Result a))

# Documentation

data ThreadGroup Source #

A `ThreadGroup` can be understood as a counter which counts the number of threads that were added to the group minus the ones that have terminated.

More formally a `ThreadGroup` has the following semantics:

- `new` initializes the counter to 0.
- Forking a thread increments the counter.
- When a forked thread terminates, whether normally or by raising an exception, the counter is decremented.
- `nrOfRunning` yields a transaction that returns the counter.
- `wait` blocks as long as the counter is greater than 0.
- `waitN` blocks as long as the counter is greater or equal to the specified number.

#### Instances

Instances details

Eq ThreadGroup Source # |
Instance details Defined in Control.Concurrent.Thread.Group Methods (==) :: ThreadGroup -> ThreadGroup -> Bool Source # (/=) :: ThreadGroup -> ThreadGroup -> Bool Source #

new :: IO ThreadGroup Source #

Create an empty group of threads.

nrOfRunning :: ThreadGroup -> STM Int Source #

Yield a transaction that returns the number of running threads in the group.

Note that because this function yields a `STM` computation, the returned number is guaranteed to be consistent inside the transaction.

wait :: ThreadGroup -> IO () Source #

Block until all threads in the group have terminated.

Note that: `wait = `waitN` 1`.

waitN :: Int -> ThreadGroup -> IO () Source #

Block until there are fewer than `N` running threads in the group.

# Forking threads

forkIO :: ThreadGroup -> IO a -> IO (ThreadId, IO (Result a)) Source #

Same as `Control.Concurrent.Thread.`forkIO`` but additionaly adds the thread to the group.

forkOS :: ThreadGroup -> IO a -> IO (ThreadId, IO (Result a)) Source #

Same as `Control.Concurrent.Thread.`forkOS`` but additionaly adds the thread to the group.

forkOn :: Int -> ThreadGroup -> IO a -> IO (ThreadId, IO (Result a)) Source #

Same as `Control.Concurrent.Thread.`forkOn`` but additionaly adds the thread to the group.

forkIOWithUnmask :: ThreadGroup -> ((forall b. IO b -> IO b) -> IO a) -> IO (ThreadId, IO (Result a)) Source #

Same as `Control.Concurrent.Thread.`forkIOWithUnmask`` but additionaly adds the thread to the group.

forkOnWithUnmask :: Int -> ThreadGroup -> ((forall b. IO b -> IO b) -> IO a) -> IO (ThreadId, IO (Result a)) Source #

Like `Control.Concurrent.Thread.`forkOnWithUnmask`` but additionaly adds the thread to the group.