jeudi 29 octobre 2015

Multithread in a Loop

I am presenting an "minimalist example" to try to understand how can i make multi-threading in C++11. So given this example:

std::thread t[total_threads];
for(int i = 0; i<total_threads; i++){
   t[i]= std::thread(&thread_function_1, random1, parameters1);
   t[i].join();
   t[i]= std::thread(&thread_function_2, random2, parameters2);
   t[i].join();
   t[i]= std::thread(&thread_function_2, random3, parameters3);
   t[i].join();
}

Obviously if I try to debugg this code I will have abortion errors. Since is not possible to use the same thread twice (I guess). One possible solution is to create an array with "millions" of threads to make it happen (and iterate between them). But I guess it is a bad solution. How can I make this efficiently, since I can not use the same thread twice?

Aucun commentaire:

Enregistrer un commentaire