Is std :: exception_ptr a thread safe?

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:

// In class header (inside class definition)
std::exception_ptr m_threadException;

// In class CPP file
void MyClass::MyThreadFunction()
{
    try {
        while (true) {
            // Do thread stuff
        }
    }
    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; // Somehow reset it back to null; not sure if this will work
    }

    // Do normal function stuff
}

, , , , exception_ptr ( SomethingMainThreadCalls()) . ( ++ 11) , ( ), .

std::atomic , ? :

std::atomic<std::exception_ptr> m_threadException;

- ? , . .

+4
2

exception_ptr . , : , .

atomic<bool> atomic<exception_ptr>, , exception_ptr. , :

  • m_threadException
  • m_threadException
  • / / .
  • m_threadException .
+3

, std::exception_ptr, std::exception_ptr .

, .

+2

Source: https://habr.com/ru/post/1664591/


All Articles