WaitForSingleObject 等待線程句柄

http://blog.csdn.net/dyzhen/article/details/5993396


WaitForSingleObject(hThread/*某個線程的句柄*/,INFINITE)可以嗎? 
hThread代表的線程如果在運行,就要一直等下去,直到線程退出來嗎? 
如果是這樣,就是說我等到的時候,線程已經退出了?我還需要調用類似於ReleaseSemaphore、對hThread做類似處理的某個(??)函數嗎?什麼函數? 

比如: 
CWinThread   *pThread=AfxBeginThread(......); 


WaitForSingleObject(pThread-> m_hThread,INFINITE); 
正確否?

 

----------------------------------------------------------------------

> > WaitForSingleObject(hThread/*某個線程的句柄*/,INFINITE)可以嗎? 
線程的句柄在WIN32中可以作爲信號量使用。當線程結束時,其狀態由非信號狀態轉變爲信號狀態。可以使用WaitForSingleObject函數來等線程對象。 

> > hThread代表的線程如果在運行,就要一直等下去,直到線程退出來嗎? 
是否一直等下去,取決於第二個參數傳入的內容。如下所示: 
INFINITE:像你所使用的那樣傳入此參數,此函數會一直等待下去。 
0               :函數檢測對象狀態並立即返回 
> 0             :如果超出等待時間,線程仍然處於非信號狀態,將返回WAIT_TIMEOUT 

> > 如果是這樣,就是說我等到的時候,線程已經退出了?我還需要調用類似於> > ReleaseSemaphore、對hThread做類似處理的某個(??)函數嗎?什麼函數? 
如果沒有其它與此線程相關的資源需要回收,關閉線程句柄就行了。 
BOOL   CloseHandle( 
    HANDLE   hObject       //   handle   to   object 
);

-----------------------------------------------------------------------

CloseHandle   invalidates   the   specified   object   handle,   decrements   the   object 's   handle   count,   and   performs   object   retention   checks.   After   the   last   handle   to   an   object   is   closed,   the   object   is   removed   from   the   system.   

Closing   a   thread   handle   does   not   terminate   the   associated   thread.   To   remove   a   thread   object,   you   must   terminate   the   thread,   then   close   all   handles   to   the   thread. 
........ 
The   thread   object   remains   in   the   system   until   the   thread   has   terminated   and   all   handles   to   it   have   been   closed   through   a   call   to   CloseHandle.  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章