I have a problem about main threads and other threads in the same process. When the main function returns, do the other threads exit too? I am confused about this.
Consider the following test code:
void* test1(void *arg) { unsigned int i = 0; while (1){ i+=1; } return NULL; } void* test2(void *arg) { long double i = 1.0; while (1){ i *= 1.1; } return NULL; } void startThread ( void * (*run)(void*), void *arg) { pthread_t t; pthread_attr_t attr; if (pthread_attr_init(&attr) != 0 || pthread_create(&t, &attr, run, arg) != 0 || pthread_attr_destroy(&attr) != 0 || pthread_detach(t) != 0) { printf("Unable to launch a thread\n"); exit(1); } } int main() { startThread(test1, NULL); startThread(test2, NULL); sleep(4); printf("main thread return.\n"); return 0; }
When the "main thread return." prints out, thread test1 and test2 also exit, can anyone tell me why?
Threads are part of the process. If the process exits, they (along with all other process resources) cease to exist.
The answer to the original question "what happens to a detached thread when main() exits" is: It continues running (because the standard doesn't say it is stopped), and that's well-defined, as long as it touches neither (automatic|thread_local) variables of other threads nor static objects.
Calling the exit subroutine terminates the entire process, including all its threads. In a multithreaded program, the exit subroutine should only be used when the entire process needs to be terminated; for example, in the case of an unrecoverable error.
As long as the main-method thread or any other user thread remains alive, your application will continue to execute. In your case, the threads are user threads and hence are allowed to complete before the main thread exits.
You should use pthread_join()
on each of the new threads, to inform the calling thread to wait on the sub-threads, suspending execution - and process exit - until those threads terminate.
Calling pthread_detach
on the created threads won't keep them around after a process exits. From the linux man page:
The detached attribute merely determines the behavior of the system when the thread terminates; it does not prevent the thread from being terminated if the process terminates using exit(3) (or equivalently, if the main thread returns).
You'll sometimes see a pthread_exit
in main
used instead of explicit pthread_join
calls, the intent being that exiting main
in this way will allow other threads to continue running. In fact, the linux man page states this explicitly:
To allow other threads to continue execution, the main thread should terminate by calling pthread_exit() rather than exit(3).
But I don't know if this is expected behavior on all platforms, and I've always stuck to using pthread_join
.
pthread_join
requires the pthread_t
for the target thread, so your code will need to change a bit since you need to create both threads before calling pthread_join
to wait for them both. So you can't call it in startThread
. You'll need to return a pthread_t
, or pass a pointer to a pthread_t
to your startThread
function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With