2018-02-22 06:09:53 +00:00
|
|
|
// threads1.rs
|
2022-07-15 09:59:53 +00:00
|
|
|
// Execute `rustlings hint threads1` or use the `hint` watch subcommand for a hint.
|
2021-12-23 14:19:39 +00:00
|
|
|
// This program should wait until all the spawned threads have finished before exiting.
|
2015-09-29 18:39:25 +00:00
|
|
|
|
2019-11-11 12:38:24 +00:00
|
|
|
// I AM NOT DONE
|
|
|
|
|
2015-09-29 18:39:25 +00:00
|
|
|
use std::thread;
|
2016-02-08 21:13:45 +00:00
|
|
|
use std::time::Duration;
|
2015-09-29 18:39:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
fn main() {
|
2021-12-23 14:19:39 +00:00
|
|
|
|
|
|
|
let mut handles = vec![];
|
|
|
|
for i in 0..10 {
|
|
|
|
thread::spawn(move || {
|
2016-02-08 21:13:45 +00:00
|
|
|
thread::sleep(Duration::from_millis(250));
|
2021-12-23 14:19:39 +00:00
|
|
|
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!");
|
2015-09-29 18:39:25 +00:00
|
|
|
}
|
2021-12-23 14:19:39 +00:00
|
|
|
|
2015-09-29 18:39:25 +00:00
|
|
|
}
|