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 thestopThread
going 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:
- c# – the calling thread must be STA, because ma…
- Error: [$compile:nonassign] Expression used with directive ‘uibTab’ is non-assignable
- [Solved]Swift error: Method does not override any method from its superclass
- Python uses decorator and reports error typeerror: ‘nonetype’ object is not callable ‘
- PostgreSQL“ DESCRIBE TABLE” – PostgreSQL “DESCRIBE TABLE”
- Solve the problem of destroying windows caused by multiple attchs of control classes in MFC. CWnd * pWnd = CWnd:: fromhandlepermanent (hWnd); ASSERT(pWnd != NULL); Assertion failure problem
- [Solved] VS Compilation errors: #error: Building MFC application with /MD[d] (CRT dll version) requires MFC shared dll ver…
- Concurrent notes (4): Notes on the use of wait / notify / notifyAll method
- invalid conversion from ‘void* (*)()’ to ‘void* (*)(void*)’
- The meaning of eagain and resource temporarily unavailable in Linux