WaitForSingleObject 等待线程句柄

来源:互联网 发布:程序员名词 编辑:程序博客网 时间:2024/06/10 01:08

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.