Run a same process simultaneously

Hello
I have a problem with rust multi-threading
and I hope somebody could help me
with that

I have a single function that I want to call it simultaneously with specefic number of threads(ex:30)

but I want to keep threads up to given number until the whole process finishes
when a thread finishes its job ,
anotherone spawn

but my program now is waiting for all those threads to be finished
and then spawn another 30 threads
here is my code
I would be really thankful if somone could help me

 let (tx,rx) = mpsc::sync_channel::<JoinHandle<()>>(thread);
  
    loop{
         if ips.len()==0{
            break;
         }

        match rx.recv(){
           Ok(handle)=> {
            handle.join().unwrap();
            bar.inc(1);
            each_thread_job(&ips.pop().clone().unwrap(), config_file.clone(),curl_installed.clone(),tx.clone());
           },
           Err(e)=>eprint!("{:?}",e)

        }


            
        }

Sounds like you're looking for a thread pool. There are likely a number of implementations around that you could choose from to fit your scenario. Perhaps threadpool - Rust (docs.rs)

Rayon is probably the most popular crate for this type of problem, it has very nice parallel iterators for interacting with the thread pool. But the thread pool can be used directly as well.

1 Like

thank you so much for your reply
i'll try it and say the result in here

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.