Merge pull request #1929 from mo8it/threads2

threads2: simplify the exercise
This commit is contained in:
liv 2024-03-31 16:22:54 +02:00 committed by GitHub
commit ca07abf3dc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 13 additions and 18 deletions

View file

@ -18,7 +18,9 @@ struct JobStatus {
} }
fn main() { 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(JobStatus { jobs_completed: 0 });
let mut handles = vec![]; let mut handles = vec![];
for _ in 0..10 { for _ in 0..10 {
let status_shared = Arc::clone(&status); let status_shared = Arc::clone(&status);
@ -29,11 +31,12 @@ fn main() {
}); });
handles.push(handle); handles.push(handle);
} }
// Waiting for all jobs to complete
for handle in handles { for handle in handles {
handle.join().unwrap(); handle.join().unwrap();
// TODO: Print the value of the JobStatus.jobs_completed. Did you notice
// anything interesting in the output? Do you have to 'join' on all the
// handles?
println!("jobs completed {}", ???);
} }
// TODO: Print the value of `JobStatus.jobs_completed`
println!("Jobs completed: {}", ???);
} }

View file

@ -1136,25 +1136,17 @@ to **immutable** data. But we want to *change* the number of `jobs_completed`
so we'll need to also use another type that will only allow one thread to so we'll need to also use another type that will only allow one thread to
mutate the data at a time. Take a look at this section of the book: mutate the data at a time. Take a look at this section of the book:
https://doc.rust-lang.org/book/ch16-03-shared-state.html#atomic-reference-counting-with-arct https://doc.rust-lang.org/book/ch16-03-shared-state.html#atomic-reference-counting-with-arct
and keep reading if you'd like more hints :)
Do you now have an `Arc` `Mutex` `JobStatus` at the beginning of main? Like: Keep reading if you'd like more hints :)
Do you now have an `Arc<Mutex<JobStatus>>` at the beginning of `main`? Like:
``` ```
let status = Arc::new(Mutex::new(JobStatus { jobs_completed: 0 })); let status = Arc::new(Mutex::new(JobStatus { jobs_completed: 0 }));
``` ```
Similar to the code in the example in the book that happens after the text Similar to the code in the following example in the book:
that says 'Sharing a Mutex<T> Between Multiple Threads'. If not, give that a https://doc.rust-lang.org/book/ch16-03-shared-state.html#sharing-a-mutext-between-multiple-threads
try! If you do and would like more hints, keep reading!! """
Make sure neither of your threads are holding onto the lock of the mutex
while they are sleeping, since this will prevent the other thread from
being allowed to get the lock. Locks are automatically released when
they go out of scope.
If you've learned from the sample solutions, I encourage you to come
back to this exercise and try it again in a few days to reinforce
what you've learned :)"""
[[exercises]] [[exercises]]
name = "threads3" name = "threads3"