Untitled
unknown
plain_text
2 years ago
664 B
9
Indexable
fn incremente_et_compte(compteur:Arc<Mutex<MonCompteur>>) {
let mut c = compteur.lock().unwrap();
c.i += 1;
println!("{}", c.i);
}
struct MonCompteur {
i: i32
}
use std::{sync::{Arc, Mutex}, time::Duration};
fn main() {
let compteur = MonCompteur {i: 0};
let my_arc = Arc::new(Mutex::new(compteur));
let mut handles = vec![];
while my_arc.lock().unwrap().i < 10 {
let clone = my_arc.clone();
handles.push(std::thread::spawn(move || {
std::thread::sleep(std::time::Duration::from_secs(10));
incremente_et_compte(clone);
}));
}
for h in handles {
h.join();
}
}Editor is loading...
Leave a Comment