base-4.18.1.0: Basic libraries

Copyright | (c) The University of Glasgow 2001
License | BSD-style (see the file libraries/base/LICENSE)
Maintainer | libraries@haskell.org
Stability | stable
Portability | non-portable (concurrency)
Safe Haskell | Trustworthy
Language | Haskell2010

Control.Concurrent.Chan

Contents

- The `Chan` type
- Operations
- Stream interface

Description

Unbounded channels.

The channels are implemented with `MVar`s and therefore inherit all the caveats that apply to `MVar`s (possibility of races, deadlocks etc). The stm (software transactional memory) library has a more robust implementation of channels called `TChan`s.

Synopsis

- data Chan a
- newChan :: IO (Chan a)
- writeChan :: Chan a -> a -> IO ()
- readChan :: Chan a -> IO a
- dupChan :: Chan a -> IO (Chan a)
- getChanContents :: Chan a -> IO [a]
- writeList2Chan :: Chan a -> [a] -> IO ()

# The `Chan` type

data Chan a Source #

`Chan` is an abstract type representing an unbounded FIFO channel.

#### Instances

Instances details

Eq (Chan a) Source # | Since: base-4.4.0.0
Instance details Defined in Control.Concurrent.Chan Methods (==) :: Chan a -> Chan a -> Bool Source # (/=) :: Chan a -> Chan a -> Bool Source #

# Operations

newChan :: IO (Chan a) Source #

Build and returns a new instance of `Chan`.

writeChan :: Chan a -> a -> IO () Source #

Write a value to a `Chan`.

readChan :: Chan a -> IO a Source #

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.

dupChan :: Chan a -> IO (Chan a) Source #

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`.)

# Stream interface

getChanContents :: Chan a -> IO [a] Source #

Return a lazy list representing the contents of the supplied `Chan`, much like `hGetContents`.

writeList2Chan :: Chan a -> [a] -> IO () Source #

Write an entire list of items to a `Chan`.