Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it necessary to explicitly stop all threads prior to exiting a Win32 application?

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.

This blog post says 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?

Do I have to stop all threads prior to exiting?

like image 811
sharptooth Avatar asked Feb 04 '10 06:02

sharptooth


People also ask

What does ExitThread do?

ExitThread is the preferred method of exiting a thread in C code. However, in C++ code, the thread is exited before any destructors can be called or any other automatic cleanup can be performed. Therefore, in C++ code, you should return from your thread function.


2 Answers

Yes, you have to if you are simply exiting or terminating the main thread via ExitThread or TerminateThread, otherwise your application may not fully shutdown. I recommend reading Raymond Chen's excellent blog posts on this topic:

  • The old-fashioned theory on how processes exit
  • Quick overview of how processes exit on Windows XP
  • How my lack of understanding of how processes exit on Windows XP forced a security patch to be recalled
  • During process termination, the gates are now electrified
  • If you return from the main thread, does the process exit?

But please note in particular that if you properly return from the main or WinMain function, the process will exit as described by the ExitProcess API documentation and the last post by Raymond Chen that is being linked above!

like image 179
Alex Jenter Avatar answered Oct 02 '22 23:10

Alex Jenter


The short of it is: For a native Win32 process to terminate, one of two conditions must be met:

  • 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).

(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).

The c-runtime imposes different conditions: For a C/C++ application to terminate, you must either:

  • return from main (or WinMain).
  • call exit()

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.

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.

like image 36
Chris Becke Avatar answered Oct 02 '22 22:10

Chris Becke