程序在Visual Studio调试器中挂起

Arn*_*aud 7 c++ visual-studio visual-c++ c++11

一些背景:我试图追踪一个导致我头痛的错误.经过许多死胡同(见这个问题)后我终于得到了这段代码:

#include <thread>
#include <vector>
#include <iosfwd>
#include <sstream>
#include <string>
#include <windows.h>

int main()
{
    SRWLOCK srwl;
    InitializeSRWLock(&srwl);
    for(size_t i=0;i<1000;++i)
    {
        std::vector<std::thread>threads;
        for(size_t j=0;j<100;++j)
        {
            OutputDebugString(".");
            threads.emplace_back([&](){
                AcquireSRWLockExclusive(&srwl);
                //Code below modifies the probability to see the bug.
                std::this_thread::sleep_for(std::chrono::microseconds(1));
                std::wstringstream wss;
                wss<<std::this_thread::get_id();
                wss.str();
                //Code above modifies the probability to see the bug.
                ReleaseSRWLockExclusive(&srwl);});
        }
        for(auto&t:threads){t.join();}
        OutputDebugString((std::to_string(i)+"\n").data());
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我在VS 2013调试器中运行此代码时,程序会挂起如下输出:

....................................................................................................0
....................................................................................................1
....................................................................................................2
...........................
Run Code Online (Sandbox Code Playgroud)

奇怪的是,如果我暂停调试器并检查发生了什么,其中一个线程在AcquireSRWLockExclusive内(在NtWaitForAlertByThreadId中)显然没有理由为什么程序挂起.当我点击继续时,程序会愉快地继续并打印更多东西,直到它再次被阻止.

你知道这里发生了什么吗?

更多信息:

  • 据我所知,这个bug只存在于Windows 8.1上.
  • 我试过VS2013.4和VS2015 RC.
  • 我可以在Windows 8.1下的两台不同的计算机上重现它.
  • 其中一台机器被格式化,RAM,CPU和磁盘测试(我想到了一个故障,因为起初我只能观察到这台特定机器上的错误)
  • 我永远无法在Windows 7上重现它.
  • 修改注释之间的代码以观察错误可能很有用.当我添加微秒睡眠时,我终于可以在另一台计算机上重现该错误.
  • 使用VS2015 RC,我可以使用简单的std :: mutex重现相同的行为.在VS2013上,SRWLOCK似乎必须遵守该bug.

Ant*_*sha 4

此问题是由 2014 年春季 Windows 8.1 更新中引入的操作系统调度程序错误引起的。此问题的修补程序已于 2015 年 5 月发布,可从https://support.microsoft.com/en-us/kb/3036169获取。