Safe Haskell | None |
---|---|
Language | Haskell2010 |
Synopsis
- modifyVar' :: Var a -> (a -> a) -> IO a
- modifyVarIO' :: Var a -> (a -> IO a) -> IO a
- modifyVar :: Var a -> (a -> IO (a, b)) -> IO b
- modifyVar_ :: Var a -> (a -> IO a) -> IO ()
- forkOnWithUnmask :: Int -> ((forall a. IO a -> IO a) -> IO ()) -> IO ThreadId
- forkIOWithUnmask :: ((forall a. IO a -> IO a) -> IO ()) -> IO ThreadId
- forkOn :: Int -> IO () -> IO ThreadId
- forkOS :: IO () -> IO ThreadId
- forkIO :: IO () -> IO ThreadId
- data ThreadId
- threadWaitWriteSTM :: Fd -> IO (STM (), IO ())
- threadWaitReadSTM :: Fd -> IO (STM (), IO ())
- threadWaitWrite :: Fd -> IO ()
- threadWaitRead :: Fd -> IO ()
- runInUnboundThread :: IO a -> IO a
- runInBoundThread :: IO a -> IO a
- isCurrentThreadBound :: IO Bool
- forkOSWithUnmask :: ((forall a. IO a -> IO a) -> IO ()) -> IO ThreadId
- forkFinally :: IO a -> (Either SomeException a -> IO ()) -> IO ThreadId
- rtsSupportsBoundThreads :: Bool
- writeList2Chan :: Chan a -> [a] -> IO ()
- getChanContents :: Chan a -> IO [a]
- dupChan :: Chan a -> IO (Chan a)
- readChan :: Chan a -> IO a
- writeChan :: Chan a -> a -> IO ()
- newChan :: IO (Chan a)
- data Chan a
- signalQSem :: QSem -> IO ()
- waitQSem :: QSem -> IO ()
- newQSem :: Int -> IO QSem
- data QSem
- signalQSemN :: QSemN -> Int -> IO ()
- waitQSemN :: QSemN -> Int -> IO ()
- newQSemN :: Int -> IO QSemN
- data QSemN
- threadDelay :: Int -> IO ()
- mkWeakMVar :: MVar a -> IO () -> IO (Weak (MVar a))
- addMVarFinalizer :: MVar a -> IO () -> IO ()
- modifyMVarMasked :: MVar a -> (a -> IO (a, b)) -> IO b
- modifyMVarMasked_ :: MVar a -> (a -> IO a) -> IO ()
- modifyMVar :: MVar a -> (a -> IO (a, b)) -> IO b
- modifyMVar_ :: MVar a -> (a -> IO a) -> IO ()
- withMVarMasked :: MVar a -> (a -> IO b) -> IO b
- withMVar :: MVar a -> (a -> IO b) -> IO b
- swapMVar :: MVar a -> a -> IO a
- mkWeakThreadId :: ThreadId -> IO (Weak ThreadId)
- threadCapability :: ThreadId -> IO (Int, Bool)
- yield :: IO ()
- myThreadId :: IO ThreadId
- throwTo :: Exception e => ThreadId -> e -> IO ()
- killThread :: ThreadId -> IO ()
- setNumCapabilities :: Int -> IO ()
- getNumCapabilities :: IO Int
- isEmptyMVar :: MVar a -> IO Bool
- tryReadMVar :: MVar a -> IO (Maybe a)
- tryPutMVar :: MVar a -> a -> IO Bool
- tryTakeMVar :: MVar a -> IO (Maybe a)
- putMVar :: MVar a -> a -> IO ()
- readMVar :: MVar a -> IO a
- takeMVar :: MVar a -> IO a
- newMVar :: a -> IO (MVar a)
- newEmptyMVar :: IO (MVar a)
- data MVar a
- waitBarrierMaybe :: Barrier a -> IO (Maybe a)
- waitBarrier :: Barrier a -> IO a
- signalBarrier :: Partial => Barrier a -> a -> IO ()
- newBarrier :: IO (Barrier a)
- withVar :: Var a -> (a -> IO b) -> IO b
- modifyVar_' :: Var a -> (a -> IO a) -> IO ()
- writeVar' :: Var a -> a -> IO ()
- writeVar :: Var a -> a -> IO ()
- readVar :: Var a -> IO a
- newVar :: a -> IO (Var a)
- withLockTry :: Lock -> IO a -> IO (Maybe a)
- withLock :: Lock -> IO a -> IO a
- newLock :: IO Lock
- onceFork :: IO a -> IO (IO a)
- once :: IO a -> IO (IO a)
- withNumCapabilities :: Int -> IO a -> IO a
- data Lock
- data Var a
- data Barrier a
Documentation
modifyVar' :: Var a -> (a -> a) -> IO a Source #
Strict modification that returns the new value
forkOnWithUnmask :: Int -> ((forall a. IO a -> IO a) -> IO ()) -> IO ThreadId #
Like forkIOWithUnmask
, but the child thread is pinned to the
given CPU, as with forkOn
.
Since: base-4.4.0.0
forkIOWithUnmask :: ((forall a. IO a -> IO a) -> IO ()) -> IO ThreadId #
Like forkIO
, but the child thread is passed a function that can
be used to unmask asynchronous exceptions. This function is
typically used in the following way
... mask_ $ forkIOWithUnmask $ \unmask -> catch (unmask ...) handler
so that the exception handler in the child thread is established with asynchronous exceptions masked, meanwhile the main body of the child thread is executed in the unmasked state.
Note that the unmask function passed to the child thread should only be used in that thread; the behaviour is undefined if it is invoked in a different thread.
Since: base-4.4.0.0
forkOn :: Int -> IO () -> IO ThreadId #
Like forkIO
, but lets you specify on which capability the thread
should run. Unlike a forkIO
thread, a thread created by forkOn
will stay on the same capability for its entire lifetime (forkIO
threads can migrate between capabilities according to the scheduling
policy). forkOn
is useful for overriding the scheduling policy when
you know in advance how best to distribute the threads.
The Int
argument specifies a capability number (see
getNumCapabilities
). Typically capabilities correspond to physical
processors, but the exact behaviour is implementation-dependent. The
value passed to forkOn
is interpreted modulo the total number of
capabilities as returned by getNumCapabilities
.
GHC note: the number of capabilities is specified by the +RTS -N
option when the program is started. Capabilities can be fixed to
actual processor cores with +RTS -qa
if the underlying operating
system supports that, although in practice this is usually unnecessary
(and may actually degrade performance in some cases - experimentation
is recommended).
Since: base-4.4.0.0
forkOS :: IO () -> IO ThreadId #
Like forkIO
, this sparks off a new thread to run the IO
computation passed as the first argument, and returns the ThreadId
of the newly created thread.
However, forkOS
creates a bound thread, which is necessary if you
need to call foreign (non-Haskell) libraries that make use of
thread-local state, such as OpenGL (see Control.Concurrent).
Using 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 unsafe
.
forkIO :: IO () -> IO ThreadId #
Creates a new thread to run the IO
computation passed as the
first argument, and returns the ThreadId
of the newly created
thread.
The new thread will be a lightweight, unbound thread. Foreign calls
made by this thread are not guaranteed to be made by any particular OS
thread; if you need foreign calls to be made by a particular OS
thread, then use forkOS
instead.
The new thread inherits the masked state of the parent (see
mask
).
The newly created thread has an exception handler that discards the
exceptions BlockedIndefinitelyOnMVar
, BlockedIndefinitelyOnSTM
, and
ThreadKilled
, and passes all other exceptions to the uncaught
exception handler.
A ThreadId
is an abstract type representing a handle to a thread.
ThreadId
is an instance of Eq
, Ord
and Show
, where
the Ord
instance implements an arbitrary total ordering over
ThreadId
s. The Show
instance lets you convert an arbitrary-valued
ThreadId
to string form; showing a ThreadId
value is occasionally
useful when debugging or diagnosing the behaviour of a concurrent
program.
Note: in GHC, if you have a ThreadId
, you essentially have
a pointer to the thread itself. This means the thread itself can't be
garbage collected until you drop the ThreadId
.
This misfeature will hopefully be corrected at a later date.
Instances
Eq ThreadId | Since: base-4.2.0.0 |
Ord ThreadId | Since: base-4.2.0.0 |
Defined in GHC.Conc.Sync | |
Show ThreadId | Since: base-4.2.0.0 |
Hashable ThreadId | |
Defined in Data.Hashable.Class | |
NFData ThreadId | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq |
threadWaitWriteSTM :: Fd -> IO (STM (), IO ()) #
Returns an STM action that can be used to wait until data can be written to a file descriptor. The second returned value is an IO action that can be used to deregister interest in the file descriptor.
Since: base-4.7.0.0
threadWaitReadSTM :: Fd -> IO (STM (), IO ()) #
Returns an STM action that can be used to wait for data to read from a file descriptor. The second returned value is an IO action that can be used to deregister interest in the file descriptor.
Since: base-4.7.0.0
threadWaitWrite :: Fd -> IO () #
Block the current thread until data can be written to the given file descriptor (GHC only).
This will throw an IOError
if the file descriptor was closed
while this thread was blocked. To safely close a file descriptor
that has been used with threadWaitWrite
, use
closeFdWith
.
threadWaitRead :: Fd -> IO () #
Block the current thread until data is available to read on the given file descriptor (GHC only).
This will throw an IOError
if the file descriptor was closed
while this thread was blocked. To safely close a file descriptor
that has been used with threadWaitRead
, use
closeFdWith
.
runInUnboundThread :: IO a -> IO a #
Run the IO
computation passed as the first argument. If the calling thread
is bound, an unbound thread is created temporarily using forkIO
.
runInBoundThread
doesn't finish until the IO
computation finishes.
Use this function only in the rare case that you have actually observed a
performance loss due to the use of bound threads. A program that
doesn't need its main thread to be bound and makes heavy use of concurrency
(e.g. a web server), might want to wrap its main
action in
runInUnboundThread
.
Note that exceptions which are thrown to the current thread are thrown in turn to the thread that is executing the given computation. This ensures there's always a way of killing the forked thread.
runInBoundThread :: IO a -> IO a #
Run the IO
computation passed as the first argument. If the calling thread
is not bound, a bound thread is created temporarily. runInBoundThread
doesn't finish until the IO
computation finishes.
You can wrap a series of foreign function calls that rely on thread-local state
with runInBoundThread
so that you can use them without knowing whether the
current thread is bound.
isCurrentThreadBound :: IO Bool #
Returns True
if the calling thread is bound, that is, if it is
safe to use foreign libraries that rely on thread-local state from the
calling thread.
forkOSWithUnmask :: ((forall a. IO a -> IO a) -> IO ()) -> IO ThreadId #
Like forkIOWithUnmask
, but the child thread is a bound thread,
as with forkOS
.
forkFinally :: IO a -> (Either SomeException a -> IO ()) -> IO ThreadId #
Fork a thread and call the supplied function when the thread is about to terminate, with an exception or a returned value. The function is called with asynchronous exceptions masked.
forkFinally action and_then = mask $ \restore -> forkIO $ try (restore action) >>= and_then
This function is useful for informing the parent when a child terminates, for example.
Since: base-4.6.0.0
rtsSupportsBoundThreads :: Bool #
True
if bound threads are supported.
If rtsSupportsBoundThreads
is False
, isCurrentThreadBound
will always return False
and both forkOS
and runInBoundThread
will
fail.
writeList2Chan :: Chan a -> [a] -> IO () #
Write an entire list of items to a Chan
.
getChanContents :: Chan a -> IO [a] #
Return a lazy list representing the contents of the supplied
Chan
, much like hGetContents
.
dupChan :: Chan a -> IO (Chan a) #
Duplicate a Chan
: the duplicate channel begins empty, but data written to
either channel from then on will be available from both. Hence this creates
a kind of broadcast channel, where data written by anyone is seen by
everyone else.
(Note that a duplicated channel is not equal to its original.
So: fmap (c /=) $ dupChan c
returns True
for all c
.)
Read the next value from the Chan
. Blocks when the channel is empty. Since
the read end of a channel is an MVar
, this operation inherits fairness
guarantees of MVar
s (e.g. threads blocked in this operation are woken up in
FIFO order).
Throws BlockedIndefinitelyOnMVar
when the channel is
empty and no other thread holds a reference to the channel.
Chan
is an abstract type representing an unbounded FIFO channel.
signalQSem :: QSem -> IO () #
Signal that a unit of the QSem
is available
Build a new QSem
with a supplied initial quantity.
The initial quantity must be at least 0.
Build a new QSemN
with a supplied initial quantity.
The initial quantity must be at least 0.
threadDelay :: Int -> IO () #
Suspends the current thread for a given number of microseconds (GHC only).
There is no guarantee that the thread will be rescheduled promptly when the delay has expired, but the thread will never continue to run earlier than specified.
addMVarFinalizer :: MVar a -> IO () -> IO () #
modifyMVarMasked :: MVar a -> (a -> IO (a, b)) -> IO b #
Like modifyMVar
, but the IO
action in the second argument is executed with
asynchronous exceptions masked.
Since: base-4.6.0.0
modifyMVarMasked_ :: MVar a -> (a -> IO a) -> IO () #
Like modifyMVar_
, but the IO
action in the second argument is executed with
asynchronous exceptions masked.
Since: base-4.6.0.0
modifyMVar :: MVar a -> (a -> IO (a, b)) -> IO b #
A slight variation on modifyMVar_
that allows a value to be
returned (b
) in addition to the modified value of the MVar
.
modifyMVar_ :: MVar a -> (a -> IO a) -> IO () #
An exception-safe wrapper for modifying the contents of an MVar
.
Like withMVar
, modifyMVar
will replace the original contents of
the MVar
if an exception is raised during the operation. This
function is only atomic if there are no other producers for this
MVar
.
withMVarMasked :: MVar a -> (a -> IO b) -> IO b #
Like withMVar
, but the IO
action in the second argument is executed
with asynchronous exceptions masked.
Since: base-4.7.0.0
withMVar :: MVar a -> (a -> IO b) -> IO b #
withMVar
is an exception-safe wrapper for operating on the contents
of an MVar
. This operation is exception-safe: it will replace the
original contents of the MVar
if an exception is raised (see
Control.Exception). However, it is only atomic if there are no
other producers for this MVar
.
mkWeakThreadId :: ThreadId -> IO (Weak ThreadId) #
Make a weak pointer to a ThreadId
. It can be important to do
this if you want to hold a reference to a ThreadId
while still
allowing the thread to receive the BlockedIndefinitely
family of
exceptions (e.g. BlockedIndefinitelyOnMVar
). Holding a normal
ThreadId
reference will prevent the delivery of
BlockedIndefinitely
exceptions because the reference could be
used as the target of throwTo
at any time, which would unblock
the thread.
Holding a Weak ThreadId
, on the other hand, will not prevent the
thread from receiving BlockedIndefinitely
exceptions. It is
still possible to throw an exception to a Weak ThreadId
, but the
caller must use deRefWeak
first to determine whether the thread
still exists.
Since: base-4.6.0.0
threadCapability :: ThreadId -> IO (Int, Bool) #
Returns the number of the capability on which the thread is currently
running, and a boolean indicating whether the thread is locked to
that capability or not. A thread is locked to a capability if it
was created with forkOn
.
Since: base-4.4.0.0
The yield
action allows (forces, in a co-operative multitasking
implementation) a context-switch to any other currently runnable
threads (if any), and is occasionally useful when implementing
concurrency abstractions.
myThreadId :: IO ThreadId #
Returns the ThreadId
of the calling thread (GHC only).
throwTo :: Exception e => ThreadId -> e -> IO () #
throwTo
raises an arbitrary exception in the target thread (GHC only).
Exception delivery synchronizes between the source and target thread:
throwTo
does not return until the exception has been raised in the
target thread. The calling thread can thus be certain that the target
thread has received the exception. Exception delivery is also atomic
with respect to other exceptions. Atomicity is a useful property to have
when dealing with race conditions: e.g. if there are two threads that
can kill each other, it is guaranteed that only one of the threads
will get to kill the other.
Whatever work the target thread was doing when the exception was raised is not lost: the computation is suspended until required by another thread.
If the target thread is currently making a foreign call, then the
exception will not be raised (and hence throwTo
will not return)
until the call has completed. This is the case regardless of whether
the call is inside a mask
or not. However, in GHC a foreign call
can be annotated as interruptible
, in which case a throwTo
will
cause the RTS to attempt to cause the call to return; see the GHC
documentation for more details.
Important note: the behaviour of throwTo
differs from that described in
the paper "Asynchronous exceptions in Haskell"
(http://research.microsoft.com/~simonpj/Papers/asynch-exns.htm).
In the paper, throwTo
is non-blocking; but the library implementation adopts
a more synchronous design in which throwTo
does not return until the exception
is received by the target thread. The trade-off is discussed in Section 9 of the paper.
Like any blocking operation, throwTo
is therefore interruptible (see Section 5.3 of
the paper). Unlike other interruptible operations, however, throwTo
is always interruptible, even if it does not actually block.
There is no guarantee that the exception will be delivered promptly,
although the runtime will endeavour to ensure that arbitrary
delays don't occur. In GHC, an exception can only be raised when a
thread reaches a safe point, where a safe point is where memory
allocation occurs. Some loops do not perform any memory allocation
inside the loop and therefore cannot be interrupted by a throwTo
.
If the target of throwTo
is the calling thread, then the behaviour
is the same as throwIO
, except that the exception
is thrown as an asynchronous exception. This means that if there is
an enclosing pure computation, which would be the case if the current
IO operation is inside unsafePerformIO
or unsafeInterleaveIO
, that
computation is not permanently replaced by the exception, but is
suspended as if it had received an asynchronous exception.
Note that if throwTo
is called with the current thread as the
target, the exception will be thrown even if the thread is currently
inside mask
or uninterruptibleMask
.
killThread :: ThreadId -> IO () #
killThread
raises the ThreadKilled
exception in the given
thread (GHC only).
killThread tid = throwTo tid ThreadKilled
setNumCapabilities :: Int -> IO () #
Set the number of Haskell threads that can run truly simultaneously
(on separate physical processors) at any given time. The number
passed to forkOn
is interpreted modulo this value. The initial
value is given by the +RTS -N
runtime flag.
This is also the number of threads that will participate in parallel garbage collection. It is strongly recommended that the number of capabilities is not set larger than the number of physical processor cores, and it may often be beneficial to leave one or more cores free to avoid contention with other processes in the machine.
Since: base-4.5.0.0
getNumCapabilities :: IO Int #
Returns the number of Haskell threads that can run truly
simultaneously (on separate physical processors) at any given time. To change
this value, use setNumCapabilities
.
Since: base-4.4.0.0
isEmptyMVar :: MVar a -> IO Bool #
Check whether a given MVar
is empty.
Notice that the boolean value returned is just a snapshot of
the state of the MVar. By the time you get to react on its result,
the MVar may have been filled (or emptied) - so be extremely
careful when using this operation. Use tryTakeMVar
instead if possible.
tryReadMVar :: MVar a -> IO (Maybe a) #
tryPutMVar :: MVar a -> a -> IO Bool #
A non-blocking version of putMVar
. The tryPutMVar
function
attempts to put the value a
into the MVar
, returning True
if
it was successful, or False
otherwise.
tryTakeMVar :: MVar a -> IO (Maybe a) #
A non-blocking version of takeMVar
. The tryTakeMVar
function
returns immediately, with Nothing
if the MVar
was empty, or
if the Just
aMVar
was full with contents a
. After tryTakeMVar
,
the MVar
is left empty.
putMVar :: MVar a -> a -> IO () #
Put a value into an MVar
. If the MVar
is currently full,
putMVar
will wait until it becomes empty.
There are two further important properties of putMVar
:
putMVar
is single-wakeup. That is, if there are multiple threads blocked inputMVar
, and theMVar
becomes empty, only one thread will be woken up. The runtime guarantees that the woken thread completes itsputMVar
operation.- When multiple threads are blocked on an
MVar
, they are woken up in FIFO order. This is useful for providing fairness properties of abstractions built usingMVar
s.
Atomically read the contents of an MVar
. If the MVar
is
currently empty, readMVar
will wait until it is full.
readMVar
is guaranteed to receive the next putMVar
.
readMVar
is multiple-wakeup, so when multiple readers are
blocked on an MVar
, all of them are woken up at the same time.
Compatibility note: Prior to base 4.7, readMVar
was a combination
of takeMVar
and putMVar
. This mean that in the presence of
other threads attempting to putMVar
, readMVar
could block.
Furthermore, readMVar
would not receive the next putMVar
if there
was already a pending thread blocked on takeMVar
. The old behavior
can be recovered by implementing 'readMVar as follows:
readMVar :: MVar a -> IO a readMVar m = mask_ $ do a <- takeMVar m putMVar m a return a
Return the contents of the MVar
. If the MVar
is currently
empty, takeMVar
will wait until it is full. After a takeMVar
,
the MVar
is left empty.
There are two further important properties of takeMVar
:
takeMVar
is single-wakeup. That is, if there are multiple threads blocked intakeMVar
, and theMVar
becomes full, only one thread will be woken up. The runtime guarantees that the woken thread completes itstakeMVar
operation.- When multiple threads are blocked on an
MVar
, they are woken up in FIFO order. This is useful for providing fairness properties of abstractions built usingMVar
s.
newEmptyMVar :: IO (MVar a) #
Create an MVar
which is initially empty.
An MVar
(pronounced "em-var") is a synchronising variable, used
for communication between concurrent threads. It can be thought of
as a box, which may be empty or full.
Instances
NFData1 MVar | Since: deepseq-1.4.3.0 |
Defined in Control.DeepSeq | |
Eq (MVar a) | Since: base-4.1.0.0 |
NFData (MVar a) | NOTE: Only strict in the reference and not the referenced value. Since: deepseq-1.4.2.0 |
Defined in Control.DeepSeq | |
PrimUnlifted (MVar a) | |
Defined in Data.Primitive.Unlifted.Class toUnlifted# :: MVar a -> Unlifted (MVar a) # fromUnlifted# :: Unlifted (MVar a) -> MVar a # writeUnliftedArray# :: MutableArrayArray# s -> Int# -> MVar a -> State# s -> State# s # readUnliftedArray# :: MutableArrayArray# s -> Int# -> State# s -> (# State# s, MVar a #) # indexUnliftedArray# :: ArrayArray# -> Int# -> MVar a # | |
type Unlifted (MVar a) | |
Defined in Data.Primitive.Unlifted.Class |
waitBarrierMaybe :: Barrier a -> IO (Maybe a) #
A version of waitBarrier
that never blocks, returning Nothing
if the barrier has not yet been signaled.
waitBarrier :: Barrier a -> IO a #
Wait until a barrier has been signaled with signalBarrier
.
signalBarrier :: Partial => Barrier a -> a -> IO () #
Write a value into the Barrier, releasing anyone at waitBarrier
.
Any subsequent attempts to signal the Barrier
will throw an exception.
newBarrier :: IO (Barrier a) #
Create a new Barrier
.
modifyVar_' :: Var a -> (a -> IO a) -> IO () #
Strict variant of modifyVar_
onceFork :: IO a -> IO (IO a) #
Like once
, but immediately starts running the computation on a background thread.
\(x :: IO Int) -> join (onceFork x) == x \(x :: IO Int) -> (do a <- onceFork x; a; a) == x
Given an action, produce a wrapped action that runs at most once. If the function raises an exception, the same exception will be reraised each time.
let x ||| y = do t1 <- onceFork x; t2 <- onceFork y; t1; t2 \(x :: IO Int) -> void (once x) == pure () \(x :: IO Int) -> join (once x) == x \(x :: IO Int) -> (do y <- once x; y; y) == x \(x :: IO Int) -> (do y <- once x; y ||| y) == x
withNumCapabilities :: Int -> IO a -> IO a #
On GHC 7.6 and above with the -threaded
flag, brackets a call to setNumCapabilities
.
On lower versions (which lack setNumCapabilities
) this function just runs the argument action.
Like an MVar
, but has no value.
Used to guarantee single-threaded access, typically to some system resource.
As an example:
lock <-newLock
let output =withLock
lock . putStrLn forkIO $ do ...; output "hello" forkIO $ do ...; output "world"
Here we are creating a lock to ensure that when writing output our messages
do not get interleaved. This use of MVar
never blocks on a put. It is permissible,
but rare, that a withLock contains a withLock inside it - but if so,
watch out for deadlocks.
Like an MVar
, but must always be full.
Used to operate on a mutable variable in a thread-safe way.
As an example:
hits <-newVar
0 forkIO $ do ...;modifyVar_
hits (+1); ... i <-readVar
hits print ("HITS",i)
Here we have a variable which we modify atomically, so modifications are
not interleaved. This use of MVar
never blocks on a put. No modifyVar
operation should ever block, and they should always complete in a reasonable
timeframe. A Var
should not be used to protect some external resource, only
the variable contained within. Information from a readVar
should not be subsequently
inserted back into the Var
.
Starts out empty, then is filled exactly once. As an example:
bar <-newBarrier
forkIO $ do ...; val <- ...;signalBarrier
bar val print =<<waitBarrier
bar
Here we create a barrier which will contain some computed value. A thread is forked to fill the barrier, while the main thread waits for it to complete. A barrier has similarities to a future or promise from other languages, has been known as an IVar in other Haskell work, and in some ways is like a manually managed thunk.