Function std::sync::mpsc::sync_channel
[−]
[src]
pub fn sync_channel<T>(bound: usize) -> (SyncSender<T>, Receiver<T>)
Creates a new synchronous, bounded channel.
Like asynchronous channels, the Receiver
will block until a message
becomes available. These channels differ greatly in the semantics of the
sender from asynchronous channels, however.
This channel has an internal buffer on which messages will be queued. When the internal buffer becomes full, future sends will block waiting for the buffer to open up. Note that a buffer size of 0 is valid, in which case this becomes "rendezvous channel" where each send will not return until a recv is paired with it.
As with asynchronous channels, all senders will panic in send
if the
Receiver
has been destroyed.
Examples
fn main() { use std::sync::mpsc::sync_channel; use std::thread; let (tx, rx) = sync_channel(1); // this returns immediately tx.send(1).unwrap(); thread::spawn(move|| { // this will block until the previous message has been received tx.send(2).unwrap(); }); assert_eq!(rx.recv().unwrap(), 1); assert_eq!(rx.recv().unwrap(), 2); }use std::sync::mpsc::sync_channel; use std::thread; let (tx, rx) = sync_channel(1); // this returns immediately tx.send(1).unwrap(); thread::spawn(move|| { // this will block until the previous message has been received tx.send(2).unwrap(); }); assert_eq!(rx.recv().unwrap(), 1); assert_eq!(rx.recv().unwrap(), 2);