From ed9701981d31a6532bde6f365d5eeb713c04b150 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Tue, 30 Jul 2024 12:24:23 -0700 Subject: [PATCH] limit parallel threads by waiting for the first thread to complete --- src/main.rs | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1402ed6..371e783 100644 --- a/src/main.rs +++ b/src/main.rs @@ -654,12 +654,27 @@ impl std::fmt::Display for ConvertError{ } impl std::error::Error for ConvertError{} +type MapThread=std::thread::JoinHandle>; + fn convert_to_snf(pathlist:Vec,output_folder:PathBuf)->AResult<()>{ + let n_paths=pathlist.len(); let start = std::time::Instant::now(); - let mut threads:Vec>>=Vec::new(); + let mut threads:std::collections::VecDeque=std::collections::VecDeque::new(); + let mut i=0; + let mut join_thread=|thread:MapThread|{ + i+=1; + if let Err(e)=thread.join(){ + println!("thread error: {:?}",e); + }else{ + println!("{}/{}",i,n_paths); + } + }; for path in pathlist{ + if 32<=threads.len(){ + join_thread(threads.pop_front().unwrap()); + } let output_folder=output_folder.clone(); - threads.push(std::thread::spawn(move ||{ + threads.push_back(std::thread::spawn(move ||{ let dom=strafesnet_rbx_loader::read( std::fs::File::open(path.as_path()) .map_err(ConvertError::IO)? @@ -703,15 +718,8 @@ fn convert_to_snf(pathlist:Vec,output_folder:PathBuf)->AResu })); } - let mut i=0; - let n_threads=threads.len(); for thread in threads{ - i+=1; - if let Err(e)=thread.join(){ - println!("thread error: {:?}",e); - }else{ - println!("{}/{}",i,n_threads); - } + join_thread(thread); } println!("{:?}", start.elapsed()); Ok(())