diff options
author | Tom Barrett <tombarrett@siu.edu> | 2017-11-24 06:53:29 -0600 |
---|---|---|
committer | Tom Barrett <tombarrett@siu.edu> | 2017-11-24 06:53:29 -0600 |
commit | 6b152d1b551353ae58f38b046596eb8d0418f39b (patch) | |
tree | dd981dac665dbd9cc2f81e86f634f6d61fdbc5a2 /src/list.rs | |
parent | 007bb4def33ac4291f6d5b014ab57f2c760c1e3b (diff) |
-gave threads more time to calculate before joining (would even like to do a non blocking join one day)
Diffstat (limited to 'src/list.rs')
-rw-r--r-- | src/list.rs | 27 |
1 files changed, 12 insertions, 15 deletions
diff --git a/src/list.rs b/src/list.rs index cba5c16..1383394 100644 --- a/src/list.rs +++ b/src/list.rs @@ -1,4 +1,4 @@ -use std::thread::spawn; +use std::thread::{spawn, JoinHandle}; extern crate pancurses; @@ -9,10 +9,10 @@ use location::Location; use character::Character; use constants::Colors; -#[derive(Clone)] pub struct List{ pub men : Vec<Character>, impassable_locations : Vec<Location>, + threads : Vec<JoinHandle<(usize, Option<Vec<Location>>)>> } impl List { @@ -25,6 +25,7 @@ impl List { List { men : men, impassable_locations : impassable_locations, + threads : Vec::new(), } } @@ -35,28 +36,24 @@ impl List { self.men[i].action(free_locations); } - let mut threads = vec!(); + while !self.threads.is_empty() { + let thread = self.threads.pop().unwrap(); + let (i, path) = thread.join().unwrap(); + self.men[i].give_path(path); + } + let impassable = self.get_all_impassable(); for i in 0..self.men.len() { if self.men[i].needs_path() { let man = self.men[i].clone(); let impassable_clone = impassable.clone(); - threads.push(spawn(move || { + self.threads.push(spawn(move || { (i, calculate_path(man, impassable_clone)) })); } } - - for thread in threads { - let data = thread.join(); - match data { - Ok(data) => self.men[data.0].give_path(data.1), - _ => (), - } - } } - pub fn give_destination(&mut self, destination : Location) { for i in 0..self.men.len() { self.men[i].give_destination(destination) @@ -147,9 +144,9 @@ fn calculate_path(man : Character, impassable : Vec<(Location, usize)>) -> Optio result.0.pop(); Some(result.0) } - None => None, + _ => None, } } - None => None, + _ => None, } } |