简体   繁体   English

我在哪里使用_endthreadex - windows API

[英]Where do I use _endthreadex - windows API

I am a novice in the windows API, and recently I learned I should not use CreateThread and TerminateThread. 我是Windows API的新手,最近我了解到我不应该使用CreateThread和TerminateThread。 I switched to _beginthreadex , however I am not sure how I am supposed to use _endthreadex ? 我切换到_beginthreadex ,但是我不确定我应该如何use _endthreadex

For example , here is a basic function I was testing with: 例如 ,这是我正在测试的基本功能:

// MyThreadFunction - outputs "#.) I Work" and ends
unsigned int __stdcall MyThreadFunction( void * lpParam ) 
{
    int i = (int)lpParam;
    cout << i << ".) I work! " << endl;
    _endthreadex(0);
    return 0;
}

Is my placement of _endthreadex correct? 我的_endthreadex位置是否正确? I have it before the return 0, which just seems odd to me? 在返回0之前我有它,这对我来说似乎很奇怪? I read in the msdn pages about _endthreadex that it is called automatically when a function ends, but you should call it for better memory cleanup, and that is why I tried putting it in. It just doesn't seem right, sorry if this is a bad question. 我在msdn页面中读到_endthreadex它是在函数结束时自动调用的,但你应该调用它以获得更好的内存清理,这就是我尝试将其放入的原因。它只是看起来不对,对不起,如果这是一个糟糕的问题。 I just want to make sure I am doing everything right to the best of my abilities 我只是想确保我尽我所能做正确的事

You actually don't need to call _endthreadex() at all. 实际上你根本不需要调用_endthreadex() It will be called for you automatically after your thread function returns anyway. 无论如何,在线程函数返回后,它将自动为您调用。

The answer is already given in short version, and if you fancy to know more - the return from your function will actually be used as an argument in _endthreadex call. 答案已在短版本中给出,如果您想了解更多 - 函数的返回实际上将用作_endthreadex调用中的参数。 Here is what runtime is doing when you start a thread. 以下是启动线程时运行时正在执行的操作。 The thread is indeed started with a starting point somewhere in CRT, where internal _callthreadstartex is doing: 该线程确实以CRT中的某个起点开始,其中内部_callthreadstartex正在执行:

_endthreadex(MyThreadFunction(...));

That is, as soon as you return, _endthreadex will be immediately called for you and it will exit from the thread. 也就是说,一旦你返回, _endthreadex将立即为你调用,它将退出线程。

Can you use it explicitly? 你能明确地使用它吗? Yes you can, and your code is also good because it does not matter much whether you call it yourself or it will be called for you, provided that no leaks take place on the way (such as, in particular, not yet called destructors of local variables). 是的,你可以,并且你的代码也很好,因为无论你是自己调用它还是为你调用都没关系,前提是没有泄漏发生在路上(例如,特别是,尚未被称为析构函数)局部变量)。

Since it will anyway be called for you, and return from function is safer in terms of freeing local resources, there is no use, advantage and sense to make an explicit call. 因为无论如何都会为你调用,并且从函数返回在释放本地资源方面更安全,所以没有使用,优势和意义来进行显式调用。

MSDN basically explains exactly the same right there in Remarks section . MSDN基本上在备注部分解释完全相同。

You can call _endthread or _endthreadex explicitly to terminate a thread; 您可以显式调用_endthread或_endthreadex来终止线程; however, _endthread or _endthreadex is called automatically when the thread returns from the routine passed as a parameter to _beginthread or _beginthreadex. 但是,当线程从作为参数传递的例程返回到_beginthread或_beginthreadex时,会自动调用_endthread或_endthreadex。 Terminating a thread with a call to endthread or _endthreadex helps ensure proper recovery of resources allocated for the thread. 通过调用endthread或_endthreadex来终止线程有助于确保正确恢复为线程分配的资源。

You don't have to call it, as already mentioned. 如前所述,您不必调用它。 Concerning your code, I think that the final return 0; 关于你的代码,我认为最终return 0; will not even be called because _endthreadex() never returns. 甚至不会被调用,因为_endthreadex()永远不会返回。 This could also cause destructors of local objects not to be called (which is what I believe the MSDN wanted to say), so using this function could even be harmful in C++. 这也可能导致不调用本地对象的析构函数(这是我认为MSDN想要说的),因此使用此函数甚至可能在C ++中有害。 Consider using Boost.Thread, they have an exception dedicated specifically to ending a thread, which should do the right thing if you want to terminate the thread somewhere in the middle. 考虑使用Boost.Thread,它们有一个专门用于结束线程的异常,如果你想在中间的某个地方终止线程,它应该做正确的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM