Skip to content

Instantly share code, notes, and snippets.

@mariot
Last active September 24, 2021 14:40
Show Gist options
  • Select an option

  • Save mariot/c838b4590c78dc4d45d4a9f50d29ebee to your computer and use it in GitHub Desktop.

Select an option

Save mariot/c838b4590c78dc4d45d4a9f50d29ebee to your computer and use it in GitHub Desktop.
use std::sync::Mutex;
use std::thread;
fn main() {
let compteur = Mutex::new(0);
let mut manipulateurs = vec![];
for _ in 0..10 {
let manipulateur = thread::spawn(move || {
let mut nombre = compteur.lock().unwrap();
*nombre += 1;
});
manipulateurs.push(manipulateur);
}
for manipulateur in manipulateurs {
manipulateur.join().unwrap();
}
println!("Resultat : {}", *compteur.lock().unwrap());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment