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

来源:互联网 发布:淘宝砗磲 编辑:程序博客网 时间:2024/06/05 08:18

线程分离和聚合

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 gothread_cb call

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

将pthread_detach改成pthread_join的运行结果:

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

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

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

总结

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

原创粉丝点击