首页 新闻 论坛 群组 Blog 文档 下载 读书 Tag 网摘 搜索 .NET Java 游戏 视频 人才 外包 培训 数据库 书店 程序员
中国软件网
欢迎您:游客 | 登录 注册 帮助
  • pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);和pthread_detach(pthread_self()); 有区别吗。怎么我执行的效果不一样 [已结帖,结帖人:berniechen0123]
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • berniechen0123
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    • 结帖率:
    发表于:2008-09-11 22:41:37 楼主
    C/C++ code
    #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() 等待,从返回值看执行成功了。
    请大家帮忙看看
    20  修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • bshawk
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    • 2

    发表于:2008-09-11 22:46:391楼 得分:0
    第一个当然不等待,你设置了它的属性为PTHREAD_CREATE_DETACHED! detecth什么意思? 很不专业的说,就是它独立(脱离)出去了,它可以自生自灭了,父线程不用理它了。
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • guosha
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    • 3

    发表于:2008-09-11 23:00:532楼 得分:0
    pthread_join()对象必须是未分离线程,你的第一个是join一个分离进程,所以没有返回0,面是返回了一个错误值
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • berniechen0123
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-09-11 23:18:313楼 得分:0
    我可能没说清楚,请注意看,我第二个线程里有这样 一句pthread_detach(pthread_self());
    void* task2(void*)
    {
        pthread_detach(pthread_self());
        printf("task2\n");
        pthread_exit(NULL);
    }

    所以我觉得第二个pthread_join也应该失败。可它成功了。我觉得很奇怪。第一个我没问题
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • guosha
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    • 3

    发表于:2008-09-12 08:48:314楼 得分:15
    那你看看pthread_detach(pthread_self())的返回值看看是不是成功的,因为有可能是主线程选执行join了这个线程,导致这个线程detach不成功。猜的,你试试.

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

    所以我觉得第二个pthread_join也应该失败。可它成功了。我觉得很奇怪。第一个我没问题
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • berniechen0123
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-09-12 12:34:385楼 得分:0
    昨晚我试过了,pthread_detach(pthread_self()); 的返回值是0,
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • berniechen0123
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-09-12 12:48:056楼 得分:0
    已经解决了。感谢各位。原因很简单
    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之前执行了。
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • guosha
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    • 3

    发表于:2008-09-12 13:17:097楼 得分:0
    4楼就是这个意思啊,因为先join了, 所以detach失败。只是我以为先join了的线程,detach会返回错误。
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • unilgr
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-09-12 13:28:398楼 得分:5
    第一种情况:
    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的原因


    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • guosha
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    • 3

    发表于:2008-09-12 13:32:429楼 得分:0
    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的解释不是很全面
    修改 删除 举报 引用 回复