mirror of
https://github.com/KaiserY/trpl-zh-cn
synced 2025-05-03 11:58:04 +08:00
25 lines
498 B
Rust
25 lines
498 B
Rust
use std::rc::Rc;
|
|
use std::sync::Mutex;
|
|
use std::thread;
|
|
|
|
fn main() {
|
|
let counter = Rc::new(Mutex::new(0));
|
|
let mut handles = vec![];
|
|
|
|
for _ in 0..10 {
|
|
let counter = Rc::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();
|
|
}
|
|
|
|
println!("Result: {}", *counter.lock().unwrap());
|
|
}
|