Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 相当于C++;11螺纹_C++_Multithreading_Winapi_C++11 - Fatal编程技术网

C++ 相当于C++;11螺纹

C++ 相当于C++;11螺纹,c++,multithreading,winapi,c++11,C++,Multithreading,Winapi,C++11,我正在重写使用WinAPI进行线程处理的代码,以使用新的标准线程库 我想知道,在C++11中,注意到互斥被放弃或丢失的等效方式是什么 下面的代码必须将初始化过程“外包”给创建的线程,但在完成并知道初始化结果之前不应返回 bool Foo::Example() { m_thread = std::thread(&Foo::ThreadProc, this); // wait for event before waiting for mutex WaitForSin

我正在重写使用WinAPI进行线程处理的代码,以使用新的标准线程库

我想知道,在C++11中,注意到互斥被放弃或丢失的等效方式是什么

下面的代码必须将初始化过程“外包”给创建的线程,但在完成并知道初始化结果之前不应返回

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
}
用什么来代替放弃的支票?我在std::mutex中没有找到任何东西。它甚至说
如果互斥锁在被销毁之前没有解锁,即某些线程仍然拥有互斥锁,那么行为是未定义的。
没有等价物吗?std线程库中是否有与此类似的内容


我还采纳了改进代码的建议。对于这样一个简单的任务来说,同步似乎太多了。

没有等价物。您可以使用RAII解锁互斥锁,避免放弃互斥锁,这样就不需要测试互斥锁了

您可以使用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
}
boolfoo::Example()
{
std::promise p;
自动恢复=获取未来();
m_thread=std::thread(&Foo::ThreadProc,this,std::ref(p));
return res.get();
}
void Foo::ThreadProc(标准::promise&p)
{
//…初始化(需要在此线程中)
如果(初始化失败)
{
p、 设置_值(false);//失败。
返回;
}
p、 设置_值(真);
//重要提示:p现在是一个悬而未决的参考!
//…做这项工作
}
主线程将阻塞,直到承诺得到满足,然后根据初始化是否有效返回true或false


< >可以避免悬空引用,使其成为<代码> thREADPRC(STD::POUNTY P),然后传递为“代码> STD::移动(p)< /C> >代替 STD::REF(p)< /Cord>。但是我认为VisualC++中的 STD::线程< /C> >支持只移动类型的完美转发。

标准中没有等价物。(从您引用的措辞中应该非常清楚)。也就是说,您不能以可移植的方式实现此功能。可能某个特定的实现以某种方式提供了对此功能的支持,但最终的程序将处于“未定义的行为”登陆后,一切都会发生。使用WAIT_是一个严重的错误,应该始终保留它来表示代码中存在严重的线程错误。如果使用Microsoft std::mutex实现,则会得到完全不同的结果,它不使用OS mutex。它构建在ConcRT库之上。Which从头开始重新实现同步原语。当删除std::mutex对象并仍然持有锁时,您将得到一个断言。只有在调试版本中,发布版本中出现无声故障。谢谢。这并不能真正解决问题。我需要等待初始化,但同时发出成功或失败的信号。@typ1232,我添加了一个比依赖废弃设备简单得多的替代方案mutexes@typ1232:如果您需要发出成功或失败的信号,只需使用一个额外的变量即可。您不应使用放弃与未放弃来传达程序状态。
WAIT\u audded
状态为“哦,糟了,有些事情出了可怕的错误,最好现在就尽快退出”标志,而不是正常程序运行期间应该发生的事情。