concurrent-extra-0.3.1: Extra concurrency primitives

MaintainerBas van Dijk <> , Roel van Dijk <>



A Broadcast variable is a mechanism for communication between threads. Multiple reader threads can wait until a broadcaster thread writes a signal. The readers block until the signal is received. When the broadcaster sends the signal all readers are woken.

All functions are exception safe. Throwing asynchronous exceptions will not compromise the internal state of a Broadcast variable.

This module is designed to be imported qualified. We suggest importing it like:

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



data Broadcast α Source

A broadcast variable. It can be thought of as a box, which may be empty of full.


new :: IO (Broadcast α)Source

Create a new empty Broadcast variable.

newWritten :: α -> IO (Broadcast α)Source

Create a new Broadcast variable containing an initial value.

read :: Broadcast α -> IO αSource

Read the value of a Broadcast variable.

If the Broadcast variable contains a value it will be returned immediately, otherwise it will block until another thread writes a value to the Broadcast variable.

tryRead :: Broadcast α -> IO (Maybe α)Source

Try to read the value of a Broadcast variable; non blocking.

Like read but doesn't block. Returns Just the contents of the Broadcast if it wasn't empty, Nothing otherwise.

readTimeout :: Broadcast α -> Integer -> IO (Maybe α)Source

Read the value of a Broadcast variable if it is available within a given amount of time.

Like read, but with a timeout. A return value of Nothing indicates a timeout occurred.

The timeout is specified in microseconds. A timeout of 0 μs will cause the function to return Nothing without blocking in case the Broadcast was empty. Negative timeouts are treated the same as a timeout of 0 μs.

write :: Broadcast α -> α -> IO ()Source

Write a new value into a Broadcast variable.

If the variable is empty any threads that are reading from the variable will be woken. If the variable is full its contents will simply be overwritten.

clear :: Broadcast α -> IO ()Source

Clear the contents of a Broadcast variable.