03-线程同步:锁、Condvar和信号量
线程同步:锁、Condvar和信号量 — Rust By Practice
内容来源 · Rust By Practice / Rust 语言实战
原文链接: https://practice-rust.beatai.org/threads/sync.html
线程同步:锁、Condvar和信号量
多线程共享可变数据时,常用 Mutex<T> 配合 Arc<T> 实现安全共享。
- 🌟
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| // 让代码工作
use std::sync::Mutex;
fn main() {
let m = Mutex::new(5);
{
let mut num = m.lock().unwrap();
*num = 6;
}
assert_eq!(*m.lock().unwrap(), 6);
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
| // 让代码工作
use std::sync::{Arc, Mutex};
use std::thread;
fn main() {
let counter = Arc::new(Mutex::new(0));
let mut handles = vec![];
for _ in 0..10 {
let counter = Arc::clone(&counter);
let handle = thread::spawn(move || {
let mut num = counter.lock().unwrap();
*num += 1;
});
handles.push(handle);
}
for handle in handles {
handle.join().unwrap();
}
assert_eq!(*counter.lock().unwrap(), 10);
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
| // 填空
use std::sync::{Arc, Mutex};
use std::thread;
fn main() {
let data = Arc::new(Mutex::new(vec![]));
let mut handles = vec![];
for i in 0..3 {
let data = Arc::clone(&data);
handles.push(thread::spawn(move || {
let mut v = data.lock().unwrap();
v.push(i);
}));
}
for h in handles {
h.join().unwrap();
}
let v = data.lock().unwrap();
assert_eq!(v.len(), 3);
println!("Success!");
}
|
你可以在这里找到答案(在 solutions 路径下),但请只在需要时使用