W3cubDocs

/Rust

Function std::sync::mpsc::sync_channel

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. bound specifies the buffer size. 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.

Like asynchronous channels, if the Receiver is disconnected while trying to send() with the SyncSender, the send() method will return an error.

Examples

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);

© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/sync/mpsc/fn.sync_channel.html