I use Visual Studio 2005 to develop my application, which is running on Windows Server 2008 R2 standard. We met an issue, it seems that it was due to thread scheduling.
We have two threads name ThreadA and ThreadB in our codes, the ThreadA is waiting for an auto reset event, the code is like below,
----------------------------------------------------------------------------------------
while(true)
{
//...
DWORD status = WaitForSingleObject(requestStartedEvent, INFINITE);
if (status == WAIT_OBJECT_0)
{
Log("Received a Start Event");
}
else
{
Log("Received a non-zero start event - " + itoa(status));
}
_error = DoOperation();
//set requestCompleteEvent event
//...
}
------------------------------------------------------------------------------
The ThreadB will set the event each time, and wait for a complete event.
+++++++++++++++++++++++++++++++++++++++++++++
while(true)
{
if (SetEvent(requestStartedEvent))
{
Log("set requestStartedEvent");
}
else
{
Log("Set requestStartedEvent failed, error code = " + itoa(GetLastError()));
return;
}
DWORD status = WaitForSingleObject(requestCompleteEvent, 5*60*1000);
//if time out, generate a dump.
//other work
}
++++++++++++++++++++++++++++++++++++++++++++++++
The application has run several hours, the behavior is correct before the issue occurred, that means, each time couple of logs are found in log file,
Timestamp-ThreadB "set requestStartedEvent"
Timestamp-ThreadA "Received a Start Event"
But the last log we only found the ThreadB log. We aslo got the dump, from which the ThreadA was still waiting for the requestStartedEvent.
The question is why the ThreadA was not scheduled after the requestStartedEvent event was signaled.
Note: Only one application is running in the system, no other thread is waiting for the event too.