mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-11 05:09:19 +00:00
38 lines
1.1 KiB
Rust
38 lines
1.1 KiB
Rust
// 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 :)
|
|
|
|
use std::sync::{Arc, Mutex};
|
|
use std::thread;
|
|
use std::time::Duration;
|
|
|
|
struct JobStatus {
|
|
jobs_completed: u32,
|
|
}
|
|
|
|
fn main() {
|
|
let status = Arc::new(Mutex::new(JobStatus { jobs_completed: 0 }));
|
|
let status_shared = status.clone();
|
|
thread::spawn(move || {
|
|
for _ in 0..10 {
|
|
thread::sleep(Duration::from_millis(250));
|
|
let mut data = status_shared.lock().unwrap();
|
|
data.jobs_completed += 1;
|
|
}
|
|
});
|
|
loop {
|
|
{
|
|
let data = status.lock().unwrap();
|
|
if data.jobs_completed >= 10 {
|
|
break;
|
|
}
|
|
}
|
|
println!("waiting... ");
|
|
thread::sleep(Duration::from_millis(500));
|
|
}
|
|
}
|