Add threads1 solution

This commit is contained in:
David Bailey 2021-01-16 20:45:55 +00:00
parent cc8e0bc00a
commit 0202ba2869

View file

@ -6,9 +6,7 @@
// of "waiting..." and the program ends without timing out when running,
// you've got it :)
// I AM NOT DONE
use std::sync::Arc;
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;
@ -17,15 +15,15 @@ struct JobStatus {
}
fn main() {
let status = Arc::new(JobStatus { jobs_completed: 0 });
let status = Arc::new(Mutex::new(JobStatus { jobs_completed: 0 }));
let status_shared = status.clone();
thread::spawn(move || {
for _ in 0..10 {
thread::sleep(Duration::from_millis(250));
status_shared.jobs_completed += 1;
status_shared.lock().unwrap().jobs_completed += 1;
}
});
while status.jobs_completed < 10 {
while status.lock().unwrap().jobs_completed < 10 {
println!("waiting... ");
thread::sleep(Duration::from_millis(500));
}