[Solved] std::thread “terminate called without an active exception”

Recently, when using STD:: thread, we encountered such a problem:

std::thread t(func);

If you do not use the call t.join() will encounter “terminate called whithout an active exception”, but in the use of boost:thread did not encounter this problem, google it and find the answer:

The trouble you are encountering is a result of thestopThreadgoing out of scope on the stack. The C++ standard has the following to say about this:

30.3.1.3thread destructor [thread.thread.destr]

~thread();

Ifjoinable()then terminate(), otherwise no effects. [Note:Either implicitly detaching or joining ajoinable()thread in its destructor could result in difficult to debug correctness (for detach) or performance (for join) bugs encountered only when an exception is raised. Thus the programmer must ensure that the destructor is never executed while the thread is still joinable. —end note]

What this means is that you should not let threads go out of scope without first calling eitherjoin()ordetatch().

The way you describe it, you want the thread to go out of scope without joining so it will continue to run as your application runs. That requires a call todetach(). From there, I can only offer a little wisdom…

The idea is that if you don’t call join() before ~thread(); you will have problems debugging, and if you don’t want to call join() and wait for the thread to finish you can call detach(). This way you will not encounter “terminate called whithout an active exception”

as follows:

{
    std::thread t(func);
    t.detach();
}

Similar Posts: