pthread-线程分离和聚合的差别

线程分离和聚合

posix线程支撑linux上的多线程使用
线程有两个截然相反的属性 分离和聚合
该属性分别由两个接口设置
聚合:
int pthread_join(pthread_t thread, void **retval);
分离:
int pthread_detach(pthread_t tid);

分离和聚合属性的差别

线程被设置为聚合后 会在原地等待这个线程的退出
设置为分离 则会继续按代码顺序运行
差别类似于阻塞和非阻塞的接口
阻塞则原地等地阻塞接口退出
非阻塞则继续按顺序运行

示例代码

 25 #include <pthread.h>$                                                            
 27 #include <stdio.h>$                                                              
 26 $                                                                                
 25 #define log(fmt,...)\$                                                           
 24           do {\$                                                                 
 23               printf(fmt,##__VA_ARGS__);\$                                       
 22           } while (0)$                                                           
 21 $                                                                                
 20 void *thread_cb(void *args) {$                                                   
 19     log("thread_cb call\n");$                                                    
 18     usleep(1000 * 1000);$                                                        
 17 }$                                                                               
 16 $                                                                                
 15 int main(int arg, char **argv) {$                                                
 14     pthread_t thread_t;$                                                         
 13 $                                                                                
 12     int ret = pthread_create(&thread_t, NULL, thread_cb, NULL);$                 
 11     if (ret < 0) {$                                                              
 10         log("failed to create thread.\n");$                                      
  9         return -1;$                                                              
  8     }$                                                                           
  7 $                                                                                
  6     //pthread_join(thread_t, NULL);$                                             
  5     pthread_detach(thread_t);$                                                   
  4     log("ready to go\n");$                                                       
  3 $                                                                                
  2     while(1);$                                                                   
  1     return 0;$                                                                   
29  }$  

设置detach的运行结果:

cx@alg-65:~/work$ ./a.out 
ready to go
thread_cb call

可以看到pthread_detach接口立刻退出并往下执行了
而线程在其后执行

将pthread_detach改成pthread_join的运行结果:

cx@alg-65:~/work$ ./a.out 
thread_cb call
ready to go

可以看到直到线程执行完毕之后 pthread_join才退出

程序退出前的while(1)是为了防止当线程设置为detach时
pthread_detach退出后立刻执行return 0 直接退出了整个程序
这样看上去就像是线程没有得到执行

总结

pthread_join 等待线程退出
pthread_detach “放养”线程
从参数上也可以看出 pthread_join可以获取线程的返回值
而pthread_detach不可以
这也侧面表明了两种属性的差别

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值