Reputation:
I've got a code like this:
int main(){
thread loop2(loop2);
while(running){
}
}
void loop2(){
while(something){
}
}
When running
turns to false
, the program should exit. But I'm getting an error from VS: -abort() has been called.
I know that this happens because the second thread is still running. I tried to terminate the thread with ~thread()
, but it didn't work.
Upvotes: 1
Views: 83
Reputation: 142
Replace
while(running){
}
by
loop2.join()
It will wait until the thread is terminated.
Upvotes: 1
Reputation: 385395
You don't just arbitrarily invoke some object's destructor whenever you feel like it!
Instead, you should interrupt and then join the thread.
Upvotes: 0