diff --git a/exercises/20_threads/threads1.rs b/exercises/20_threads/threads1.rs index 4dd26c1..3db13e2 100644 --- a/exercises/20_threads/threads1.rs +++ b/exercises/20_threads/threads1.rs @@ -8,8 +8,6 @@ // Execute `rustlings hint threads1` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE - use std::thread; use std::time::{Duration, Instant}; diff --git a/exercises/20_threads/threads2.rs b/exercises/20_threads/threads2.rs index 60d6824..34a7df6 100644 --- a/exercises/20_threads/threads2.rs +++ b/exercises/20_threads/threads2.rs @@ -7,9 +7,7 @@ // Execute `rustlings hint threads2` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE - -use std::sync::Arc; +use std::sync::{Arc, Mutex}; use std::thread; use std::time::Duration; @@ -19,7 +17,7 @@ struct JobStatus { fn main() { // TODO: `Arc` isn't enough if you want a **mutable** shared state - let status = Arc::new(JobStatus { jobs_completed: 0 }); + let status = Arc::new(Mutex::new(JobStatus { jobs_completed: 0 })); let mut handles = vec![]; for _ in 0..10 { @@ -27,7 +25,8 @@ fn main() { let handle = thread::spawn(move || { thread::sleep(Duration::from_millis(250)); // TODO: You must take an action before you update a shared value - status_shared.jobs_completed += 1; + let mut s = status_shared.lock().unwrap(); + s.jobs_completed += 1; }); handles.push(handle); } @@ -38,5 +37,5 @@ fn main() { } // TODO: Print the value of `JobStatus.jobs_completed` - println!("Jobs completed: {}", ???); + println!("Jobs completed: {}", status.lock().unwrap().jobs_completed); }