pub fn channel<T>(buffer: usize) -> (Sender<T>, Receiver<T>)Expand description
Creates a bounded channel for communicating between tasks with backpressure.
The channel buffers up to buffer messages. Once the buffer is full, a
send waits until a message is received. The buffer must hold at least one
message.
Everything sent on a Sender comes out of the Receiver, in the order
it was sent (see Ordering). The Sender can be cloned
to send to the same channel from many places; only one Receiver exists.
If the Receiver is closed or dropped, a send returns a
SendError with the value. When every Sender is dropped, recv
returns None once the buffered messages are received.
ยงPanics
Panics if buffer is 0, or larger than
Semaphore::MAX_PERMITS.
use nuclei::sync::mpsc;
let (tx, mut rx) = mpsc::channel(4);
let producer = std::thread::spawn(move || {
for i in 0..10 {
tx.blocking_send(i).expect("the receiver is still there");
}
});
for i in 0..10 {
assert_eq!(rx.blocking_recv(), Some(i));
}
assert_eq!(rx.blocking_recv(), None);
producer.join().unwrap();