[英]How to properly use _beginthread and endthread
I'm used to work with good old WinAPI call CreateThread()
, and check thread's status using the waiting function, eg WaitForSingleObject()
, Once thread thread is signaled with WAIT_OBJECT_0
, i close it using CloseHandle().
我习惯使用旧的WinAPI调用
CreateThread()
,并使用等待函数检查线程的状态,例如WaitForSingleObject()
,一旦用WAIT_OBJECT_0
发信号通知线程线程,我使用CloseHandle().
关闭它CloseHandle().
Recently I've decided to move to beginthread
and somehow avoid the risks of uninitialized crt and accidental memory leaks that may happen. 最近,我决定搬到
beginthread
并以某种方式避免未初始化CRT和可能发生的意外内存泄漏的风险。
Doing that got me confused. 这样做让我很困惑。
endthread()
? endthread()
的确切目的是什么? why when i call CloseHandle()
in the main function, after thread's execution, CloseHandle()
crashes with invalid handle? CloseHandle()
时,在执行线程后, CloseHandle()
因无效句柄而崩溃? beginthread
? beginthread
返回的beginthread
吗? endthread
, As i understood is invoked automatically by the thread once my function goes out of scope, so should i call it anyway just before i get out of scope? endthread
,正如我endthread
,一旦我的函数超出范围,线程会自动调用,所以我应该在我超出范围之前调用它吗? CloseHandle()
1.From where does the thread obtain a reference / instance to its handle. CloseHandle()
1.从哪里线程获得其句柄的引用/实例。 2. If i do insist on using endthread()
, should it be the last command in the thread? endthread()
,它应该是线程中的最后一个命令吗? thanks 谢谢
EDIT: MSDN paper describing the leaks, here . 编辑: 这里描述泄漏的MSDN论文。
As stated in the comments by David Hefferman you can simply change your code back to using CreateThread. 正如David Hefferman的评论中所述,您只需将代码更改回使用CreateThread即可。 The Visual C++ runtime (CRT) will automatically initialize the CRT's per thread data the first time you use a function that uses the per thread data.
第一次使用使用每线程数据的函数时,Visual C ++运行时(CRT)将自动初始化CRT的每个线程数据。
The CRT will also automatically free the per thread data when a thread ends, so using CreateThread won't cause memory leaks. 当线程结束时,CRT也会自动释放每线程数据,因此使用CreateThread不会导致内存泄漏。 There is one exception, if all of the following conditions are true then per thread data isn't automatically freed:
有一个例外,如果满足以下所有条件,那么每个线程数据不会自动释放:
Note that even if all this true in your case, the memory leak isn't going to be significant unless you're creating a long lived application that creates and destroys hundreds of thousands of threads over its life time. 请注意,即使在您的情况下所有这一切都是正确的,除非您创建一个长期存在的应用程序,在其生命周期内创建并销毁数十万个线程,否则内存泄漏不会很重要。
If you still want to use the CRTs thread creation functions ( _beginthread
/ _beginthreadex
) you should follow these guidelines: 如果您仍想使用CRTs线程创建函数(
_beginthread
/ _beginthreadex
),您应该遵循以下准则:
_beginthread
. _beginthread
返回的句柄。 With _beginthread
the thread handle is automatically closed when the thread exits, which can potentially happen before _beginthread
even returns. _beginthread
,线程句柄会在线程退出时自动关闭,这可能会在_beginthread
返回之前发生。 You can't use it with WaitForSingleObject safely because the thread might have already exited before you call this function. _beginthreadex
instead. _beginthreadex
。 _beginthread
. _beginthread
返回的句柄。 The CRT will do it automatically, and as described in the previous point, may do so before you have chance to. _beginthreadex
when you no longer need it. _beginthreadex
返回的句柄。 The CRT won't do this automatically for you, so it's your own responsibility. _endthread
or _endthreadex
unless you want to quickly and abnormally terminate the thread. _endthread
或_endthreadex
。 While the CRT will free its own per thread data, none of the C++ destructors for any of the thread's objects will be called. _exit
ends the process without calling destructors. _exit
在不调用析构函数的情况下结束进程的方式。 _beginthread
or _beginthreadex
. _beginthread
或_beginthreadex
。 This will result in C++ destructors being called as a normal part of the function return.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.