免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 2617 | 回复: 10
打印 上一主题 下一主题

请问各位高手,pthread_join函数是否必要? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-27 15:53 |只看该作者 |倒序浏览
我已经知道,为了防止僵尸进程,父进程会使用wait,或是交给init进程处理。   那么对于线程来说,有没有类似的事情?pthread_join函数是否必要?谢谢

论坛徽章:
0
2 [报告]
发表于 2011-12-27 15:58 |只看该作者
pthread_detach

论坛徽章:
1
射手座
日期:2014-08-04 16:49:43
3 [报告]
发表于 2011-12-27 17:06 |只看该作者
恩 分离线程比较好。。。

论坛徽章:
16
2015亚冠之吉达阿赫利
日期:2015-08-17 11:21:462015年迎新春徽章
日期:2015-03-04 09:58:11酉鸡
日期:2014-12-07 09:06:19水瓶座
日期:2014-11-04 14:23:29天秤座
日期:2014-03-02 08:57:52双鱼座
日期:2014-02-22 13:07:56午马
日期:2014-02-14 11:08:18双鱼座
日期:2014-02-13 11:09:37卯兔
日期:2014-02-06 15:10:34子鼠
日期:2014-01-20 14:48:19戌狗
日期:2013-12-19 09:37:46射手座
日期:2013-12-19 09:33:47
4 [报告]
发表于 2011-12-27 17:08 |只看该作者
回复 3# hanzhenlll


    如果程序中没有调用pthread_join或pthread_detach函数,会造成资源泄漏吗?
   
    还是当main thread 退出时,一切都被回收了?

论坛徽章:
0
5 [报告]
发表于 2011-12-27 17:20 |只看该作者
回复 4# embeddedlwp


The new thread terminates in one of the following ways:
* It calls pthread_exit(3), specifying an exit status value that is available to another thread in the same process that calls pthread_join(3).
* It returns from start_routine(). This is equivalent to calling pthread_exit(3) with the value supplied in the return statement.
* It is canceled (see pthread_cancel(3)).
* Any of the threads in the process calls exit(3), or the main thread performs a return from main(). This causes the termination of all threads in the process.


man pthread_create(3)

论坛徽章:
16
2015亚冠之吉达阿赫利
日期:2015-08-17 11:21:462015年迎新春徽章
日期:2015-03-04 09:58:11酉鸡
日期:2014-12-07 09:06:19水瓶座
日期:2014-11-04 14:23:29天秤座
日期:2014-03-02 08:57:52双鱼座
日期:2014-02-22 13:07:56午马
日期:2014-02-14 11:08:18双鱼座
日期:2014-02-13 11:09:37卯兔
日期:2014-02-06 15:10:34子鼠
日期:2014-01-20 14:48:19戌狗
日期:2013-12-19 09:37:46射手座
日期:2013-12-19 09:33:47
6 [报告]
发表于 2011-12-27 17:30 |只看该作者
回复 5# hbmhalley


    嘿嘿,我是想问如果pthread_create时没有设置detachstate,没有调用pthread_join函数,没有调用pthread_detach函数
    会不会像进程那样变成僵尸,也就是说会不会即使main thread退出了,依旧存在僵死线程?
    求指点!

论坛徽章:
2
天蝎座
日期:2014-03-28 10:18:052015年亚洲杯之乌兹别克斯坦
日期:2015-02-10 11:32:25
7 [报告]
发表于 2011-12-27 17:48 |只看该作者
不会僵,和主线程一起死了

论坛徽章:
0
8 [报告]
发表于 2011-12-27 17:55 |只看该作者
txdgtwpv 发表于 2011-12-27 17:48
不会僵,和主线程一起死了



是这个结论吗?

论坛徽章:
0
9 [报告]
发表于 2011-12-27 18:04 |只看该作者
回复 8# banbi1985


    main return前是僵着的.和进程一样
Failure to join with a thread that is joinable (i.e., one that is not detached), produces a "zombie thread". Avoid doing this, since each zombie thread consumes some system resources, and when enough zombie threads have accumulated, it will no longer be possible to create new threads (or processes).

    main return后就没了

论坛徽章:
2
天蝎座
日期:2014-03-28 10:18:052015年亚洲杯之乌兹别克斯坦
日期:2015-02-10 11:32:25
10 [报告]
发表于 2011-12-27 18:54 |只看该作者
banbi1985 发表于 2011-12-27 17:55
是这个结论吗?


没有detach就是这样的
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP