关于线程函数结束前显式调用_endthreadex

1. 本来,MSDN已经明确指出了:

You can call _endthread or _endthreadex explicitly to terminate a thread; however, _endthread or _endthreadex is

called automatically when the thread returns from the routine passed as a parameter to_beginthread or

_beginthreadex. Terminating a thread with a call toendthread or _endthreadex helps ensure proper recovery of

resources allocated for the thread(http://msdn.microsoft.com/en-us/library/vstudio/hw264s73.aspx).

即,线程从线程函数(例程)返回后,将自动调用_endthreadex. 意即不必在线程函数返回前显式调用_endthreadex.

而在示例_beginthreadex函数时,MSDN给出了如下例子(http://msdn.microsoft.com/en-us/library/kdzttdcb(v=VS.80).aspx):

 
 

2. 到底要不在线程函数前调用_endthreadex呢?

MSDN在对_endthreadex的注解中提到:

_endthread and _endthreadex cause C++ destructors pending in the thread not to be called.

意即在线程中调用_endthreadex,会导致C++对象的析构函数挂起而不被调用。如果在线程函数中生成的对象关乎资源分配,则可能引起资

源不能被正常回收(destructor不被调用)。

相关资料:

http://stackoverflow.com/questions/9090143/shouldnt-i-use-endthreadex-in-thread-procedure-for-stack-unwinding

http://www.tech-archive.net/Archive/Development/microsoft.public.win32.programmer.kernel/2005-05/msg00096.html

而如果非要用怎么办?

如下例(http://blog.chinaunix.net/uid-406135-id-3421540.html):

在MSDN的示例中,线程函数结束都有

    _endthreadex( 0 );

    return 0;


以前,我一直不加_endthreadex( 0 ),因为觉得它很丑很冗余,MSDN上如是说:

_endthread or _endthreadex is called automatically when the thread returns from the routine passed as a parameter.

也就是它屁用没有,假如我写成:
    _endthreadex( 0 );
    return 1;
还会引起歧义。
今日我一时兴起,在return前加上了 _endthreadex( 0 ),然后就报内存泄露,因为它中止线程前不会析构局部变量。

于是将代码改为:
unsigned __stdcall foo( void* p )
{
    {
        //这里是执行代码
    }
    _endthreadex( 0 );
    return 0;
}
也就是加了个似乎没用的{}。

转载于:https://www.cnblogs.com/qinfengxiaoyue/archive/2013/05/18/3084817.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值