tags:

views:

60

answers:

2
+1  Q: 

Detached Threads

When we make Detached threads in main. and supose main exits... do the detached threads keep on going on or do they also exit just like our normal Joinable threads?

A: 

If this would be another thread then main, the other threads would continue. But the C99 standard says

If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function...

(All common platforms nowadays will return an int from main, in particular this is required by POSIX.)

And the POSIX page for exit states

These functions shall terminate the calling process...

So in summary a return from main terminates the whole program including all threads.

Jens Gustedt
You should cite which standard. The text you've cited seems a bit odd, since current versions of POSIX are aligned with C99 which **requires** `main` to have a return type of `int`.
R..
@R.: No, you are mistaken, C99 doesn't require a return of `int`. With some weird wording at the end of the para for `main` it allows other signatures. And with the para I am citing it makes clear that non-`int` return is a possibility.
Jens Gustedt
+1  A: 

It depends entirely on how the main thread exits. If it exits using exit() or returning from main(), then the entire process is exited, and every thread is terminated.

However, if it uses pthread_exit() to terminate, then the process continues running.

caf