1
votes

In linux, the whole process exits when the main thread terminates no matter how it terminates,by the function exit() or returns from main. If the main thread returns from main(),it will return to the "C runtime" known as crt.o or something like that. In the crt.o,whose c code like this: exit(main(argc, argv)); exit() will be called by the main thread
eventually, as a result, all the threads terminate.

Does my thought seem right?

If in the crt.o exit() is replaced by a thread terminating function like void thread_exit(int),which can only terminates a thread with an exit status, the c source code of crt.o seems like thread_exit(main(argc,argv)),do other thread still run when the main thread terminates?

2
It feels like it'd be faster to write a test program to tell than to write a question :) - sarnold
@sarnold: Writing a test program is rarely the correct way to answer a question like this. - R.. GitHub STOP HELPING ICE

2 Answers

6
votes

Returning from main is equivalent to calling exit, and terminates the process. To terminate just a single thread, use pthread_exit. Note that it's valid for the initial thread to call pthread_exit (and the process does not terminate until all threads have exited or until one of them calls exit) and threads other than the initial thread implicitly call pthread_exit if you return from their start functions.

4
votes

on unix, a process terminates after the last thread has been terminated. Note that this can be any thread, not just "main" thread. So, if you replace exit with pthread_exit, but spawned a thread before returning in main, your process will not terminate.