I'm keeping track of some threads in C++ by adding them to a vector.
I want to periodically loop through the vector, check to see if they've exited (they've become joinable), and if so, remove them from the vector.
My code looks like:
void do_stuff(){
for(int i=0; i<30; i++){
cout << "Doing Stuff.\n";
sleep(10);
}
}
void main_loop(){
std::vector<std::thread> client_threads;
while(1){
if(stuff_needs_to_be_done()){
client_threads.push_back(std::thread(&do_stuff));
}
// Cleanup threads.
auto itr = std::begin(client_threads);
while (itr != std::end(client_threads)) {
if ((*itr).joinable()){
itr = client_threads.erase(itr);
}else{
++itr;
}
}
}
}
Upon stepping through the code, I find when it gets to my thread cleanup section, my process exits with:
terminate called without an active exception
Aborted (core dumped)
I'm not sure what this means exactly, other than I'm probably not cleaning up my threads correctly. What am I doing wrong?