Skip to content

Instantly share code, notes, and snippets.

@Haniyya
Created May 26, 2019 05:46
Show Gist options
  • Save Haniyya/c59eae6b369ac152914999704f84ac6b to your computer and use it in GitHub Desktop.
Save Haniyya/c59eae6b369ac152914999704f84ac6b to your computer and use it in GitHub Desktop.
use std::sync::{Mutex, Arc};
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();
}
println!("Result: {}", *counter.lock().unwrap());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment