File tree Expand file tree Collapse file tree 2 files changed +13
-18
lines changed Expand file tree Collapse file tree 2 files changed +13
-18
lines changed Original file line number Diff line number Diff line change @@ -18,7 +18,9 @@ struct JobStatus {
18
18
}
19
19
20
20
fn main ( ) {
21
+ // TODO: `Arc` isn't enough if you want a **mutable** shared state
21
22
let status = Arc :: new ( JobStatus { jobs_completed : 0 } ) ;
23
+
22
24
let mut handles = vec ! [ ] ;
23
25
for _ in 0 ..10 {
24
26
let status_shared = Arc :: clone ( & status) ;
@@ -29,11 +31,12 @@ fn main() {
29
31
} ) ;
30
32
handles. push ( handle) ;
31
33
}
34
+
35
+ // Waiting for all jobs to complete
32
36
for handle in handles {
33
37
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 {}" , ???) ;
38
38
}
39
+
40
+ // TODO: Print the value of `JobStatus.jobs_completed`
41
+ println ! ( "Jobs completed: {}" , ???) ;
39
42
}
Original file line number Diff line number Diff line change @@ -1136,25 +1136,17 @@ to **immutable** data. But we want to *change* the number of `jobs_completed`
1136
1136
so we'll need to also use another type that will only allow one thread to
1137
1137
mutate the data at a time. Take a look at this section of the book:
1138
1138
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 :)
1140
1139
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:
1142
1143
```
1143
1144
let status = Arc::new(Mutex::new(JobStatus { jobs_completed: 0 }));
1144
1145
```
1145
1146
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
+ """
1158
1150
1159
1151
[[exercises ]]
1160
1152
name = " threads3"
You can’t perform that action at this time.
0 commit comments