November 2017
Intermediate to advanced
264 pages
5h 45m
English
The kind of sending channel we have used up until now has been asynchronous, which means it does not block the executing code. Rust also has a synchronous channel type, sync_channel, where the send() ;method blocks the code execution if its internal buffer becomes full; it waits until the parent thread starts receiving the data. In the following code, this type of channel is used to send a value of struct Msg over the channel:
// code from Chapter 9/code/sync_channel.rs:
use std::sync::mpsc::sync_channel;
use std::thread;
type TokenType = i32;
struct Msg {
typ: TokenType,
val: String,
}
fn main() {
let (tx, rx) = sync_channel(1); // buffer size 1 tx.send(Msg {typ: 42, val: "Rust is cool".to_string()}).unwrap(); ...Read now
Unlock full access