I have a workflow that is constantly running, created and managed using std::thread. At the top level of my workflow, I have a try / catch block with a while loop inside it. If an exception leaks to the upper level of the stream, I will catch it and save it in std::exception_ptr, which is a member of the class that also owns the non-static stream function:
std::exception_ptr m_threadException;
void MyClass::MyThreadFunction()
{
try {
while (true) {
}
}
catch (std::exception const& e) {
m_threadException = std::current_exception();
}
}
As soon as the thread dies due to such an exception, my class (which is also used mainly in the main thread) does not know yet. My plan was to add flow control points to the beginning of all the basic functions of the class, for example:
void MyClass::SomethingMainThreadCalls()
{
if (m_threadException) {
std::rethrow_exception(m_threadException);
m_threadException = nullptr;
}
}
, , , , exception_ptr ( SomethingMainThreadCalls()) . ( ++ 11) , ( ), .
std::atomic , ? :
std::atomic<std::exception_ptr> m_threadException;
- ? , . .