Rust
Rust 并发篇 (十九):无畏并发 (Fearless Concurrency) 与消息传递
Waitwalker2026-08-2912 min
## 1. 多生产者单消费者 (MPSC) 通道
“不要通过共享内存来通信,而要通过通信来共享内存”:
```rust
use std::sync::mpsc;
use std::thread;
use std::time::Duration;
fn main() {
let (tx, rx) = mpsc::channel();
thread::spawn(move || {
let val = String::from("hello from worker");
tx.send(val).unwrap();
});
let received = rx.recv().unwrap();
println!("主线程收到消息: {}", received);
}
```
#Rust#并发#多线程#MPSC
回到文章列表 →