January 2019
Beginner to intermediate
554 pages
13h 31m
English
Synchronous channels have a bounded buffer and, when it's full, the send method blocks until there's more space in the channel. The usage is otherwise quite similar to asynchronous channels:
// sync_channels.rsuse std::thread; use std::sync::mpsc; fn main() { let (tx, rx) = mpsc::sync_channel(1); let tx_clone = tx.clone(); let _ = tx.send(0); thread::spawn(move || { let _ = tx.send(1); }); thread::spawn(move || { let _ = tx_clone.send(2); }); println!("Received {} via the channel", rx.recv().unwrap()); println!("Received {} via the channel", rx.recv().unwrap()); println!("Received {} via the channel", rx.recv().unwrap()); println!("Received {:?} via the channel", rx.recv());}
The synchronous channel size is 1, which ...
Read now
Unlock full access