Subj : Re: Main thread going away but worker threads executing? To : comp.programming.threads From : doug Date : Tue Jul 19 2005 12:42 am "Kev" wrote in message news:Pine.SOL.4.63.0507181045340.12475@conquest.OCF.Berkeley.EDU... > > 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 > > > 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.) > > Thanks in advance! > Kevin > Crikey, I've had a nightmare of a time with my computers lately. Bad disk sectors, fried network interfaces and cables, and an exploding router. In Scotland it just rains. Down here in London it positively *thunderstikes*. I sent a post a wee while ago, but there's a chance my new router ate it (it's been a difficult evening). So, since everyting appears to be stable, let me crack open a beer. So here goes again. 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 is not part of the C or C++ spec (which has no real notion of threads). Look at the manpages for pthread_exit() - they state that if the main thread exits without calling pthread_exit, the process exits. Calling pthread_exit() in your main thread will make it exit like any other thread (and it can even be joined on some platforms). Doug * Pedant notice: if you create them with default attributes**. ** I don't like pedants. *** There is no ***. Go away. .