简体   繁体   English

等效于C ++ 11线程中的WAIT_ABANDONED

[英]Equivalent of WAIT_ABANDONED in C++11 threading

I'm rewriting code that uses WinAPI for threading to use the new standard threading libraries. 我正在重写使用WinAPI进行线程化以使用新标准线程库的代码。

I was wondering what would be the equivalent way in C++11 to notice that a mutex was abandoned or lost. 我想知道在C ++ 11中注意到互斥体被遗弃或丢失的等效方式。

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? 什么是WAIT_ABANDONED支票的替代品? I didn't find anything in std::mutex. 我在std :: mutex中找不到任何东西。 It even says The behavior is undefined if the mutex is not unlocked before being destroyed, ie some thread still owns it. 它甚至说The behavior is undefined if the mutex is not unlocked before being destroyed, ie some thread still owns it. Is there no equivalent? 没有等价物吗? Anything in std threading library that comes close to this? std线程库中的任何东西都接近这个?

I also take suggestions in improoving the code. 我还提出了改进代码的建议。 It seems to be too much synchronization for such a simple task. 这样一个简单的任务似乎太过同步了。

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. 您可以使用RAII来解锁互斥锁并避免首先放弃互斥锁,然后您就不需要能够对其进行测试。

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: 您可以使用future而不是等待事件并使用互斥锁,这使得它比容易出错的显式同步更简单:

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. 主线程将阻塞,直到履行完成,然后根据初始化是否有效返回true或false。

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. 您可以通过使其成为ThreadProc(std::promise<bool> p)来避免悬空引用,然后将其作为std::move(p)而不是std::ref(p)传递,但我不认为std::thread Visual C ++中的std::thread支持完全转发仅移动类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM