Skip to main content

unbounded_channel

Function unbounded_channel 

Source
pub fn unbounded_channel<T>() -> (UnboundedSender<T>, UnboundedReceiver<T>)
Expand description

Creates an unbounded channel for communicating between tasks without backpressure.

A send never waits and never fails for lack of room, so UnboundedSender::send is an ordinary function, usable from synchronous and asynchronous code alike. The queue grows with the values sent faster than they are received, so memory is the only limit; use channel where a producer can outrun the receiver.

Everything sent on an UnboundedSender comes out of the UnboundedReceiver, in the order it was sent (see Ordering). The sender can be cloned; only one receiver exists.

use nuclei::sync::mpsc;

let (tx, mut rx) = mpsc::unbounded_channel();
let producer = std::thread::spawn(move || {
    for i in 0..10 {
        tx.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();