WaitForMultipleObjects是Windows中的一個(gè)功能非常強(qiáng)大的函數(shù),幾乎可以等待Windows中的所有的內(nèi)核對(duì)象(關(guān)于該函數(shù)的描述和例子見MSDN,)。但同時(shí)該函數(shù)在用法上卻需要一定的技巧。
原型:DWORD WaitForMultipleObjects(
DWORD nCount,
const HANDLE* lpHandles,
BOOL bWaitAll,
DWORD dwMilliseconds
);
當(dāng)WaitForMultipleObjects等到多個(gè)內(nèi)核對(duì)象的時(shí)候,如果它的bWaitAll 參數(shù)設(shè)置為false。其返回值減去WAIT_OBJECT_0 就是參數(shù)lpHandles數(shù)組的序號(hào)。如果同時(shí)有多個(gè)內(nèi)核對(duì)象被出發(fā),這個(gè)函數(shù)返回的只是其中序號(hào)最小的那個(gè)。
問題就在這里,我們?nèi)绾慰梢垣@取所有被同時(shí)觸發(fā)的內(nèi)核對(duì)象。舉個(gè)例子:我們需要在一個(gè)線程中處理從完成端口、數(shù)據(jù)庫、和可等待定時(shí)器來的數(shù)據(jù)。一個(gè)典型的實(shí)現(xiàn)方法就是:用WaitForMultipleObjects等待所有的這些事件。如果完成端口,數(shù)據(jù)庫發(fā)過來的數(shù)據(jù)量非常大,可等待定時(shí)器時(shí)間也只有幾十毫秒。那么這些事件同時(shí)觸發(fā)的幾率可以說非常大,我們不希望丟棄任何一個(gè)被觸發(fā)的事件。那么如何能高效地實(shí)現(xiàn)這一處理呢?
MSDN中有一句非常重要的描述,它可以說是WaitForMultipleObjects用法的精髓:The function modifies the state of some types of synchronization objects. Modification occurs only for the object or objects whose signaled state caused the function to return. For example, the count of a semaphore object is decreased by one. When bWaitAll is FALSE, and multiple objects are in the signaled state, the function chooses one of the objects to satisfy the wait; the states of the objects not selected are unaffected.
多個(gè)內(nèi)核對(duì)象被觸發(fā)時(shí),WaitForMultipleObjects選擇其中序號(hào)最小的返回。而WaitForMultipleObjects它只會(huì)改變使它返回的那個(gè)內(nèi)核對(duì)象的狀態(tài)。
這兒又會(huì)產(chǎn)生一個(gè)問題,如果序號(hào)最小的那個(gè)對(duì)象頻繁被觸發(fā),那么序號(hào)比它大的內(nèi)核對(duì)象將得不到被處理的機(jī)會(huì)。
為了解決這一問題,可以采用雙WaitForMultipleObjects檢測(cè)機(jī)制來實(shí)現(xiàn)。見下面的例子:
DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
DWORD dwRet = 0;
int nIndex = 0;
while(1)
{
dwRet = WaitForMultipleObjects(nCount,pHandles,false,INFINITE);
switch(dwRet)
{
case WAIT_TIMEOUT:
break;
case WAIT_FAILED:
return 1;
default:
{
nIndex = dwRet - WAIT_OBJECT_0;
ProcessHanlde(nIndex++);
//同時(shí)檢測(cè)其他的事件
while(nIndex < nCount)
{
dwRet = WaitForMultipleObjects(nCount - nIndex,&pHandles[nIndex],false,0);
switch(dwRet)
{
case WAIT_TIMEOUT:
nIndex = nCount; //退出檢測(cè),因?yàn)闆]有被觸發(fā)的對(duì)象了.
break;
case WAIT_FAILED:
return 1;
default:
{
nIndex = dwRet - WAIT_OBJECT_0;
ProcessHanlde(nIndex++);
}
break
}
}
}
break;
}
}
return 0;
}
|