• 线程退出【Linux学习】pthread_create主线程与创建的新线程之间退出关系


    本篇文章个人在青岛吃饭的时候突然想到的...最近就有想写几篇关于线程退出的文章,所以回家到之后就奋笔疾书的写出来发布了

        我们在一个线程中经常会创立另外的新线程,如果主线程退出,会不会影响它所创立的新线程呢?下面就来讨论一下。

        1、  主线程等待新线程先结束退出,主线程后退出。畸形执行。

        实例代码:

    #include "apue.h"
    #include <pthread.h>
    
    pthread_t ntid;//线程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);//使得主线程先退出
            printids("new thread");
    
            return ((void *)0);
    }
    
    int main(){
            int err;
            err = pthread_create(&ntid,NULL,thrfun,NULL);
    
            if(err != 0)
                    err_quit("can't create thread: %s\n",strerror(err));
            printids("main thread");
    
            sleep(1);//等待新线程先结束
    
            exit(0);
    }

        运行结果:

        线程和退出

        

        2、  进程先退出,新线程也会当即退出,系统清除所有资源。

        实例代码:

    #include "apue.h"
    #include <pthread.h>
    
    pthread_t ntid;//线程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);//使得主线程先退出
            printids("new thread");
    
            return ((void *)0);
    }
    
    int main(){
            int err;
            err = pthread_create(&ntid,NULL,thrfun,NULL);
    
            if(err != 0)
                    err_quit("can't create thread: %s\n",strerror(err));
            printids("main thread");
    
            //sleep(1);
    
            exit(0);//注意是进程(不是线程)退出
    }

        运行结果:

        线程和退出

        可以发现主线程退出后所创立的新线程也停止运行了。

        3、如果主线程调用了pthread_exit,那么它退出了,子线程也不会退出。

        实例代码:

    #include "apue.h"
    #include <pthread.h>
    
    pthread_t ntid;//线程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);//使得主线程先退出
            printids("new thread");
    
            return ((void *)0);
    }
    
    int main(){
            int err;
            err = pthread_create(&ntid,NULL,thrfun,NULL);
    
            if(err != 0)
                    err_quit("can't create thread: %s\n",strerror(err));
            printids("main thread");
    
            //sleep(1);
    
                pthread_exit(NULL);
    
            exit(0);
    }
        每日一道理
    岭上娇艳的鲜花,怎敌她美丽的容颜?山间清澈的小溪,怎比她纯洁的心灵?

        运行结果:

        线程和退出

        POSIX标准定义:

        When you program with POSIX Threads API,there is one thing about pthread_exit() that you may ignore for mistake. Insubroutines that complete normally, there is nothing special you have to dounless you want to pass a return code back using pthread_exit(). The completionwon't affect the other threads which were created by the main thread of thissubroutine. However, in main(), when the code has been executed to the end,there could leave a choice for you. If you want to kill all the threads thatmain() created before, you can dispense with calling any functions. But if you want to keep the process and all the other threadsexcept for the main thread alive after the exit of main(), then you can call pthread_exit()to realize it. And any files opened inside the main thread will remain openafter its termination.

        按照POSIX标准定义,当主线程在子线程终止之前调用pthread_exit()时,子线程是不会退出的。

        注意:这里在main函数中调用pthread_exit()只会是主线程退出,而进程并未退出。因此新线程继续执行而没有退出。

        我们可以在return 0;这条语句后面添加一条输出语句printf(“Mainthread has exited!\n”);来停止测试,输出结果不发生任何变化,说明这条语句没有被执行到。也就说明进程并未退出。

        因此:

        一个线程的退出不会影响另外一个线程。但是进程结束,所有线程也就结束了,所有资源会被回收。

        我们可以再写一个程序来停止验证:

        4、在创立的新线程B中再次创立新线程C,那么如果B先退出,那么C将会继续执行而不会退出。

        实例代码:

     

    #include "apue.h"
    #include<pthread.h>
    
    pthread_t ntid;//线程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 *thrfun2(void *arg){
            sleep(1);//使得创立它的主线程先退出
            printids("new thread of the new thread");
    
            return ((void *)0);
    }
    
    void *thrfun(void *arg){
            sleep(1);//使得主线程先退出
            printids("new thread");
            int err;
            err = pthread_create(&ntid,NULL,thrfun2,NULL);
    
            if(err != 0)
                    err_quit("can'tcreate thread: %s\n",strerror(err));
    
            return ((void *)0);
    }
    
    int main(){
            int err;
            err = pthread_create(&ntid,NULL,thrfun,NULL);
    
            if(err != 0)
                    err_quit("can'tcreate thread: %s\n",strerror(err));
            printids("main thread");
    
            //sleep(1);
    
            pthread_exit(NULL);
    
            printf("main thread has exited!\n");
    
            exit(0);
    }

    运行结果:

    线程和退出

    文章结束给大家分享下程序员的一些笑话语录: 一位程序员去海边游泳,由于水性不佳,游不回岸了,于是他挥着手臂,大声求.救:“F1,F1!”

    --------------------------------- 原创文章 By
    退出和学习
    ---------------------------------

  • 相关阅读:
    (转+原)android获取系统时间
    (转+原)VC编译错误:uafxcw.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) 已经在 LIBCMT.lib(new.obj) 中定义
    android的reference table的问题
    (原+转)Eclipse中Android调用OpenCv
    JAVA IO 字符流 FileReader FileWriter
    JAVA IO
    JAVA FIle类
    JAVA 泛型
    JAVA Collection工具类 Collections
    JAVA Map子接口之 HashMap
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/3100794.html
Copyright © 2020-2023  润新知