在多線程下面,有時候我們會希望等待某一線程完成了再繼續做其他事情,要實現這個目的,可以使用Windows API函數WaitForSingleObject,或者WaitForMultipleObjects。這兩個函數都會等待Object被標為有信號(signaled)時才返回的。那么,什么是信號呢?簡單來說,Windows下創建的Object都會被賦予一個狀態量。如果Object被激活了,或者正在使用,那么該Object就是無信號,也就是不可用;另一方面,如果Object可用了,那么它就恢復有信號了。這兩個函數的優點是它們在等待的過程中會進入一個非常高效沉睡狀態,只占用極少的CPU時間片。(這兩個函數都是在內核狀態下等待內核對象,不切換到用戶模式下,因而效率很高)
WaitForSingleObject()
1. 格式
DWord WaitForSingleObject( HANDLE hHandle, DWORDdwMilliseconds);
有兩個參數,分別是THandle和Timeout(毫秒單位)。
如果想要等待一條線程,那么你需要指定線程的Handle,以及相應的Timeout時間。當然,如果你想無限等待下去,Timeout參數可以指定系統常量INFINITE。
2. 使用對象
它可以等待如下幾種類型的對象:
Event,Mutex,Semaphore,PRocess,Thread
3. 返回類型
有三種返回類型:
WAIT_OBJECT_0, 表示等待的對象有信號(對線程來說,表示執行結束);
WAIT_TIMEOUT, 表示等待指定時間內,對象一直沒有信號(線程沒執行完);
WAIT_ABANDONED 表示對象有信號,但還是不能執行 一般是因為未獲取到鎖或其他原因
示例:
[cpp] view plain copyprint?<span style="font-family:Times New Roman;">#include <windows.h> #include <stdio.h> #include <iostream.h> //聲明函數 創建線程 DWORD WINAPI FunProc( LPVOID lpParameter); void main() { HANDLE hThread; hThread=CreateThread(NULL,0,FunProc,NULL,0,NULL); DWORD dwRet=WaitForSingleObject(hThread, 1); if(dwRet==WAIT_OBJECT_0) { printf("創建的線程執行結束/n"); } if(dwRet==WAIT_TIMEOUT) { printf("等待超時/n"); } if(dwRet==WAIT_ABANDONED) { printf("Abandoned/n"); } CloseHandle(hThread); } DWORD WINAPI FunProc( LPVOID lpParameter ) { int i=1; for(; i<1000; i++) { printf("%d ", i); if(! (i%10)) printf("/n"); } return 0; }</span> 
#include <windows.h>#include <stdio.h>#include <iostream.h>//聲明函數 創建線程DWORD WINAPI FunProc( LPVOID lpParameter); void main(){ HANDLE hThread; hThread=CreateThread(NULL,0,FunProc,NULL,0,NULL); DWORD dwRet=WaitForSingleObject(hThread, 1); if(dwRet==WAIT_OBJECT_0) { printf("創建的線程執行結束/n"); } if(dwRet==WAIT_TIMEOUT) { printf("等待超時/n"); } if(dwRet==WAIT_ABANDONED) { printf("Abandoned/n"); } CloseHandle(hThread);}DWORD WINAPI FunProc( LPVOID lpParameter ){ int i=1; for(; i<1000; i++) { printf("%d ", i); if(! (i%10)) printf("/n"); } return 0;}WaitForMultipleObjecct
相對來說,WaitForMultipleObjects要復雜點點
格式為:
DWORD WaitForMultipleObjects(DWORD nCount, CONST HANDLE *lpHandles, BOOLfWaitAll, DWORDdwMilliseconds);
四個參數分別是:
1. nCount,DWORD類型,用于指定句柄數組的數量2. lphObjects,Pointer類型,用于指定句柄數組的內存地址3. fWaitAll,Boolean類型,True表示函數等待所有指定句柄的Object有信號為止4. dwTimeout,DWORD類型,用于指定等待的Timeout時間,單位毫秒,可以是INFINITE當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檢測機制來實現。見下面的例子:
[cpp] view plain copyprint?<span style="font-family:Times New Roman;">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[nIndex],false,0); switch(dwRet) { case WAIT_TIMEOUT: nIndex = nCount; //退出檢測,因為沒有被觸發的對象了. break; case WAIT_FAILED: return 1; default: { nIndex = dwRet - WAIT_OBJECT_0; ProcessHanlde(nIndex++); } break; }//switch結束 }//while結束 }//default結束 break; }//switch結束 }//while結束 return 0; }</span> 
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[nIndex],false,0); switch(dwRet) { case WAIT_TIMEOUT: nIndex = nCount; //退出檢測,因為沒有被觸發的對象了. break; case WAIT_FAILED: return 1; default: { nIndex = dwRet - WAIT_OBJECT_0; ProcessHanlde(nIndex++); } break; }//switch結束 }//while結束 }//default結束 break; }//switch結束 }//while結束 return 0; }MSDN對于這個函數的返回值還有一句話:
Return Values If the function succeeds, the return value indicates the event that caused the function to return. This value can be one of the following. ValueMeaning WAIT_OBJECT_0 to (WAIT_OBJECT_0 + nCount – 1)If bWaitAll is TRUE, the return value 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 signalled during the call, this is the array index of the signalled object with the smallest index value of all the signalled objects. WAIT_ABANDONED_0 to (WAIT_ABANDONED_0 + nCount – 1)If bWaitAll is TRUE, the return value indicates that the state of all specified objects is signaled and at least one of the objects is an abandoned mutex object. If bWaitAll is FALSE, the return value minus WAIT_ABANDONED_0 indicates the lpHandles array index of an abandoned mutex object that satisfied the wait. WAIT_TIMEOUTThe time-out interval elapsed and the conditions specified by the bWaitAll parameter are not satisfied.
返回值
如果函數成功,返回值表示該事件導致該函數返回。
這個值可以是下列之一:
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參數指定的條件得不到滿足。
from http://blog.csdn.net/xiaobai1593/article/details/6672193