針對多CEvent物件,WaitForMultipleObjects一直回傳0,而不是1.......
試驗了好多次,系統是2016server2 + vs2017. 奇怪了.............
main()
{
// TODO: 在此添加控制元件通知處理程式代碼
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)
{ //每次等500毫秒
int nIndex = WaitForMultipleObjects(2, m_hEvent, TRUE, 5000);
spdlog::info("等待兩個事件,nIndex = {} ", nIndex);
if (nIndex == WAIT_OBJECT_0 + 1)
{
spdlog::info("第二個事件發生, nIndex = {} ", nIndex);
break;
}
else if (nIndex == WAIT_OBJECT_0) //第一個事件發生
{
spdlog::info("第一個事件 , nIndex = {} ", nIndex);
break;
}
else if (nIndex == WAIT_TIMEOUT) //超時500毫秒
{
break;
}
}
spdlog::info("執行緒結束....");
return 0;
}
uj5u.com熱心網友回復:
msdnMSDN說這個
Return code/value
WAIT_OBJECT_0 to (WAIT_OBJECT_0 + nCount– 1)
Description
If bWaitAll is TRUE, a return value in this range indicates that the state of all specified objects is signaled.
If bWaitAll is FALSE, the return value minus WAIT_OBJECT_0 indicates 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 object with the smallest index value of all the signaled objects.
true的時候,現在看return value是0 ;
false的時候,也是0,回傳最小的也是0
uj5u.com熱心網友回復:
多個內核物件被觸發時,WaitForMultipleObjects選擇其中序號最小的回傳。而WaitForMultipleObjects它只會改變使它回傳的那個內核物件的狀態。
這兒又會產生一個問題,如果序號最小的那個物件頻繁被觸發,那么序號比它大的內核物件將的不到被出理的機會。
為了解決這一問題,可以采用雙WaitForMultipleObjects檢測機制來實作。
————————————————
著作權宣告:本文為CSDN博主「四極管」的原創文章,遵循CC 4.0 BY-SA著作權協議,轉載請附上原文出處鏈接及本宣告。
原文鏈接:https://blog.csdn.net/yangxingbo0311/article/details/7323762
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/282208.html
標籤:進程/線程/DLL
上一篇:有無vb大佬解答迷津?
下一篇:串口接收無顯示
