windows 是否有必要在退出 Win32 应用程序之前显式停止所有线程?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2197699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it necessary to explicitly stop all threads prior to exiting a Win32 application?
提问by sharptooth
I have a Win32 native VC++ application that upon entering WinMain()
starts a separate thread, then does some useful job while that other thread is running, then simply exits WinMain()
- the other thread is not explicitly stopped.
我有一个 Win32 本机 VC++ 应用程序,它在进入WinMain()
时启动一个单独的线程,然后在另一个线程运行时做一些有用的工作,然后简单地退出WinMain()
- 另一个线程没有明确停止。
This blog postsays that a .NET application will not terminate in this case since the other thread is still running. Does the same apply to native Win32 applications?
这篇博文说 .NET 应用程序在这种情况下不会终止,因为另一个线程仍在运行。这同样适用于本机 Win32 应用程序吗?
Do I have to stop all threads prior to exiting?
我是否必须在退出之前停止所有线程?
回答by Alex Jenter
Yes you have, otherwise your application may hang. I recommend reading Raymond Chen's excellent blog posts on this topic:
是的,您有,否则您的应用程序可能会挂起。我建议阅读 Raymond Chen 关于这个主题的优秀博客文章:
回答by Chris Becke
The short of it is: For a native Win32 process to terminate, one of two conditions must be met:
简而言之:要终止本机 Win32 进程,必须满足以下两个条件之一:
- Someone calls ExitProcess or TerminateProcess.
- All the threads exit (by returning from their ThreadProc (including the WinMainEntryPoint that is the first thread created by windows)), close (by calling ExitThread), or terminated (someone calls TerminateThread).
- 有人调用 ExitProcess 或 TerminateProcess。
- 所有线程都退出(通过从它们的 ThreadProc 返回(包括 WinMainEntryPoint,它是 Windows 创建的第一个线程))、关闭(通过调用 ExitThread)或终止(有人调用 TerminateThread)。
(The first condition is actually the same as the 2nd: ExitProcess and TerminateProcess, as part of their cleanup, both call TerminateThread on each thread in the process).
(第一个条件实际上与第二个相同:ExitProcess 和 TerminateProcess,作为它们清理的一部分,都在进程中的每个线程上调用 TerminateThread)。
The c-runtime imposes different conditions: For a C/C++ application to terminate, you must either:
c 运行时强加了不同的条件: 要终止 C/C++ 应用程序,您必须:
- return from main (or WinMain).
- call exit()
- 从 main(或 WinMain)返回。
- 调用退出()
Calling exit() or returning from main() both cause the c-runtime to call ExitProcess(). Which is how c & c++ applications exit without cleaning up their threads. I, personally, think this is a bad thing.
调用 exit() 或从 main() 返回都会导致 c 运行时调用 ExitProcess()。这就是 c & c++ 应用程序退出而不清理它们的线程的方式。我个人认为这是一件坏事。
However, non trivial Win32 processes can never terminate because many perfectly, otherwise reasonable, Win32 subsystems create worker threads. winsock, ole, etc. And do not provide any way to cause those threads to spontaneously close.
然而,非平凡的 Win32 进程永远不会终止,因为许多完美的、否则合理的 Win32 子系统会创建工作线程。winsock、ole 等。并且不提供任何方法来导致这些线程自发关闭。
回答by minjang
No, when WinMain returns, the process will be terminated, and this means all threads spawned by the process should be terminated though they might not be closed gracefully.
不,当 WinMain 返回时,进程将终止,这意味着该进程产生的所有线程都应该终止,尽管它们可能不会正常关闭。
However, it is possible that a primary thread is terminated while the other threads are running, resulting in the application is still running. If you call ExitThread
(not exit
or ExitProcess
) in WinMain, and there are running threads (eventually created by the primary thread), then, you may observe this behavior. Nonetheless, just return in WinMain will call ExitProcess
, and that means all threads are should be terminated.
但是,主线程可能会在其他线程正在运行时终止,从而导致应用程序仍在运行。如果您在 WinMain 中调用ExitThread
(notexit
或ExitProcess
),并且有正在运行的线程(最终由主线程创建),那么您可能会观察到这种行为。尽管如此,只要在 WinMain 中 return 就会调用ExitProcess
,这意味着应该终止所有线程。
Correct me if it's wrong.
如果错了,请纠正我。
回答by ddh
I think you can first close all your windows(so the user won't see your application), and then set a flag for exit, your thread should check the flag periodicly, and once found set, the thread should return.
我认为您可以先关闭所有窗口(这样用户就不会看到您的应用程序),然后设置退出标志,您的线程应定期检查该标志,一旦找到设置,线程应返回。
after set the flag, your main thread could call ::WaitForSingleObject() or ::WaitForMultipleObjects() for a while (say, three seconds), if the thread(s) not return, just kill them by ::TerminateThread().
设置标志后,您的主线程可以调用 ::WaitForSingleObject() 或 ::WaitForMultipleObjects() 一段时间(例如,三秒钟),如果线程没有返回,只需通过 ::TerminateThread() 杀死它们。
回答by YeenFei
short answer : yes
简短的回答:是的