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.  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值