Skip to content

Commit d7fcfd8

Browse files
04101847
1 parent 3e74703 commit d7fcfd8

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

exercises/threads/threads2.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
// Execute `rustlings hint threads2` or use the `hint` watch subcommand for a
88
// hint.
99

10-
// I AM NOT DONE
11-
12-
use std::sync::Arc;
10+
use std::sync::Arc;
11+
use std::sync::Mutex;
1312
use std::thread;
1413
use std::time::Duration;
1514

@@ -18,14 +17,15 @@ struct JobStatus {
1817
}
1918

2019
fn main() {
21-
let status = Arc::new(JobStatus { jobs_completed: 0 });
20+
let status = Arc::new(Mutex::new(JobStatus { jobs_completed: 0 }));
2221
let mut handles = vec![];
2322
for _ in 0..10 {
2423
let status_shared = Arc::clone(&status);
2524
let handle = thread::spawn(move || {
2625
thread::sleep(Duration::from_millis(250));
2726
// TODO: You must take an action before you update a shared value
28-
status_shared.jobs_completed += 1;
27+
let mut status_guard = status_shared.lock().unwrap();
28+
status_guard.jobs_completed += 1;
2929
});
3030
handles.push(handle);
3131
}
@@ -34,6 +34,6 @@ fn main() {
3434
// TODO: Print the value of the JobStatus.jobs_completed. Did you notice
3535
// anything interesting in the output? Do you have to 'join' on all the
3636
// handles?
37-
println!("jobs completed {}", ???);
37+
println!("jobs completed {}", status.lock().unwrap().jobs_completed);
3838
}
3939
}

0 commit comments

Comments
 (0)