From b4f52cb937e9f2b90913402964ae014240705a5f Mon Sep 17 00:00:00 2001 From: jaystile <46078028+jaystile@users.noreply.github.com> Date: Thu, 23 Dec 2021 06:19:39 -0800 Subject: feat: Adding threads1.rs with a focus on JoinHandles and waiting for spawned threads to finish. Moved the original threads1.rs to threads2.rs with the focus on the Mutex and modifying shared data. #892 --- exercises/threads/threads1.rs | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) (limited to 'exercises/threads/threads1.rs') diff --git a/exercises/threads/threads1.rs b/exercises/threads/threads1.rs index f31b317..ddb6155 100644 --- a/exercises/threads/threads1.rs +++ b/exercises/threads/threads1.rs @@ -1,32 +1,31 @@ // threads1.rs -// Make this compile! Execute `rustlings hint threads1` for hints :) -// The idea is the thread spawned on line 22 is completing jobs while the main thread is -// monitoring progress until 10 jobs are completed. Because of the difference between the -// spawned threads' sleep time, and the waiting threads sleep time, when you see 6 lines -// of "waiting..." and the program ends without timing out when running, -// you've got it :) +// Make this compile and run! Execute 'rustlings hint threads1' for hints :) +// This program should wait until all the spawned threads have finished before exiting. // I AM NOT DONE -use std::sync::Arc; use std::thread; use std::time::Duration; -struct JobStatus { - jobs_completed: u32, -} fn main() { - let status = Arc::new(JobStatus { jobs_completed: 0 }); - let status_shared = status.clone(); - thread::spawn(move || { - for _ in 0..10 { + + let mut handles = vec![]; + for i in 0..10 { + thread::spawn(move || { thread::sleep(Duration::from_millis(250)); - status_shared.jobs_completed += 1; - } - }); - while status.jobs_completed < 10 { - println!("waiting... "); - thread::sleep(Duration::from_millis(500)); + println!("thread {} is complete", i); + }); + } + + let mut completed_threads = 0; + for handle in handles { + // TODO: a struct is returned from thread::spawn, can you use it? + completed_threads += 1; + } + + if completed_threads != 10 { + panic!("Oh no! All the spawned threads did not finish!"); } + } -- cgit v1.2.3 From f99eafc56f49852d4b801b5c4cfedf47ef4968ae Mon Sep 17 00:00:00 2001 From: mokou Date: Fri, 15 Jul 2022 11:59:53 +0200 Subject: fix(threads): add hint comments --- exercises/threads/threads1.rs | 2 +- exercises/threads/threads2.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'exercises/threads/threads1.rs') diff --git a/exercises/threads/threads1.rs b/exercises/threads/threads1.rs index ddb6155..e59f4ce 100644 --- a/exercises/threads/threads1.rs +++ b/exercises/threads/threads1.rs @@ -1,5 +1,5 @@ // threads1.rs -// Make this compile and run! Execute 'rustlings hint threads1' for hints :) +// Execute `rustlings hint threads1` or use the `hint` watch subcommand for a hint. // This program should wait until all the spawned threads have finished before exiting. // I AM NOT DONE diff --git a/exercises/threads/threads2.rs b/exercises/threads/threads2.rs index 2cb0d04..d0f8578 100644 --- a/exercises/threads/threads2.rs +++ b/exercises/threads/threads2.rs @@ -1,5 +1,5 @@ // threads2.rs -// Make this compile! Execute `rustlings hint threads2` for hints :) +// Execute `rustlings hint threads2` or use the `hint` watch subcommand for a hint. // Building on the last exercise, we want all of the threads to complete their work but this time // the spawned threads need to be in charge of updating a shared value: JobStatus.jobs_completed -- cgit v1.2.3