mercredi 9 septembre 2020

C++ track thread completion

I have a vector of 25 elements and I want to process 5 elements concurrently in different threads But at any time I don't want to create more than 5 thread i.e in one particular time 5 elements will be processed( 0-4, 5-9, 10-14, 15-19, 20-24)

The code is as follows

void test(vector<string> &input){

vector<std::thread> threads;  
for(int i=0; i<25;++i){
  threads.push_back(std::thread(func, input, i)); //func is a function called by the thread which accepts 2 argument vector<string> and int
  if(threads.size()==5){
    for (auto &th : threads) {
      th.join();
    }
    threads.clear();
  }
}

Here the code will wait till all the 5 thread are completed and then will process for next set

Is there a way to check if any thread gets completed, then a new thread is created but the total count of threads should be <= 5

Aucun commentaire:

Enregistrer un commentaire