pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);和pthread_detach(pthread_self()); 有区别吗。怎么我执行的效果不一样

berniechen0123 2008-09-11 10:41:37
#include<stdio.h>
#include<unistd.h>
#include<pthread.h>

void* task1(void*);
void* task2(void*);

int main()
{
pthread_t pid1, pid2;
pthread_attr_t attr;
int ret;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pthread_create(&pid1, &attr, task1, NULL);
ret=pthread_join(pid1, NULL);
printf("ret=%d\n", ret);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
pthread_create(&pid2, &attr, task2, NULL);
ret=pthread_join(pid2, NULL);
printf("ret=%d\n", ret);
return 1;
}

void* task1(void*)
{
printf("task1\n");
pthread_exit(NULL);
}

void* task2(void*)
{
pthread_detach(pthread_self());
printf("task2\n");
pthread_exit(NULL);
}


执行结果
ret=22
task1
task2
ret=0
第一个phread_join() 没有等待直接返回了
第二个phread_join() 等待,从返回值看执行成功了。
请大家帮忙看看
...全文
2860 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
berniechen0123 2008-09-12
  • 打赏
  • 举报
回复
已经解决了。感谢各位。原因很简单
After pthread_detach completes, subsequent attempts to perform
pthread_join on th will fail. If another thread is already joining the
thread at the time pthread_detach is called, pthread_detach does
nothing and leaves th in the joinable state.

pthread_join在pthread_detach之前执行了。
berniechen0123 2008-09-12
  • 打赏
  • 举报
回复
昨晚我试过了,pthread_detach(pthread_self()); 的返回值是0,
快乐田伯光 2008-09-12
  • 打赏
  • 举报
回复
那你看看pthread_detach(pthread_self())的返回值看看是不是成功的,因为有可能是主线程选执行join了这个线程,导致这个线程detach不成功。猜的,你试试.

[Quote=引用 3 楼 berniechen0123 的回复:]
我可能没说清楚,请注意看,我第二个线程里有这样 一句pthread_detach(pthread_self());
void* task2(void*)
{
pthread_detach(pthread_self());
printf("task2\n");
pthread_exit(NULL);
}

所以我觉得第二个pthread_join也应该失败。可它成功了。我觉得很奇怪。第一个我没问题
[/Quote]
快乐田伯光 2008-09-12
  • 打赏
  • 举报
回复
FC5下man pthread_detach的结果
RETURN VALUE
If the call succeeds, pthread_detach() shall return 0; otherwise, an error number
shall be returned to indicate the error.

看这个意思,应该是没有detach成功,应该是不能返回0,但我写了个程序测试,确实如果先join了,detach也返回0,看来这个man的解释不是很全面
unilgr 2008-09-12
  • 打赏
  • 举报
回复
第一种情况:
Linux/Unix的man都会有类似的说明:
int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);
DESCRIPTION
The detachstate attribute controls whether the thread is created in a detached state. If the thread is created
detached, then use of the ID of the newly created thread by the pthread_detach() or pthread_join() function is an error
问题比较明显,楼上几位也说清楚了

第二种情况:
这是正确的用法!只不过线程属性是JOINABLE时,一般pthread_detach用在线程注册的清理函数中
void pthread_cleanup_push(void (*routine)(void*), void *arg),进行动态detach,比如线程被cancel时,就能调用,而且cancel属性默认都是deferred, 所以调用pthread_join仍然很合理,可以准确知道线程的退出时间。但是你现在的用法也不会返回错误。这就是 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)不能完全替代pthread_detach的原因



快乐田伯光 2008-09-12
  • 打赏
  • 举报
回复
4楼就是这个意思啊,因为先join了, 所以detach失败。只是我以为先join了的线程,detach会返回错误。
berniechen0123 2008-09-11
  • 打赏
  • 举报
回复
我可能没说清楚,请注意看,我第二个线程里有这样 一句pthread_detach(pthread_self());
void* task2(void*)
{
pthread_detach(pthread_self());
printf("task2\n");
pthread_exit(NULL);
}

所以我觉得第二个pthread_join也应该失败。可它成功了。我觉得很奇怪。第一个我没问题
快乐田伯光 2008-09-11
  • 打赏
  • 举报
回复
pthread_join()对象必须是未分离线程,你的第一个是join一个分离进程,所以没有返回0,面是返回了一个错误值
bshawk 2008-09-11
  • 打赏
  • 举报
回复
第一个当然不等待,你设置了它的属性为PTHREAD_CREATE_DETACHED! detecth什么意思? 很不专业的说,就是它独立(脱离)出去了,它可以自生自灭了,父线程不用理它了。

23,124

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧