Multithreading program in c++, the problem is: i have a vector of threads, where i push_back every thread i create, and i want to make a kind of "steal-work scheduling", in such a way that when a thread is joined, so i assume his job is terminated, i delete it from the vector of threads and i push_back a new thread assining him another task. The problem is that when i try to erase threads after their join, i got "terminate()" so i assume that i'm erasing threads that are still working, even if my goal was to erase only already joined threads. Where am i wrong?
vector<thread> tids;
const int nw = atoi(argv[1]); //number of worker
//Just erase the thread that has been joined
void erasePosition(thread &t, vector<thread> &threads){
for(int i=0;i<threads.size();i++){
if(t.get_id()==threads[i].get_id()) {
threads.erase(threads.begin()+i);
}
}
}
//Assign job to threads
for (int i = 0; i < nw; i++) {
tids.push_back(thread(solveSubTree, ref(works[i]), ref(nw)));
}
//
for (thread &t : tids) {
t.join();
erasePosition(t,tids);
cout << " tids.size() = " << tids.size() << endl;
}
I see that the program execute correctly until i erase the middle thread, for example if i execute the program with 4 threads, it call "terminate()" when i try to erase the 2nd thread, while if i use 8 threads, it terminate when i try to erase the 4th thread, maybe it will be useful to you.
The exact error is this: terminate called after throwing an instance of 'std::system_error' what(): Invalid argument