简体   繁体   English

关闭线程方法

[英]Closing threads methods

What is the difference between 之间有什么区别

ExitThread(exitcode);

and

CloseHandle(threadHandle);

?

It seems that they do the same thing, but they must have a difference. 看起来他们做同样的事情,但是必须有所不同。

These functions do very different things. 这些功能做的事情非常不同。 ExitThread immediately ends the calling thread. ExitThread立即结束调用线程。 CloseHandle just destroys a handle, thus removing a reference to a thread object. CloseHandle只是破坏了一个句柄,因此删除了对线程对象的引用。 It has no affect on the thread which handle is closed; 它对关闭句柄的线程没有影响; the thread will continue its normal execution. 线程将继续其正常执行。 Typically, a thread handle is closed after the thread has finished (eg after waiting for its completion with WaitForSingleObject ). 通常,线程句柄在线程完成之后关闭(例如,在使用WaitForSingleObject等待其完成之后)。 But sometimes, a handle is closed for the thread that is still running; 但是有时,对于仍在运行的线程,将关闭句柄。 it's often referred as detaching a thread. 这通常称为分离线程。 This needs to be done if the application "forgets" the thread and will not wait for its completion; 如果应用程序“忘记”线程并且不等待其完成,则需要完成此操作。 handles to such a thread should be closed to let the kernel automatically destroy associated resources when the thread completes. 应该关闭此类线程的句柄,以使内核在线程完成时自动销毁关联的资源。

Also see answers to a similar question: Windows C++ - closing thread with CloseHandle 另请参见类似问题的答案: Windows C ++-使用CloseHandle关闭线程

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

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