mpmc_channel)Expand description
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. 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:
#![feature(mpmc_channel)]
use std::thread;
use std::sync::mpmc::channel;
// Create a simple streaming channel
let (tx, rx) = channel();
thread::spawn(move || {
tx.send(10).unwrap();
});
assert_eq!(rx.recv().unwrap(), 10);Shared usage:
#![feature(mpmc_channel)]
use std::thread;
use std::sync::mpmc::channel;
thread::scope(|s| {
// Create a shared channel that can be sent along from many threads
// where tx is the sending half (tx for transmission), and rx is the receiving
// half (rx for receiving).
let (tx, rx) = channel();
for i in 0..10 {
let tx = tx.clone();
s.spawn(move || {
tx.send(i).unwrap();
});
}
for _ in 0..5 {
let rx1 = rx.clone();
let rx2 = rx.clone();
s.spawn(move || {
let j = rx1.recv().unwrap();
assert!(0 <= j && j < 10);
});
s.spawn(move || {
let j = rx2.recv().unwrap();
assert!(0 <= j && j < 10);
});
}
})Propagating panics:
#![feature(mpmc_channel)]
use std::sync::mpmc::channel;
// The call to recv() will return an error because the channel has already
// hung up (or been deallocated)
let (tx, rx) = channel::<i32>();
drop(tx);
assert!(rx.recv().is_err());Structs§
- Into
Iter Experimental - An owning iterator over messages on a
Receiver, created byinto_iter. - Iter
Experimental - An iterator over messages on a
Receiver, created byiter. - Receiver
Experimental - The receiving half of Rust’s
channel(orsync_channel) type. Different threads can share thisReceiverby cloning it. - Recv
Error Experimental - An error returned from the
recvfunction on aReceiver. - Send
Error Experimental - An error returned from the
Sender::sendorSyncSender::sendfunction on channels. - Sender
Experimental - The sending-half of Rust’s synchronous
channeltype. - TryIter
Experimental - An iterator that attempts to yield all pending values for a
Receiver, created bytry_iter.
Enums§
- Recv
Timeout Error Experimental - This enumeration is the list of possible errors that made
recv_timeoutunable to return data when called. This can occur with both achanneland async_channel. - Send
Timeout Error Experimental - An error returned from the
send_timeoutmethod. - TryRecv
Error Experimental - This enumeration is the list of the possible reasons that
try_recvcould not return data when called. This can occur with both achanneland async_channel. - TrySend
Error Experimental - This enumeration is the list of the possible error outcomes for the
try_sendmethod.
Functions§
- channel
Experimental - Creates a new asynchronous channel, returning the sender/receiver halves.
- sync_
channel Experimental - Creates a new synchronous, bounded channel.