For many CEvent object, WaitForMultipleObjects always returns 0, rather than 1...
Test several times, the system is 2016 + vs2017 server2. Strange...
The main ()
{
//TODO: add the control notification handler code
M_hEvent [0]=CreateEvent (NULL, FALSE, FALSE, NULL);
M_hEvent [1]=CreateEvent (NULL, FALSE, FALSE, NULL);
SetEvent (m_hEvent [0]);
SetEvent (m_hEvent [1]);
CreateThread (NULL, 0, MyThreadProcWaitAll, this, 0, NULL);
}
DWORD WINAPI MyThreadProcWaitAll (LPVOID lpParam)
{
While (TRUE)
{//every 500 milliseconds
Int nIndex=WaitForMultipleObjects (2, m_hEvent, TRUE, 5000);
Spdlog: : info (" waiting for the two events, nIndex={} ", nIndex);
If (nIndex==WAIT_OBJECT_0 + 1)
{
Spdlog: : info (" the second incident, nIndex={} ", nIndex);
break;
}
Else if (nIndex==WAIT_OBJECT_0)//the first incident
{
Spdlog: : info (" the first event, nIndex={} ", nIndex);
break;
}
Else if (nIndex==WAIT_TIMEOUT)//timeout 500 milliseconds
{
break;
}
}
Spdlog: : info (" thread ends... ");
return 0;
}
CodePudding user response:
MSDNMSDN says the
The Return code/value
WAIT_OBJECT_0 to (WAIT_OBJECT_0 + nCount - 1)
The Description
If bWaitAll is TRUE, return a value in this range are that the state of all specified objects is signaled.
If bWaitAll is FALSE, the return value minus WAIT_OBJECT_0 are the lpHandles array index of the object that satisfied the wait. If more than one object became signaled during the call, this is the array index of the signaled the object have the smallest index value of all the signaled objects.
True, now look at the return value is zero.
False, is zero, return the smallest is 0
CodePudding user response:
Multiple kernel object is triggered, WaitForMultipleObjects choose one of the serial number of minimum return,And WaitForMultipleObjects it will only change make it returns the kernel object's state,
Here creates a problem, if the serial number the youngest objects often is triggered, the serial number is greater than its kernel object will not be out of the opportunity,
In order to solve this problem, can use double WaitForMultipleObjects detection mechanism to implement,
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Copyright statement: this article to CSDN blogger (tetrode) of the original articles, follow BY CC 4.0 - SA the copyright agreement, reproduced and this statement, please attach the original source link
The original link: https://blog.csdn.net/yangxingbo0311/article/details/7323762