免费注册 查看新帖 |

Chinaunix

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

多线程pthread_detach的奇怪问题,求详细解答 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-10-17 11:23 |只看该作者 |倒序浏览
在unix高级编程2中,关于pthread_detach有以下描述:
a thread's termination status is retained until pthread_join is called for that thread. A thread's underlying storage can be reclaimed immediately on termination if that thread has been detached. When a thread is detached, the pthread_join function can't be used to wait for its termination status. A call to pthread_join for a detached thread will fail, returning EINVAL. We can detach a thread by calling pthread_detach.

因此我想先对一个子线程调用pthread_detach,然后再对其pthread_join,测试一下是否会返回EINVAL值。但结果有点迷惑不解。
#include <pthread.h>
#include <iostream>
#include <errno.h>
using namespace std;

pthread_t ntid;

void
printids(const char *s)
{
&nbsp;&nbsp;&nbsp;&nbsp;pid_t      pid;
&nbsp;&nbsp;&nbsp;&nbsp;pthread_t  tid;

&nbsp;&nbsp;&nbsp;&nbsp;pid = getpid();
&nbsp;&nbsp;&nbsp;&nbsp;tid = pthread_self();
&nbsp;&nbsp;&nbsp;&nbsp;printf("%s pid %u tid %u (0x%x)\n", s, (unsigned int)pid,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(unsigned int)tid, (unsigned int)tid);
}

void *
thr_fn(void *arg)
{
&nbsp;&nbsp;&nbsp;&nbsp;printids("new thread: ");
&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;int err = pthread_detach(pthread_self());
&nbsp;&nbsp;&nbsp;&nbsp;if (err != 0)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("can't detach thread: err = %d, %s\n", err, strerror(err));
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;return((void *)0);
}

int
main(void)
{
&nbsp;&nbsp;&nbsp;&nbsp;int     err;

&nbsp;&nbsp;&nbsp;&nbsp;err = pthread_create(&ntid, NULL, thr_fn, NULL);
&nbsp;&nbsp;&nbsp;&nbsp;if (err != 0)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("can't create thread: %s\n", strerror(err));
&nbsp;&nbsp;&nbsp;&nbsp;printids("main thread:");
&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;sleep(1);    //这个sleep非常重要,是否存在会产生不同的结果

&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;err = pthread_join(ntid, NULL);
&nbsp;&nbsp;&nbsp;&nbsp;if (err != 0)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("can't join thread: err = %d, %s\n", err, strerror(err));
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;return 0;
}


当main函数中的sleep(1)语句存在时,回来的结果正确为:
main thread: pid 573810 tid 1 (0x1)
new thread:  pid 573810 tid 258 (0x102)
can't join thread: err = 22, Invalid argument

但注释该sleep(1)时,程序的结果会是:
main thread: pid 573810 tid 1 (0x1)
new thread:  pid 573810 tid 258 (0x102)
也就是说pthread_join正确处理了,返回了0.

我猜测是否是因为子线程在pthread_detach后,资料还未来得及释放,join函数就开始调用了,才导致该结果?

论坛徽章:
0
2 [报告]
发表于 2009-10-17 12:27 |只看该作者

回复 #1 rowdzfda 的帖子

是的,所以一般的做法是创建的线程发一个信号,创建线程等待这个信号后,再去做别的

论坛徽章:
0
3 [报告]
发表于 2009-10-17 12:37 |只看该作者
int err = pthread_detach(pthread_self());
在这句后打印个printf("aaa");,前面打印个printf("aaa");试试。
。。。。正在开机,我测试下

论坛徽章:
0
4 [报告]
发表于 2009-10-17 12:43 |只看该作者
如果不sleep,detach 在join后执行。join后,再detach?...额。。为什么detach不出错?
sleep(0.1)的话,主线程就一直等待子线程退出了。。。比较有意思。

[ 本帖最后由 peidright 于 2009-10-17 13:15 编辑 ]

论坛徽章:
0
5 [报告]
发表于 2009-10-17 18:54 |只看该作者

回复 #4 peidright 的帖子

恩,不sleep,detach不会报错,仍然能正常detach。对这个结果非常的迷惑,想弄清楚具体原因
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP