Skip to content

Commit 842e341

Browse files
committed
threads2: simplify threads2
1 parent 58b03af commit 842e341

File tree

2 files changed

+13
-18
lines changed

2 files changed

+13
-18
lines changed

exercises/20_threads/threads2.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ struct JobStatus {
1818
}
1919

2020
fn main() {
21+
// TODO: `Arc` isn't enough if you want a **mutable** shared state
2122
let status = Arc::new(JobStatus { jobs_completed: 0 });
23+
2224
let mut handles = vec![];
2325
for _ in 0..10 {
2426
let status_shared = Arc::clone(&status);
@@ -29,11 +31,12 @@ fn main() {
2931
});
3032
handles.push(handle);
3133
}
34+
35+
// Waiting for all jobs to complete
3236
for handle in handles {
3337
handle.join().unwrap();
34-
// TODO: Print the value of the JobStatus.jobs_completed. Did you notice
35-
// anything interesting in the output? Do you have to 'join' on all the
36-
// handles?
37-
println!("jobs completed {}", ???);
3838
}
39+
40+
// TODO: Print the value of `JobStatus.jobs_completed`
41+
println!("Jobs completed: {}", ???);
3942
}

info.toml

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,25 +1136,17 @@ to **immutable** data. But we want to *change* the number of `jobs_completed`
11361136
so we'll need to also use another type that will only allow one thread to
11371137
mutate the data at a time. Take a look at this section of the book:
11381138
https://doc.rust-lang.org/book/ch16-03-shared-state.html#atomic-reference-counting-with-arct
1139-
and keep reading if you'd like more hints :)
11401139
1141-
Do you now have an `Arc` `Mutex` `JobStatus` at the beginning of main? Like:
1140+
Keep reading if you'd like more hints :)
1141+
1142+
Do you now have an `Arc<Mutex<JobStatus>>` at the beginning of `main`? Like:
11421143
```
11431144
let status = Arc::new(Mutex::new(JobStatus { jobs_completed: 0 }));
11441145
```
11451146
1146-
Similar to the code in the example in the book that happens after the text
1147-
that says 'Sharing a Mutex<T> Between Multiple Threads'. If not, give that a
1148-
try! If you do and would like more hints, keep reading!!
1149-
1150-
Make sure neither of your threads are holding onto the lock of the mutex
1151-
while they are sleeping, since this will prevent the other thread from
1152-
being allowed to get the lock. Locks are automatically released when
1153-
they go out of scope.
1154-
1155-
If you've learned from the sample solutions, I encourage you to come
1156-
back to this exercise and try it again in a few days to reinforce
1157-
what you've learned :)"""
1147+
Similar to the code in the following example in the book:
1148+
https://doc.rust-lang.org/book/ch16-03-shared-state.html#sharing-a-mutext-between-multiple-threads
1149+
"""
11581150

11591151
[[exercises]]
11601152
name = "threads3"

0 commit comments

Comments
 (0)