Skip to content

Commit 8e01bb9

Browse files
author
build
committed
feat:update
1 parent 8b94861 commit 8e01bb9

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed

exercises/threads/threads1.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// Execute `rustlings hint threads1` or use the `hint` watch subcommand for a
99
// hint.
1010

11-
// I AM NOT DONE
12-
1311
use std::thread;
1412
use std::time::{Duration, Instant};
1513

@@ -27,6 +25,7 @@ fn main() {
2725
let mut results: Vec<u128> = vec![];
2826
for handle in handles {
2927
// TODO: a struct is returned from thread::spawn, can you use it?
28+
results.push(handle.join().unwrap());
3029
}
3130

3231
if results.len() != 10 {

exercises/threads/threads2.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
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, Mutex};
1311
use std::thread;
1412
use std::time::Duration;
1513

@@ -18,14 +16,15 @@ struct JobStatus {
1816
}
1917

2018
fn main() {
21-
let status = Arc::new(JobStatus { jobs_completed: 0 });
19+
let status = Arc::new(Mutex::new(JobStatus { jobs_completed: 0 }));
2220
let mut handles = vec![];
2321
for _ in 0..10 {
2422
let status_shared = Arc::clone(&status);
2523
let handle = thread::spawn(move || {
2624
thread::sleep(Duration::from_millis(250));
2725
// TODO: You must take an action before you update a shared value
28-
status_shared.jobs_completed += 1;
26+
let mut s_status = status_shared.lock().unwrap();
27+
s_status.jobs_completed += 1;
2928
});
3029
handles.push(handle);
3130
}
@@ -34,6 +33,6 @@ fn main() {
3433
// TODO: Print the value of the JobStatus.jobs_completed. Did you notice
3534
// anything interesting in the output? Do you have to 'join' on all the
3635
// handles?
37-
println!("jobs completed {}", ???);
36+
println!("jobs completed {}", status.lock().unwrap().jobs_completed);
3837
}
3938
}

0 commit comments

Comments
 (0)