From 791120c0c1ac66186a5a1b05cc525d94d19ea5f3 Mon Sep 17 00:00:00 2001 From: Donald Guy <donaldguy@hey.com> Date: Tue, 4 May 2021 18:49:13 -0400 Subject: [PATCH 1/3] threads1: use more threads, model more reference counting - Switch from a single additional thread (receiving/holding a single `Arc::clone` reference) running a loop modeling 10 sequential jobs, to a loop generating 10 threads each modeling 1 job (each getting their own `Arc::clone` reference) - use the previously ignored `for` loop var to keep the execution timing approx the same - Print a more descriptive waiting message (taking an opportunity to use comment to disambiguate the count) --- exercises/threads/threads1.rs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/exercises/threads/threads1.rs b/exercises/threads/threads1.rs index f31b317..4bf6aa3 100644 --- a/exercises/threads/threads1.rs +++ b/exercises/threads/threads1.rs @@ -1,10 +1,7 @@ // 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 :) +// The idea is the threads spawned on line 22 are completing jobs while the main thread is +// monitoring progress until 10 jobs are completed. // I AM NOT DONE @@ -18,15 +15,18 @@ struct JobStatus { fn main() { let status = Arc::new(JobStatus { jobs_completed: 0 }); - let status_shared = status.clone(); - thread::spawn(move || { - for _ in 0..10 { - thread::sleep(Duration::from_millis(250)); - status_shared.jobs_completed += 1; - } - }); + for i in 0..10 { + let status_ref = Arc::clone(&status); + thread::spawn(move || { + thread::sleep(Duration::from_millis(250 * i)); + status_ref.jobs_completed += 1; + }); + } while status.jobs_completed < 10 { - println!("waiting... "); + println!("waiting for {} jobs ({} jobs running)... ", + (10 - status.jobs_completed), + (Arc::strong_count(&status) - 1) // subtract one for refrence in _this_ thread + ); thread::sleep(Duration::from_millis(500)); } } From 1cd661997c9773ea0d7fca440a3b5a27012a2905 Mon Sep 17 00:00:00 2001 From: Donald Guy <donaldguy@hey.com> Date: Tue, 4 May 2021 21:25:48 -0400 Subject: [PATCH 2/3] Fix referenced line number --- exercises/threads/threads1.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/threads/threads1.rs b/exercises/threads/threads1.rs index 4bf6aa3..6fa4b19 100644 --- a/exercises/threads/threads1.rs +++ b/exercises/threads/threads1.rs @@ -1,6 +1,6 @@ // threads1.rs // Make this compile! Execute `rustlings hint threads1` for hints :) -// The idea is the threads spawned on line 22 are completing jobs while the main thread is +// The idea is the threads spawned on line 20 are completing jobs while the main thread is // monitoring progress until 10 jobs are completed. // I AM NOT DONE From e696f987cd09e47b40f5b02fa74eed17f54ea8b8 Mon Sep 17 00:00:00 2001 From: Donald Guy <donaldguy@hey.com> Date: Tue, 4 May 2021 21:38:18 -0400 Subject: [PATCH 3/3] Avoid first thread sleeping 0 millis --- exercises/threads/threads1.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/threads/threads1.rs b/exercises/threads/threads1.rs index 6fa4b19..d86cb3c 100644 --- a/exercises/threads/threads1.rs +++ b/exercises/threads/threads1.rs @@ -15,7 +15,7 @@ struct JobStatus { fn main() { let status = Arc::new(JobStatus { jobs_completed: 0 }); - for i in 0..10 { + for i in 1..=10 { let status_ref = Arc::clone(&status); thread::spawn(move || { thread::sleep(Duration::from_millis(250 * i));