summaryrefslogtreecommitdiff
path: root/exercises/threads/threads1.rs
diff options
context:
space:
mode:
authormokou <mokou@fastmail.com>2022-07-15 14:31:49 +0200
committermokou <mokou@fastmail.com>2022-07-15 14:31:49 +0200
commitc791cf4232fbfc313279b19b483c1adbca1c6862 (patch)
tree655ad6c9d33dab11dfd70f28d0ec29d03749a70b /exercises/threads/threads1.rs
parentf1c4caa37fe5027d121aec6433dee85433d9329d (diff)
parentc265b681b188ea21b3f8585e65ea363fc02c4b50 (diff)
Merge branch '5.0-dev'
Diffstat (limited to 'exercises/threads/threads1.rs')
-rw-r--r--exercises/threads/threads1.rs39
1 files changed, 19 insertions, 20 deletions
diff --git a/exercises/threads/threads1.rs b/exercises/threads/threads1.rs
index f31b317..e59f4ce 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 :)
+// 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
-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!");
}
+
}