02-消息传递
消息传递 — Rust By Practice
内容来源 · Rust By Practice / Rust 语言实战
原文链接: https://practice-rust.beatai.org/threads/message-passing.html
消息传递
Rust 鼓励通过消息传递在线程间通信。std::sync::mpsc 提供多生产者、单消费者通道。
- 🌟
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| // 让代码工作
use std::sync::mpsc;
use std::thread;
fn main() {
let (tx, rx) = mpsc::channel();
thread::spawn(move || {
let val = String::from("hi");
tx.send(val).unwrap();
});
let received = rx.recv().unwrap();
assert_eq!(received, "hi");
println!("Success!");
}
|
- 🌟
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| // 让代码工作
use std::sync::mpsc;
use std::thread;
use std::time::Duration;
fn main() {
let (tx, rx) = mpsc::channel();
thread::spawn(move || {
let vals = vec![
String::from("hi"),
String::from("from"),
String::from("the"),
String::from("thread"),
];
for val in vals {
tx.send(val).unwrap();
thread::sleep(Duration::from_secs(1));
}
});
for received in rx {
println!("Got: {}", received);
}
println!("Success!");
}
|
- 🌟🌟
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| // 让代码工作:克隆发送端,多个线程发送消息
use std::sync::mpsc;
use std::thread;
fn main() {
let (tx, rx) = mpsc::channel();
let tx1 = tx.clone();
thread::spawn(move || {
tx.send(String::from("hi")).unwrap();
});
thread::spawn(move || {
tx1.send(String::from("from")).unwrap();
});
for received in rx {
println!("Got: {}", received);
}
println!("Success!");
}
|
- 🌟🌟
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| // 填空
use std::sync::mpsc;
use std::thread;
fn main() {
let (tx, rx) = mpsc::channel();
thread::spawn(move || {
for i in 0..3 {
tx.send(i).unwrap();
}
});
assert_eq!(rx.recv().unwrap(), 0);
assert_eq!(rx.recv().unwrap(), 1);
assert_eq!(__, 2);
println!("Success!");
}
|
你可以在这里找到答案(在 solutions 路径下),但请只在需要时使用