limit parallel threads by waiting for the first thread to complete

This commit is contained in:
Quaternions 2024-07-30 12:24:23 -07:00
parent 60e0197344
commit ed9701981d

View File

@ -654,12 +654,27 @@ impl std::fmt::Display for ConvertError{
}
impl std::error::Error for ConvertError{}
type MapThread=std::thread::JoinHandle<Result<(),ConvertError>>;
fn convert_to_snf(pathlist:Vec<std::path::PathBuf>,output_folder:PathBuf)->AResult<()>{
let n_paths=pathlist.len();
let start = std::time::Instant::now();
let mut threads:Vec<std::thread::JoinHandle<Result<(),ConvertError>>>=Vec::new();
let mut threads:std::collections::VecDeque<MapThread>=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<std::path::PathBuf>,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(())