• linux中pthread_join()与pthread_detach()详解


    前言:
    1.linux线程执行和windows不同,pthread有两种状态joinable状态和unjoinable状态,如果线程是joinable状态,当线程函数自己返回退出时或pthread_exit时都不会释放线程所占用堆栈和线程描述符(总计8K多)。只有当你调用了pthread_join之后这些资源才会被释放。若是unjoinable状态的线程,这些资源在线程函数退出时或pthread_exit时自动会被释放。
    2.unjoinable属性可以在pthread_create时指定,或在线程创建后在线程中pthread_detach自己, 如:pthread_detach(pthread_self()),将状态改为unjoinable状态,确保资源的释放。或者将线程置为 joinable,然后适时调用pthread_join.
    3.其实简单的说就是在线程函数头加上 pthread_detach(pthread_self())的话,线程状态改变,在函数尾部直接 pthread_exit线程就会自动退出。省去了给线程擦屁股的麻烦。
    eg:

     pthread_t tid;
     int status = pthread_create(&tid, NULL, ThreadFunc, NULL);
     if(status != 0)
     {
      perror("pthread_create error");
     }
     pthread_detach(tid);
    一:pthread_join()
    (1)pthread_join()即是子线程合入主线程,主线程阻塞等待子线程结束,然后回收子线程资源。

    (2)函数说明

    1)头文件 : #include <pthread.h>

    2)函数定义: int pthread_join(pthread_t thread, void **retval);

    3)描述 :pthread_join()函数,以阻塞的方式等待thread指定的线程结束。当函数返回时,被等待线程的资源被收回。如果线程已经结束,那么该函数会立即返回。并且thread指定的线程必须是joinable的。

    4)参数 :thread: 线程标识符,即线程ID,标识唯一线程。retval: 用户定义的指针,用来存储被等待线程的返回值。

    5)返回值 : 0代表成功。 失败,返回的则是错误号。

    (3)实例

    #include <pthread.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <stdio.h>

    void *thread_function(void *arg)
    {
    int i;
    for ( i=0; i<8; i++)
    {
    printf("Thread working...! %d ",i);
    sleep(1);
    }
    return NULL;
    }

    int main(void)
    {
    pthread_t mythread;

    if ( pthread_create( &mythread, NULL, thread_function, NULL) )
    {
    printf("error creating thread.");
    abort();
    }
    if ( pthread_join ( mythread, NULL ) )
    {
    printf("error join thread.");
    abort();
    }

    printf("thread done! ");
    exit(0);
    }
    结果:

    去掉pthread_join ( mythread, NULL )

    #include <stdlib.h>
    #include <unistd.h>
    #include <stdio.h>

    void *thread_function(void *arg)
    {
    int i;
    for ( i=0; i<8; i++)
    {
    printf("Thread working...! %d ",i);
    sleep(1);
    }
    return NULL;
    }

    int main(void)
    {
    pthread_t mythread;

    if ( pthread_create( &mythread, NULL, thread_function, NULL) )
    {
    printf("error creating thread.");
    abort();
    }
    /*
    if ( pthread_join ( mythread, NULL ) )
    {
    printf("error join thread.");
    abort();
    }
    */

    printf("thread done! ");
    exit(0);
    }
    结果:

    二:pthread_detach()
    (1)pthread_detach()即主线程与子线程分离,子线程结束后,资源自动回收。

    (2)函数说明

    1)函数原型:int pthread_detach(pthread_t tid);

    2)功能:pthread_join()函数的替代函数,可回收创建时detachstate属性设置为PTHREAD_CREATE_JOINABLE的线程的存储空间。该函数不会阻塞父线程。pthread_join()函数用于只是应用程序在线程tid终止时回收其存储空间。如果tid尚未终止,pthread_detach()不会终止该线程。当然pthread_detach(pthread_self())也是可以得

    3)头文件:#include <pthread.h>     pthread非linux系统的默认库, 需手动链接-线程库 -lpthread

    4)参数:tid:线程标识符

    5)返回值:pthread_detach() 在调用成功完成之后返回零。其他任何返回值都表示出现了错误。如果检测到以下任一情况,pthread_detach()将失败并返回相应的值。

    EINVAL:tid是分离线程

    ESRCH:tid不是当前进程中有效的为分离线程

    (3)实例

    #include <stdlib.h>
    #include <unistd.h>
    #include <stdio.h>
    void print_message_function( void *ptr );
    main ( )
    {
    pthread_t thread1;
    pthread_create(&thread1,NULL,(void *)&print_message_function,(void *)0);
    int i;
    for(i=0;i<5;i++)
    {
    printf("%d ",thread1);
    }

    exit (0) ;
    }
    void print_message_function( void *ptr )
    { pthread_detach(pthread_self());
    static int g;
    printf("%d ", g++);

    pthread_exit(0) ;
    }
    结果:

    //pthread_detach(pthread_self());
    //使线成分离出来。当这个线程执行完成任务后释放释放资源。不然它会保留退出状态,等待别人来取。
    pthread_detach(threadid)和pthread_detach(pthread_self())没有什么区别吧!有很严格的区别吗???如果非要讲区别不可,我觉得应该是调用他们的线程不同。
          pthread_detach(threadid)函数的功能是使线程ID为threadid的线程处于分离状态,一旦线程处于分离状态,该线程终止时底 层资源立即被回收;否则终止子线程的状态会一直保存(占用系统资源)直到主线程调用pthread_join(threadid,NULL)获取线程的退 出状态。通常是主线程使用pthread_create()创建子线程以后,一般可以调用pthread_detach(threadid)分离刚刚创建的子线程,这里的threadid是指子线程的threadid;如此以来,该子线程止时底层资源立即被回收;被创建的子线程也可以自己分离自己,子线程调用pthread_detach(pthread_self())就是分离自己,因为pthread_self()这个函数返回的就是自己本身的线程ID;
    ---------------------
    作者:魏波-
    来源:CSDN
    原文:https://blog.csdn.net/weibo1230123/article/details/81410241
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    python3去除字符串中括号及括号里面的内容
    [机器学习]-朴素贝叶斯-最简单的入门实战例子
    [机器学习]-K近邻-最简单的入门实战例子
    [机器学习]-决策树-最简单的入门实战例子
    python3 通过qq邮箱定时发送邮件
    [代码仓库]Python3多线程编程
    [代码仓库]python常用散列函数
    [代码仓库]RSA算法
    [代码仓库]DES加密算法
    [学习记录]tensorflow超简单步骤使用
  • 原文地址:https://www.cnblogs.com/bully/p/11047440.html
Copyright © 2020-2023  润新知