Some prerequisites: I'm trying to track down an error that causes me serious headaches. After many dead ends (see this question ) I finally ended up with this code:
#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; }
When I run this code inside the VS 2013 debugger, the program crashes with an output similar to this:
....................................................................................................0 ....................................................................................................1 ....................................................................................................2 ...........................
Oddly enough, if I pause the debugger and check what happens, one of the threads is inside AcquireSRWLockExclusive (in NtWaitForAlertByThreadId), apparently there are no reasons why the program hangs. When I click resume, the program happily continues and prints a few more things until it is locked again.
Do you have any idea what is going on here?
Additional Information:
- As far as I can tell, this error only exists in Windows 8.1.
- I tried VS2013.4 and VS2015 RC.
- I could play it on two different computers running Windows 8.1.
- One of the machines was formatted: RAM, CPU and Disk were tested (I was thinking about a malfunction, because at first I could only see the error on this particular machine)
- I could never play it in Windows 7.
- It may be useful to change the code between comments to observe the error. When I added a microsecond sleep, I could finally reproduce the error on another computer.
- With VS2015 RC, I could reproduce the same behavior with a simple std :: mutex. On VS2013, however, SRWLOCK seems to be mandatory for observing the error.
c ++ c ++ 11 visual-c ++ visual-studio
Arnaud
source share