|Maintainer||Bas van Dijk <firstname.lastname@example.org> , Roel van Dijk <email@example.com>|
Standard threads extended with the ability to wait for their termination.
This module exports equivalently named functions from
GHC.Conc). Avoid ambiguities by importing this module qualified. May
import qualified Control.Concurrent.Thread as Thread ( ... )
- data Result α
- forkIO :: IO α -> IO (ThreadId, Result α)
- forkOS :: IO α -> IO (ThreadId, Result α)
- forkOnIO :: Int -> IO α -> IO (ThreadId, Result α)
- wait :: Result α -> IO (Either SomeException α)
- wait_ :: Result α -> IO ()
- unsafeWait :: Result α -> IO α
- unsafeWait_ :: Result α -> IO ()
- status :: Result α -> IO (Maybe (Either SomeException α))
- isRunning :: Result α -> IO Bool
The result of a thread
The new thread will be a lightweight thread; if you want to use a foreign
library that uses thread-local storage, use
GHC note: the new thread inherits the blocked state of the parent (see
forkOS instead of
forkIO makes no difference at all to the scheduling
behaviour of the Haskell runtime system. It is a common misconception that you
need to use
forkOS instead of
forkIO to avoid blocking all the Haskell
threads when making a foreign call; this isn't the case. To allow foreign calls
to be made without blocking all the Haskell threads (with GHC), it is only
necessary to use the
-threaded option when linking your program, and to make
sure the foreign import is not marked
forkIO, but lets you specify on which CPU the thread is
created. Unlike a
forkIO thread, a thread created by
will stay on the same CPU for its entire lifetime (
can migrate between CPUs according to the scheduling policy).
forkOnIO is useful for overriding the scheduling policy when you
know in advance how best to distribute the threads.
Int argument specifies the CPU number; it is interpreted modulo
numCapabilities (note that it actually specifies a capability number
rather than a CPU number, but to a first approximation the two are
Waiting for results
wait but will either rethrow the exception that was thrown in the
thread or return the value that was returned by the thread.
unsafeWait in that it will rethrow the exception that was thrown in
the thread but it will ignore the value returned by the thread.
Nothingif the thread is still running.
if the thread terminated normally and returned
if some exception
ewas thrown in the thread and wasn't caught.
Notice that this observation is only a snapshot of a thread's state. By the time a program reacts on its result it may already be out of date.