linux中pthread_join()与pthread_detach()详解

摘自:https://blog.csdn.net/weibo1230123/article/details/81410241

前言:
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)实例

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


结果:

去掉pthread_join ( mythread, NULL )

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


结果:

二: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)实例

 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
",thread1);
13 }
14 
15 exit (0) ;
16 }
17 void print_message_function( void *ptr )
18 { pthread_detach(pthread_self());
19 static int g;
20 printf("%d
", g++);
21 
22 pthread_exit(0) ;
23 }


结果:

//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;

原文地址:https://www.cnblogs.com/LiuYanYGZ/p/14611506.html