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中)显然没有理由为什么程序挂起.当我点击继续时,程序会愉快地继续并打印更多东西,直到它再次被阻止.
你知道这里发生了什么吗?
更多信息:
此问题是由 2014 年春季 Windows 8.1 更新中引入的操作系统调度程序错误引起的。此问题的修补程序已于 2015 年 5 月发布,可从https://support.microsoft.com/en-us/kb/3036169获取。