Multithreading, the exit order of threads

Source: Internet
Author: User

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 (&AMP;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.