1: When the main process exits, no matter the child, grandchild ... Threads will exit. However, threads created by a child thread say that, for example, the following sub-thread creates a grandchild thread, the enters upgradeable exits, and the grandchild thread does not exit
#include <pthread.h>#include<stdio.h>void* Fun (void*v) {Sleep (Ten); printf ("grandson Thread exit\n");}void* Thrd_start_routine (void*v) {pthread_t THD; Pthread_create (&THD, NULL, &Fun , NULL); Sleep (5); printf ("created thread exit\n");}intMain () {pthread_t thrdid; Pthread_create (&thrdid, NULL, &Thrd_start_routine, NULL); Sleep ( the); printf ("Main thread exit\n"); return 0;}
Output is
After 5 seconds,
Created thread exit
It's been 5 seconds.
Grandson Thread exit
It's been 5 seconds.
Main thread exit
2: If the main thread calls Pthread_exit, then it exits, and the child thread does not exit
#include <pthread.h>#include<stdio.h>void* Thrd_start_routine (void*v) {Sleep (Ten); printf ("created thread\n");}intMain () {pthread_t thrdid; Pthread_create (&thrdid, NULL, &Thrd_start_routine, NULL); Sleep (5); printf ("Main thread\n"); Pthread_exit (NULL); return 0;}
After 5 seconds,
Main thread
It's been 5 seconds.
Created thread
Multi-threaded, thread exit order