We often create additional new threads in one thread, and if the main thread exits, will it affect the new threads it creates? Let's talk about it here.
1, the main thread waiting for the new line enters upgradeable end exit, the main thread after the exit. Normal execution.
Sample code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h >
#include <sys/types.h>
pthread_t ntid;//thread id
void printids (const char *s)
{
pid_t pid ;
pthread_t Tid;
PID = Getpid ();
Tid = Pthread_self ();
printf ("%s pid%u tid%u (0x%x) \ n", s, (unsigned int) PID,
(unsigned int) tid, (unsigned int) tid)
;
void *thrfun (void *arg) {
//sleep (1);//makes the mainline enters upgradeable exit
printids ("New Thread");
return ((void *) 0);
}
int main () {
int err;
Err = Pthread_create (&ntid,null,thrfun,null);
if (Err!= 0)
perror ("Pthread_create");
Printids ("main thread");
Sleep (1);//Waiting for new line enters upgradeable end
exit (0);
}
Run Result:
huangcheng@ubuntu:~$./a.out
main thread pid 2344 tid 3077813952 (0xb773b6c0)
new thread pid 2344 tid 3077811056 ( 0XB773AB70)