mirror of https://github.com/KaiserY/trpl-zh-cn
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
529 B
28 lines
529 B
use std::sync::Mutex;
|
|
use std::thread;
|
|
|
|
fn main() {
|
|
let counter = Mutex::new(0);
|
|
let mut handles = vec![];
|
|
|
|
let handle = thread::spawn(move || {
|
|
let mut num = counter.lock().unwrap();
|
|
|
|
*num += 1;
|
|
});
|
|
handles.push(handle);
|
|
|
|
let handle2 = thread::spawn(move || {
|
|
let mut num2 = counter.lock().unwrap();
|
|
|
|
*num2 += 1;
|
|
});
|
|
handles.push(handle2);
|
|
|
|
for handle in handles {
|
|
handle.join().unwrap();
|
|
}
|
|
|
|
println!("Result: {}", *counter.lock().unwrap());
|
|
}
|