We have answer of your question!

100% solved queries, no empty question

Question: Equivalent of WAIT_ABANDONED in C++11 threading


0

I'm rewriting code that uses WinAPI for threading to use the new standard threading libraries.

I was wondering what would be the equivalent way in C++11 to notice that a mutex was abandoned or lost.

The following code has to "outsource" an initialization procedure to the created thread, but should not return until it's done and knows the result of initialization.

bool Foo::Example()
{
    m_thread = std::thread(&Foo::ThreadProc, this);

    // wait for event before waiting for mutex
    WaitForSingleObject(m_hEvent, INFINITE);
    ResetEvent(m_hEvent);

    // the thread aquired the mutex. now wait until it is released or abandoned
    DWORD ret = WaitForSingleObject(m_hMutex, INFINITE);
    ReleaseMutex(m_hMutex);

    // check the result
    if (ret == WAIT_ABANDONED)
        return false;
    return true;
}
void Foo::ThreadProc()
{
    // aquire mutex and signal that it's done
    WaitForSingleObject(m_hMutex, INFINITE);
    SetEvent(m_hEvent);

    // ... initialization (required to be in this thread)

    if (initializationfailure)
        return; // failure. mutex is abandoned

    // success. mutex is unlocked
    ReleaseMutex(m_hMutex);

    // ... do the work
}

What would be a replacement for the WAIT_ABANDONED check? I didn't find anything in std::mutex. It even says The behavior is undefined if the mutex is not unlocked before being destroyed, i.e. some thread still owns it. Is there no equivalent? Anything in std threading library that comes close to this?

I also take suggestions in improoving the code. It seems to be too much synchronization for such a simple task.

Question author Typ1232 | Source

Answer


1


There is no equivalent. You could use RAII to unlock the mutex and avoid abandoning it in the first place, then you don't need to be able to test for it.

You could use a future instead of waiting on an event and using a mutex, which makes it much simpler than error-prone explicit synchronisation:

bool Foo::Example()
{
    std::promise<bool> p;
    auto res = p.get_future();
    m_thread = std::thread(&Foo::ThreadProc, this, std::ref(p));
    return res.get();
}
void Foo::ThreadProc(std::promise<bool>& p)
{
    // ... initialization (required to be in this thread)

    if (initializationfailure)
    {
        p.set_value(false); // failure.
        return;
    }

    p.set_value(true);

    // IMPORTANT: p is a dangling reference now!

    // ... do the work
}

The main thread will block until the promise is fulfilled, and then returns true or false depending on whether the initialization worked.

You could avoid the dangling reference by making it ThreadProc(std::promise<bool> p) and then pass it as std::move(p) instead of std::ref(p) but I don't think the std::thread in Visual C++ supports perfect forwarding of move-only types.

Answer author Jonathan-wakely

Tickanswer.com is providing the only single recommended solution of the question Equivalent of WAIT_ABANDONED in C++11 threading under the categories i.e c++ , multithreading , winapi , c++11 , . Our team of experts filter the best solution for you.

Related Search Queries:


You may also add your answer

Thanks for contributing an answer to Tick Answer!