From 6b152d1b551353ae58f38b046596eb8d0418f39b Mon Sep 17 00:00:00 2001 From: Tom Barrett Date: Fri, 24 Nov 2017 06:53:29 -0600 Subject: -gave threads more time to calculate before joining (would even like to do a non blocking join one day) --- src/list.rs | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) (limited to 'src/list.rs') 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, impassable_locations : Vec, + threads : Vec>)>> } 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, } } -- cgit v1.2.3