Function crossbeam_channel::bounded[][src]

pub fn bounded<T>(cap: usize) -> (Sender<T>, Receiver<T>)

Creates a channel of bounded capacity.

This type of channel has an internal buffer of length cap in which messages get queued.

A rather special case is zero-capacity channel, also known as rendezvous channel. Such a channel cannot hold any messages since its buffer is of length zero. Instead, send and receive operations must be executing at the same time in order to pair up and pass the message over.

Examples

use std::thread;
use std::time::Duration;
use crossbeam_channel as channel;

let (s, r) = channel::bounded(1);

// This call returns immediately since there is enough space in the channel.
s.send(1);

thread::spawn(move || {
    // This call blocks the current thread because the channel is full.
    // It will be able to complete only after the first message is received.
    s.send(2);
});

thread::sleep(Duration::from_secs(1));
assert_eq!(r.recv(), Some(1));
assert_eq!(r.recv(), Some(2));
use std::thread;
use std::time::Duration;
use crossbeam_channel as channel;

let (s, r) = channel::bounded(0);

thread::spawn(move || {
    // This call blocks the current thread until a receive operation appears
    // on the other side of the channel.
    s.send(1);
});

thread::sleep(Duration::from_secs(1));
assert_eq!(r.recv(), Some(1));