前言:

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线程就会自动退出。省去了给线程擦屁股的麻烦。

例子:

  1. pthread_t tid;
  2. int status = pthread_create(&tid, NULL, ThreadFunc, NULL);
  3. if(status != 0)
  4. {
  5. perror("pthread_create error");
  6. }
  7. pthread_detach(tid);

一、pthread_create()

函数简介
  pthread_create是UNIX环境创建线程函数
头文件
  #include
函数声明
  int pthread_create(pthread_t restrict tidp,const pthread_attr_t restrict_attr,voidstart_rtn)(void),void restrict arg);
返回值
  若成功则返回0,否则返回出错编号
参数
  第一个参数为指向线程标识符的指针。
  第二个参数用来设置线程属性。
  第三个参数是线程运行函数的地址。
  最后一个参数是运行函数的参数。
注意
  在编译时注意加上-lpthread参数,以调用静态链接库。因为pthread并非Linux系统的默认库。

二、pthread_join()

函数说明:
pthread_join()即是子线程合入主线程,主线程阻塞等待子线程结束,然后回收子线程资源。
头文件
#include
函数定义
int pthread_join(pthread_t thread, void retval);
描述 :
pthread_join()函数,以阻塞的方式等待thread指定的线程结束。当函数返回时,被等待线程的资源被收回。如果线程已经结束,那么该函数会立即返回。并且thread指定的线程必须是joinable的。
参数 :
thread: 线程标识符,即线程ID,标识唯一线程。
retval: 用户定义的指针,用来存储被等待线程的返回值。
返回值 :**
0代表成功。 失败,返回的则是错误号。

(3)实例

  1. #include <pthread.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <stdio.h>
  5. void *thread_function(void *arg)
  6. {
  7. int i;
  8. for ( i=0; i<8; i++)
  9. {
  10. printf("Thread working...! %d \n",i);
  11. sleep(1);
  12. }
  13. return NULL;
  14. }
  15. int main(void)
  16. {
  17. pthread_t mythread;
  18. if ( pthread_create( &mythread, NULL, thread_function, NULL) )
  19. {
  20. printf("error creating thread.");
  21. abort();
  22. }
  23. if ( pthread_join ( mythread, NULL ) )
  24. {
  25. printf("error join thread.");
  26. abort();
  27. }
  28. printf("thread done! \n");
  29. exit(0);
  30. }

结果:
linux中pthread_join()与pthread_detach()详解 - 图1

去掉pthread_join ( mythread, NULL )

  1. #include <stdlib.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. void *thread_function(void *arg)
  5. {
  6. int i;
  7. for ( i=0; i<8; i++)
  8. {
  9. printf("Thread working...! %d \n",i);
  10. sleep(1);
  11. }
  12. return NULL;
  13. }
  14. int main(void)
  15. {
  16. pthread_t mythread;
  17. if ( pthread_create( &mythread, NULL, thread_function, NULL) )
  18. {
  19. printf("error creating thread.");
  20. abort();
  21. }
  22. /*
  23. if ( pthread_join ( mythread, NULL ) )
  24. {
  25. printf("error join thread.");
  26. abort();
  27. }
  28. */
  29. printf("thread done! \n");
  30. exit(0);
  31. }

结果:
linux中pthread_join()与pthread_detach()详解 - 图2

三、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非linux系统的默认库, 需手动链接-线程库 -lpthread
4)参数:tid:线程标识符
5)返回值:pthread_detach() 在调用成功完成之后返回零。其他任何返回值都表示出现了错误。如果检测到以下任一情况,pthread_detach()将失败并返回相应的值。
EINVAL:tid是分离线程
ESRCH:tid不是当前进程中有效的为分离线程
(3)实例

  1. #include <stdlib.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. void print_message_function( void *ptr );
  5. main ( )
  6. {
  7. pthread_t thread1;
  8. pthread_create(&thread1,NULL,(void *)&print_message_function,(void *)0);
  9. int i;
  10. for(i=0;i<5;i++)
  11. {
  12. printf("%d\n",thread1);
  13. }
  14. exit (0) ;
  15. }
  16. void print_message_function( void *ptr )
  17. { pthread_detach(pthread_self());
  18. static int g;
  19. printf("%d\n", g++);
  20. pthread_exit(0) ;
  21. }

结果:
linux中pthread_join()与pthread_detach()详解 - 图3

  1. pthread_detach(threadid)函数的功能是使线程IDthreadid的线程处于分离状态,一旦线程处于分离状态,该线程终止时底 层资源立即被回收;否则终止子线程的状态会一直保存(占用系统资源)直到主线程调用pthread_join(threadid,NULL)获取线程的退 出状态。通常是主线程使用pthread_create()创建子线程以后,一般可以调用pthread_detach(threadid)分离刚刚创建的子线程,这里的threadid是指子线程的threadid;如此以来,该子线程止时底层资源立即被回收;被创建的子线程也可以自己分离自己,子线程调用pthread_detach(pthread_self())就是分离自己,因为pthread_self()这个函数返回的就是自己本身的线程ID

引用:

linux中pthread_join()与pthread_detach()详解