WaitForMultipleObjects

WaitForMultipleObjects是Win WaitForMultipleObjects( DWORD n

函式原型與概述

WaitForMultipleObjects是Windows中的一個功能非常強大的函式,幾乎可以等待Windows中的所有的核心對象(關於該函式的描述和例子見MSDN,)。但同時該函式在用法上卻需要一定的技巧。
原型:DWORD WaitForMultipleObjects(
DWORD nCount,
const HANDLE* lpHandles,
BOOL bWaitAll,
DWORD dwMilliseconds
);
當WaitForMultipleObjects等到多個核心對象的時候,如果它的bWaitAll 參數設定為false。其返回值減去WAIT_OBJECT_0 就是參數lpHandles數組的序號。如果同時有多個核心對象被觸發,這個函式返回的只是其中序號最小的那個。如果為TRUE 則等待所有信號量有效在往下執行。(FALSE 當有其中一個信號量有效時就向下執行)
問題就在這裡,我們如何可以獲取所有被同時觸發的核心對象。舉個例子:我們需要在一個執行緒中處理從完成連線埠、資料庫、和可等待定時器來的數據。一個典型的實現方法就是:用WaitForMultipleObjects等待所有的這些事件。如果完成連線埠,資料庫發過來的數據量非常大,可等待定時器時間也只有幾十毫秒。那么這些事件同時觸發的幾率可以說非常大,我們不希望丟棄任何一個被觸發的事件。那么如何能高效地實現這一處理呢?
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.
多個核心對象被觸發時,WaitForMultipleObjects選擇其中序號最小的返回。而WaitForMultipleObjects它只會改變使它返回的那個核心對象的狀態。
這兒又會產生一個問題,如果序號最小的那個對象頻繁被觸發,那么序號比它大的核心對象將得不到被處理的機會。
為了解決這一問題,可以採用雙WaitForMultipleObjects檢測機制來實現。見下面的例子:
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++);
//同時檢測其他的事件
while(nIndex < nCount) //nCount事件對象總數
{
dwRet = WaitForMultipleObjects(nCount - nIndex,&pHandles&#91;nIndex&#93;,false,0);
switch(dwRet)
{
case WAIT_TIMEOUT:
nIndex = nCount; //退出檢測,因為沒有被觸發的對象了.
break;
case WAIT_FAILED:
return 1;
default:
{
nIndex = nIndex + dwRet - WAIT_OBJECT_0;
ProcessHanlde(nIndex++);
}
break
}
}
}
break;
}
}
return 0;
}

參數

Parameters
nCount
&#91;in&#93; Specifies the number of object handles in the array pointed to by lpHandles. The maximum number of object handles is MAXIMUM_WAIT_OBJECTS.
lpHandles
&#91;in&#93; Pointer to an array of object handles. For a list of the object types whose handles can be specified, see the following Remarks section. The array can contain handles to objects of different types. It may not contain the multiple copies of the same handle.
If one of these handles is closed while the wait is still pending, the function's behavior is undefined.
Windows NT/2000/XP: The handles must have synchronize access. For more information, see Standard Access Rights.
Windows 95/98/Me: No handle may be a duplicate of another handle created using DuplicateHandle.
bWaitAll
&#91;in&#93; Specifies the wait type. If TRUE, the function returns when the state of all objects in the lpHandles array is signaled. If FALSE, the function returns when the state of any one of the objects is set to signaled. In the latter case, the return value indicates the object whose state caused the function to return.
dwMilliseconds
&#91;in&#93; Specifies the time-out interval, in milliseconds. The function returns if the interval elapses, even if the conditions specified by the bWaitAll parameter are not met. If dwMilliseconds is zero, the function tests the states of the specified objects and returns immediately. If dwMilliseconds is INFINITE, the function's time-out interval never elapses.

返回值

如果函式成功,返回值表示該事件導致該函式返回。這個值可以是下列之一。
ValueMeaning
WAIT_OBJECT_0到(WAIT_OBJECT_0 + nCount - 1如果bWaitAll為TRUE),則返回值表明所有指定對象的狀態信號。
如果bWaitAll為FALSE,則返回值減去不是WAIT_OBJECT_0表示lpHandles數組的對象的滿意指數的等待。如果多個對象在通話過程中信號成為,這是與所有的信號對象的最小索引值的信號對象的數組索引。
WAIT_ABANDONED_0至(WAIT_ABANDONED_0 + nCount - 1如果bWaitAll為TRUE),則返回值表明所有指定對象的狀態,至少是暗示的對象之一,是一個廢棄的互斥對象
如果bWaitAll為FALSE,則返回值減去WAIT_ABANDONED_0表示lpHandles數組的一個廢棄的互斥對象的滿意指數的等待。
WAIT_TIMEOUTThe逾時間隔已過,由bWaitAll參數指定的條件得不到滿足。

相關詞條

相關搜尋

熱門詞條

聯絡我們