Module mpmc
Multi-producer, multi-consumer FIFO queue communication primitives.
This module provides message-based communication over channels, concretely defined by two types:
Senders are used to send data to a set of Receivers where each item
sent is delivered to (at most) one receiver. Both sender and receiver are
cloneable (multi-producer) such that many threads can send simultaneously
to receivers (multi-consumer).
These channels come in two flavors:
-
An asynchronous, infinitely buffered channel. The
channelfunction will return a(Sender, Receiver)tuple where all sends will be asynchronous (they never block). The channel conceptually has an infinite buffer. -
A synchronous, bounded channel. The
sync_channelfunction will return a(Sender, Receiver)tuple where the storage for pending messages is a pre-allocated buffer of a fixed size. All sends will be synchronous by blocking until there is buffer space available. Note that a bound of 0 is allowed, causing the channel to become a "rendezvous" channel where each sender atomically hands off a message to a receiver.
Disconnection
The send and receive operations on channels will all return a Result
indicating whether the operation succeeded or not. An unsuccessful operation
is normally indicative of the other half of a channel having "hung up" by
being dropped in its corresponding thread.
Once half of a channel has been deallocated, most operations can no longer
continue to make progress, so Err will be returned. Many applications
will continue to unwrap the results returned from this module,
instigating a propagation of failure among threads if one unexpectedly dies.
Examples
Simple usage:
use thread;
use channel;
// Create a simple streaming channel
let = channel;
spawn;
assert_eq!;
Shared usage:
use thread;
use channel;
scope
Propagating panics:
use channel;
// The call to recv() will return an error because the channel has already
// hung up (or been deallocated)
let = ;
drop;
assert!;
Structs
-
IntoIter
An owning iterator over messages on a
Receiver, created byinto_iter. -
Iter
An iterator over messages on a
Receiver, created byiter. -
Receiver
The receiving half of Rust's
channel(orsync_channel) type. Different threads can share thisReceiverby cloning it. -
Sender
The sending-half of Rust's synchronous
channeltype. -
TryIter
An iterator that attempts to yield all pending values for a
Receiver, created bytry_iter.
Functions
- channel Creates a new asynchronous channel, returning the sender/receiver halves.
- sync_channel Creates a new synchronous, bounded channel.