threads and iter5

This commit is contained in:
0xChasingFire 2021-05-27 20:34:46 +08:00
parent 55c2e4ed4e
commit dd131cb487
2 changed files with 7 additions and 7 deletions
exercises
standard_library_types
threads

View file

@ -11,7 +11,6 @@
//
// Make the code compile and the tests pass.
// I AM NOT DONE
use std::collections::HashMap;
@ -35,6 +34,8 @@ fn count_for(map: &HashMap<String, Progress>, value: Progress) -> usize {
fn count_iterator(map: &HashMap<String, Progress>, value: Progress) -> usize {
// map is a hashmap with String keys and Progress values.
// map = { "variables1": Complete, "from_str": None, ... }
// map.keys().len()
count_for(map, value)
}
fn count_collection_for(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
@ -53,6 +54,7 @@ fn count_collection_iterator(collection: &[HashMap<String, Progress>], value: Pr
// collection is a slice of hashmaps.
// collection = [{ "variables1": Complete, "from_str": None, ... },
// { "variables2": Complete, ... }, ... ]
count_collection_for(collection, value)
}
#[cfg(test)]

View file

@ -6,9 +6,7 @@
// of "waiting..." and the program ends without timing out when running,
// you've got it :)
// I AM NOT DONE
use std::sync::Arc;
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;
@ -17,15 +15,15 @@ struct JobStatus {
}
fn main() {
let status = Arc::new(JobStatus { jobs_completed: 0 });
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));
status_shared.jobs_completed += 1;
status_shared.lock().unwrap().jobs_completed += 1;
}
});
while status.jobs_completed < 10 {
while status.lock().unwrap().jobs_completed < 10 {
println!("waiting... ");
thread::sleep(Duration::from_millis(500));
}