From e5954bbb2b30fa6e2ae1231f8d8304082256ce26 Mon Sep 17 00:00:00 2001 From: seth <yesifan66@gmail.com> Date: Fri, 25 Jun 2021 10:54:32 +0800 Subject: [PATCH] complete: threads --- exercises/threads/threads1.rs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/exercises/threads/threads1.rs b/exercises/threads/threads1.rs index f31b317..7051544 100644 --- a/exercises/threads/threads1.rs +++ b/exercises/threads/threads1.rs @@ -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::{Mutex, Arc}; 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_shared = status.clone(); + let status = Arc::new(Mutex::new(JobStatus { jobs_completed: 0 })); + let status_shared = Arc::clone(&status); 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)); }