《Rust实战》读书笔记——第10章,重要的包
[复制链接]
rayon用于提供数据并行性,parse_byte(byte)就是安全的;
crossbeam是线程间的通信包,可以实现单向通信:
#[macro_use]
extern crate crossbeam;
use std::thread;
use crossbeam::channel::unbounded;
fn main() {
let (tx, rx) = unbounded();
thread::spawn(move || {
tx.send(42)
.unwrap();
});
select!{
recv(rx) -> msg => println!("{:?}", msg),
}
}
当然凑一对就成了双向通信:
#[macro_use]
extern crate crossbeam;
use crossbeam::channel::unbounded;
use std::thread;
use crate::ConnectivityCheck::*;
#[derive(Debug)]
enum ConnectivityCheck {
Ping,
Pong,
Pang,
}
fn main() {
let n_messages = 3;
let (requests_tx, requests_rx) = unbounded();
let (responses_tx, responses_rx) = unbounded();
thread::spawn(move || loop {
match requests_rx.recv().unwrap() {
Pong => eprintln!("unexpected pong response"),
Ping => responses_tx.send(Pong).unwrap(),
Pang => return,
}
});
for _ in 0..n_messages {
requests_tx.send(Ping).unwrap();
}
requests_tx.send(Pang).unwrap();
for _ in 0..n_messages {
select! {
recv(responses_rx) -> msg => println!("{:?}", msg),
}
}
}
最后是一堆操作系统概念。奇怪的是里面插了个wasm,却没有展开,也许作者想再版的时候单开一章?
|