user2630015
user2630015

Reputation:

Error while exiting application with another thread running

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

Answers (2)

Stephane D.
Stephane D.

Reputation: 142

Replace

while(running){

}

by

loop2.join()

It will wait until the thread is terminated.

Upvotes: 1

Lightness Races in Orbit
Lightness Races in Orbit

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

Related Questions