This repository has been archived on 2023-03-24. You can view files and clone it, but cannot push or open issues or pull requests.
rustlings-exercises-completed/exercises/threads/threads1.rs
jaystile b4f52cb937 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
2022-07-15 11:58:26 +02:00

32 lines
751 B
Rust

// threads1.rs
// 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::thread;
use std::time::Duration;
fn main() {
let mut handles = vec![];
for i in 0..10 {
thread::spawn(move || {
thread::sleep(Duration::from_millis(250));
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!");
}
}