threads-0.5: Fork threads and wait for their result

MaintainerBas van Dijk <v.dijk.bas@gmail.com> , Roel van Dijk <vandijk.roel@gmail.com>
Safe HaskellSafe-Infered

Control.Concurrent.Thread

Contents

Description

Standard threads extended with the ability to wait for their return value.

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

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

The following is an example how to use this module:


import qualified Control.Concurrent.Thread as Thread ( forkIO, result )

main = do (tid, wait) <- Thread.forkIO $ do x <- someExpensiveComputation
                                            return x
          doSomethingElse
          x <- Thread.result =<< wait
          doSomethingWithResult x

Synopsis

Forking threads

forkIO :: IO α -> IO (ThreadId, IO (Result α))Source

Like Control.Concurrent.forkIO but returns a computation that when executed blocks until the thread terminates then returns the final value of the thread.

forkOS :: IO α -> IO (ThreadId, IO (Result α))Source

Like Control.Concurrent.forkOS but returns a computation that when executed blocks until the thread terminates then returns the final value of the thread.

forkOn :: Int -> IO α -> IO (ThreadId, IO (Result α))Source

Like Control.Concurrent.forkOn but returns a computation that when executed blocks until the thread terminates then returns the final value of the thread.

forkIOWithUnmask :: ((forall β. IO β -> IO β) -> IO α) -> IO (ThreadId, IO (Result α))Source

Like Control.Concurrent.forkIOWithUnmask but returns a computation that when executed blocks until the thread terminates then returns the final value of the thread.

forkOnWithUnmask :: Int -> ((forall β. IO β -> IO β) -> IO α) -> IO (ThreadId, IO (Result α))Source

Like Control.Concurrent.forkOnWithUnmask but returns a computation that when executed blocks until the thread terminates then returns the final value of the thread.

Results

type Result α = Either SomeException αSource

A result of a thread is either some exception that was thrown in the thread and wasn't catched or the actual value that was returned by the thread.

result :: Result α -> IO αSource

Retrieve the actual value from the result.

When the result is SomeException the exception is thrown.