Subj : Re: Main thread going away but worker threads executing? To : comp.programming.threads From : Doug Harrison Date : Mon Jul 18 2005 03:51 pm On Mon, 18 Jul 2005 10:52:33 -0700, Kev wrote: > I've noticed that in a managed environment (such as C#), even if the main > thread that spawns worker threads dies, the worker threads themselves > continue to execute until they reach completion. > > Example: > Main Thread > Start Thread 0 > Start Thread 1 > Start Thread 2 > End Main Thread immediately without waiting on Threads 0,1,2 See: Foreground and Background Threads http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconthreadlevels.asp > Does the same thing happen in unmanaged environments such as C and C++? If > yes, is there any difference in the basic treatment of threads between > unmanaged and managed environments at all? > > I have been under the impression that this behavior is expected in C# but > that the opposite is true in C. (If the main thread dies, everything dies.) Speaking about Visual C++ native code, the primary thread, i.e. the one that runs main() and performs startup/termination functions, such as creating and destroying global objects, is indeed special. It will ultimately call ExitProcess, which will terminate all secondary threads you've allowed to continue to run during the shutdown. Of course, it may not get that far if a secondary thread crashes, deadlocks, etc, because the environment in which it was running was crumbling down all around it. -- Doug Harrison dsh@mvps.org .