2018-02-22 06:09:53 +00:00
|
|
|
// threads1.rs
|
2019-11-11 15:51:38 +00:00
|
|
|
// Make this compile! Execute `rustlings hint threads1` for hints :)
|
2019-11-14 07:20:38 +00:00
|
|
|
// The idea is the thread spawned on line 21 is completing jobs while the main thread is
|
2020-12-08 09:08:25 +00:00
|
|
|
// 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
|
2019-11-11 15:51:38 +00:00
|
|
|
// of "waiting..." and the program ends without timing out when running,
|
2015-09-29 19:39:25 +01:00
|
|
|
// you've got it :)
|
|
|
|
|
2019-11-11 12:38:24 +00:00
|
|
|
// I AM NOT DONE
|
|
|
|
|
2015-09-29 19:39:25 +01:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::thread;
|
2016-02-08 21:13:45 +00:00
|
|
|
use std::time::Duration;
|
2015-09-29 19:39:25 +01:00
|
|
|
|
|
|
|
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 {
|
2016-02-08 21:13:45 +00:00
|
|
|
thread::sleep(Duration::from_millis(250));
|
2015-09-29 19:39:25 +01:00
|
|
|
status_shared.jobs_completed += 1;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
while status.jobs_completed < 10 {
|
|
|
|
println!("waiting... ");
|
2016-02-08 21:13:45 +00:00
|
|
|
thread::sleep(Duration::from_millis(500));
|
2015-09-29 19:39:25 +01:00
|
|
|
}
|
|
|
|
}
|