Complete thread exercises

This commit is contained in:
tjcardinal 2021-05-16 01:31:29 -05:00
parent 8ad30c1dfe
commit 9108553a2e

View file

@ -6,9 +6,8 @@
// 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::Mutex;
use std::thread;
use std::time::Duration;
@ -17,15 +16,16 @@ 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));
}