Subj : Strange behaviour To : comp.programming.threads From : sunglo Date : Tue Apr 19 2005 11:03 am I have the following simple code (I know it's ugly, and many things are missing, like error checks, etc.): #include #include #include #include #define THREADS 5 void *threadfunc(void *unused) { printf("Bye\n"); sleep(5); pthread_exit (NULL); } int main(void) { pthread_t tids[THREADS]; int i; for(i = 0; i < THREADS; i++) { printf("main creates thread %d\n", i); pthread_create (&tids[i], NULL, threadfunc, NULL); printf("created: thread %d\n", i); } pthread_exit (NULL); } I'm using linux 2.6 with NPTL. When this code runs, sometimes it executes all the threads (ie, the messages from main() *AND* those from the threads are printed, and the process waits about 5 secs. before terminating), some other times it terminates immediately (only the messages from main() are printed, and then it terminates). It almost seems that, under certain conditions, the call to pthread_exit() from main() terminates the whole process. I believe that calling pthread_exit() from main() should not terminate the whole process...unless, perhaps, at the time main() calls pthread_exit(), no other thread has actually been created, so, being main() the only thread, pthread_exit() terminates the process? (just a guess, I hope you have some better explanation). Thanks for your answers. .