// this is our main thread spawning work loop.
this is our main thread spawning work loop.
this is our main thread spawning work loop .
fn spawn(&self) -> thread::JoinHandle<()> {
        // Clone our arcs to access our shared variables (these are mutexes)
        let queue_arc = self.queue.clone();
        let cmd_arc = self.thread_command.clone();
        let results_arc = self.results_queue.clone();
        let maxjobs_arc = self.max_jobs.clone();
        let handle = thread::spawn(move || {
            let mut job_vec: Vec<(std::process::Child, Box<dyn commands::Operation + Send>)> = Vec::new();
            loop {
                // process our commands, if any
                match get_command(&cmd_arc) {
                    Some(s) => {
                        match s {
                            ThreadCommands::FINISH => { // Finish execution when our queue is empty
                                println!("disp: we have been told to finish execution");
                                if job_vec.len() == 0 {
                                    break;
                                }
                            }
                            // TODO: This will not kill the started command windows. They'll need to be 
                            // exited manually for the time being
                            ThreadCommands::DIE => { // Kill our children and exit
                                println!("disp: we have been told die");
                                for job in job_vec.iter_mut() {
                                    job.0.kill().expect("disp: error killing job!");
                                }
                                break;
                            }
                        }
                    }
                    //None => println!("disp: no command to process")
                    None => ()
                }
                // First thing we do is see if any jobs have completed
                if !job_vec.is_empty() {
                    // Check over and over again until we have removed all completed children
                    loop {
                        match check_children(&mut job_vec) {
                            Some(s) => {
                                println!("disp: a job was returned: {:?}", s);
                                // Push our results onto our result queue
                                let mut results_queue = results_arc.lock().unwrap();
                                results_queue.push_front(s);
                                drop(results_queue);
                            },
                            None => {
                                //println!("disp: all jobs were running");
                                break;
                            }
                        }
                    }
                }
                // Loop enough times to satisfy our max job limit
                for _ in 0..(open_jobs(&maxjobs_arc, &job_vec)) { // as isize to avoid negative wrap
                    match queue_pop(&queue_arc) {
                        Some(mut x) => {
                            println!("disp: Popping a command off our queue");
                            // Execute the operation's preparation
                            let mut cmd = x.prepare();
                            // Spawn the command
                            let child = cmd.spawn().expect("disp: failed to async batch execute");
                            // push our info onto our vec
                            job_vec.push((child, x));
                        }
                        None => {
                            println!("disp: No more elements to pop");
                        }
                    }
                }
                // Delay each loop execution by 3 seconds
                std::thread::sleep(std::time::Duration::from_secs(3));
            }
        });
        return handle;
    }
fn spawn ( & self ) -> thread :: JoinHandle < ( ) > { // Clone our arcs to access our shared variables (these are mutexes) let queue_arc = self . queue . clone ( ) ; let cmd_arc = self . thread_command . clone ( ) ; let results_arc = self . results_queue . clone ( ) ; let maxjobs_arc = self . max_jobs . clone ( ) ; let handle = thread :: spawn ( move | | { let mut job_vec : Vec < ( std :: process :: Child , Box < dyn commands :: Operation + Send > ) > = Vec :: new ( ) ; loop { // process our commands, if any match get_command ( & cmd_arc ) { Some ( s ) => { match s { ThreadCommands :: FINISH => { // Finish execution when our queue is empty println ! ( " " ) ; if job_vec . len ( ) == 0 { break ; } } // TODO: This will not kill the started command windows. They'll need to be  // exited manually for the time being ThreadCommands :: DIE => { // Kill our children and exit println ! ( " " ) ; for job in job_vec . iter_mut ( ) { job . 0 . kill ( ) . expect ( " " ) ; } break ; } } } //None => println!("disp: no command to process") None => ( ) } // First thing we do is see if any jobs have completed if ! job_vec . is_empty ( ) { // Check over and over again until we have removed all completed children loop { match check_children ( & mut job_vec ) { Some ( s ) => { println ! ( " " s ) ; // Push our results onto our result queue let mut results_queue = results_arc . lock ( ) . unwrap ( ) ; results_queue . push_front ( s ) ; drop ( results_queue ) ; } , None => { //println!("disp: all jobs were running"); break ; } } } } // Loop enough times to satisfy our max job limit for _ in 0 .. ( open_jobs ( & maxjobs_arc , & job_vec ) ) { // as isize to avoid negative wrap match queue_pop ( & queue_arc ) { Some ( mut x ) => { println ! ( " " ) ; // Execute the operation's preparation let mut cmd = x . prepare ( ) ; // Spawn the command let child = cmd . spawn ( ) . expect ( " " ) ; // push our info onto our vec job_vec . push ( ( child , x ) ) ; } None => { println ! ( " " ) ; } } } // Delay each loop execution by 3 seconds std :: thread :: sleep ( std :: time :: Duration :: from_secs ( 3 ) ) ; } } ) ; return handle ; }
this is our main thread spawning work loop.
this is our main thread spawning work loop .
&self