user420878
user420878

Reputation: 229

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?

Upvotes: 5

Views: 742

Answers (2)

Jens Gustedt
Jens Gustedt

Reputation: 78973

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.

Upvotes: 1

caf
caf

Reputation: 239321

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.

Upvotes: 4

Related Questions