zoukankan      html  css  js  c++  java
  • 线程退出

    pthread_exit函数

    将单个线程退出

           void pthread_exit(void *retval);   参数:retval表示线程退出状态,通常传NULL

    思考:使用exit将指定线程退出,可以吗?                                                                         【pthrd_exit.c】

           结论:线程中,禁止使用exit函数,会导致进程内所有线程全部退出。

           在不添加sleep控制输出顺序的情况下。pthread_create在循环中,几乎瞬间创建5个线程,但只有第1个线程有机会输出(或者第2个也有,也可能没有,取决于内核调度)如果第3个线程执行了exit,将整个进程退出了,所以全部线程退出了。

           所以,多线程环境中,应尽量少用,或者不使用exit函数,取而代之使用pthread_exit函数,将单个线程退出。任何线程里exit导致进程退出,其他线程未工作结束,主控线程退出时不能return或exit。

    另注意,pthread_exit或者return返回的指针所指向的内存单元必须是全局的或者是用malloc分配的,不能在线程函数的栈上分配,因为当其它线程得到这个返回指针时线程函数已经退出了。

    【练习】:编写多线程程序,总结exit、return、pthread_exit各自退出效果。

           return:返回到调用者那里去。

           pthread_exit():将调用该函数的线程             

           exit: 将进程退出。

    /***
    exit.c
    ***/
    #include<stdio.h>
    #include<string.h>
    #include<unistd.h>
    #include<stdlib.h>
    #include<pthread.h>
    
    void  *thrd_func(void *arg)
    {
        printf("th thread: thread id = %lu, pid = %u
    ",pthread_self(),getpid());
        return NULL;
    }
    
    int main()
    {
        pthread_t tid;
        int ret,i;
    
       // for(i = 0; i < 5; i++)
        {
            ret = pthread_create(&tid,NULL,thrd_func,(void *)&i);
            if(0 != ret)
            {
                fprintf(stderr,"pthread_create error:%s
    ",strerror(ret));
                exit(1);
            }
        }
    
        printf("In main2: thread id = %lu,pid = %u
    ",pthread_self(),getpid());
        pthread_exit((void*)1);
    }

    运行结果:

    ubuntu1604@ubuntu:~/wangqinghe/linux/20190819$ ./exit

    In main2: thread id = 139958373693184,pid = 12073

    th thread: thread id = 139958365353728, pid = 12073

    /***
    tfn.c
    ***/
    #include<stdio.h>
    #include<string.h>
    #include<unistd.h>
    #include<stdlib.h>
    #include<pthread.h>
    
    void  *tfn(void *arg)
    {
        int i = *((int*)arg);
        printf("%dth thread: thread id = %lu, pid = %u
    ",i+1,pthread_self(),getpid());
        return NULL;
    }
    
    int main()
    {
        pthread_t tid;
        int ret,i;
    
        for(i = 0; i < 5; i++)
        {
            ret = pthread_create(&tid,NULL,tfn,(void *)&i);
            if(0 != ret)
            {
                fprintf(stderr,"pthread_create error:%s
    ",strerror(ret));
                exit(1);
            }
        }
    
        printf("In main2: thread id = %lu,pid = %u
    ",pthread_self(),getpid());
        pthread_exit(NULL);
    }

    ubuntu1604@ubuntu:~/wangqinghe/linux/20190819$ ./tfn

    In main2: thread id = 140606869358336,pid = 12102

    6th thread: thread id = 140606827448064, pid = 12102

    6th thread: thread id = 140606835840768, pid = 12102

    6th thread: thread id = 140606844233472, pid = 12102

    6th thread: thread id = 140606861018880, pid = 12102

    6th thread: thread id = 140606852626176, pid = 12102

  • 相关阅读:
    RESTful API 设计指南
    Lombok 安装配置及使用方法
    Python——urllib函数网络文件获取
    C与C++面试易出知识点
    编程一年
    Java练习1
    大整数求和
    PHP数组知识点整理
    CSS——NO.10(设置技巧)
    CSS——NO.9(颜色值和长度值)
  • 原文地址:https://www.cnblogs.com/wanghao-boke/p/11389699.html
Copyright © 2011-2022 走看看