trpl-zh-cn/listings/ch16-fearless-concurrency/listing-16-13/src/main.rs
2022-02-06 16:43:51 +08:00

23 lines
429 B
Rust
Executable File

use std::sync::Mutex;
use std::thread;
fn main() {
let counter = Mutex::new(0);
let mut handles = vec![];
for _ in 0..10 {
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());
}