Subj : Re: Main thread going away but worker threads executing? To : comp.programming.threads From : Kev Date : Thu Jul 21 2005 03:29 pm On Mon, 18 Jul 2005, doug wrote: > "Kev" wrote in message >> [snip] >> >> 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.) >> > [snip] > > In these managed environments (such as C#), threads usually run within an > 'engine' process - be it a JVM, a CLR runtime instance, etc. These > environments have defined threading semantics - and all threads are created > equal*. Your main thread is not special. > > With *nix using pthreads, the opposite is true. pthreads is a library, and Cool, that's what I wanted to know! But it would be nice if I could convincingly demonstrate to myself that this is indeed true. I can prove that killing the main thread doesn't automatically kill spawned threads in C#. (I have a function in which I create a few ThreadStart objects and then start threads using them. I call that function from Main() and exit immediately with a message. The other threads continue executing long after the Main thread is gone. I have been able to verify my observations with the Threads window in Visual Studio's debugger.) However, how do I prove the opposite for unmanaged C/C++? I want to write a very simple C or C++ program (on Windows) that shows a main thread killing all its spawned threads on exiting. I looked at the sample MSDN program with C and Win32 available at http://snipurl.com/gf08 but it couldn't do what I wanted... Kev .