Week5 Done
This commit is contained in:
parent
67eb2bcdfb
commit
4653d577e4
@ -70,13 +70,30 @@ fn main() {
|
||||
let num_threads = num_cpus::get();
|
||||
println!("Farm starting on {} CPUs", num_threads);
|
||||
let start = Instant::now();
|
||||
let mut thread_handles = Vec::new();
|
||||
// call get_input_numbers() and store a queue of numbers to factor
|
||||
let input_numbers = Arc::new(Mutex::new(get_input_numbers()));
|
||||
|
||||
// TODO: call get_input_numbers() and store a queue of numbers to factor
|
||||
|
||||
// TODO: spawn `num_threads` threads, each of which pops numbers off the queue and calls
|
||||
// spawn `num_threads` threads, each of which pops numbers off the queue and calls
|
||||
// factor_number() until the queue is empty
|
||||
for _ in 0..num_threads {
|
||||
let input_numbers = Arc::clone(&input_numbers);
|
||||
let handle = thread::spawn(move || loop {
|
||||
let mut queue = input_numbers.lock().unwrap();
|
||||
let to_factor = queue.pop_front();
|
||||
drop(queue);
|
||||
if let Some(to_factor) = to_factor {
|
||||
factor_number(to_factor);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
});
|
||||
thread_handles.push(handle);
|
||||
}
|
||||
|
||||
// TODO: join all the threads you created
|
||||
|
||||
// join all the threads you created
|
||||
for hndl in thread_handles {
|
||||
hndl.join().unwrap();
|
||||
}
|
||||
println!("Total execution time: {:?}", start.elapsed());
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user