免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: wangxiaoguang
打印 上一主题 下一主题

[C] sleep和pthread_join的区别 [复制链接]

论坛徽章:
0
21 [报告]
发表于 2008-09-03 10:24 |只看该作者
原帖由 wangxiaoguang 于 2008-9-3 09:43 发表
终于弄懂了,析构函数是在(子)线程结束后由调用pthread_key_create的线程调用析构函数,这里是主线程,所以要用join等待(子)线程结束后,主线程调用析构函数。而sleep()如果时间过长,(子)线程结束,而 ...


不可能由调用pthread_key_create的线程调用析构函数(这里是主线程),应该是内核调的,而且仍然在退出的线程的上下文中.
之后发生上下文切换,如果用join则回到主线程,如果用sleep,则等超时再回到主线程.

论坛徽章:
0
22 [报告]
发表于 2008-09-03 10:28 |只看该作者
原帖由 huangwei0413 于 2008-9-3 10:20 发表


#include
#include

pthread_key_t   key;

void echomsg(void* t)
{
        printf("destructor excuted in thread %08x,param=%08x\n",(unsigned int)pthread_self(),(unsigned int)t);
}

...



还是调用函数用错了。这个才是正确的。呼。

论坛徽章:
1
申猴
日期:2014-02-11 14:50:31
23 [报告]
发表于 2008-09-03 10:40 |只看该作者
原帖由 wangxiaoguang 于 2008-9-3 09:43 发表
终于弄懂了,析构函数是在(子)线程结束后由调用pthread_key_create的线程调用析构函数,这里是主线程,所以要用join等待(子)线程结束后,主线程调用析构函数。而sleep()如果时间过长,(子)线程结束,而 ...


不太可能吧,析构函数是在子线程结束后调用的,而不是主线程

论坛徽章:
0
24 [报告]
发表于 2008-09-03 11:08 |只看该作者
从运行结果中可以看出是调用pthread_key_create()的线程调用析构函数,这里是主线程

析构函数中的pthread_self()的值是主线程的tid

论坛徽章:
0
25 [报告]
发表于 2008-09-03 11:10 |只看该作者
原帖由 wangxiaoguang 于 2008-9-3 11:08 发表
从运行结果中可以看出是调用pthread_key_create()的线程调用析构函数,这里是主线程

析构函数中的pthread_self()的值是主线程的tid


不对啦。好好看看前面那个运行。明明是新建的线程的TID。

论坛徽章:
0
26 [报告]
发表于 2008-09-03 11:17 |只看该作者
原帖由 wangxiaoguang 于 2008-9-3 11:08 发表
从运行结果中可以看出是调用pthread_key_create()的线程调用析构函数,这里是主线程

析构函数中的pthread_self()的值是主线程的tid


析构函数中的pthread_self()的值是退出的那个线程的ID,请看清楚.

另外这个析构函数有两个问题:
1,setspecific注册的参数为一个栈上的地址,这里只是打印,所以没有问题,如果要访问就会有问题;
2,析构函数执行时,那个线程已经退出,这个时候再用pthread_self()可能有问题,这里正好正确,应该是巧合;

论坛徽章:
0
27 [报告]
发表于 2008-09-03 11:22 |只看该作者
在我的机器上不是这样的结果,而是下面的结果,唉,太郁闷了
#include <stdio.h>
#include <pthread.h>

pthread_key_t   key;

void echomsg(void* t)
{
        printf("destructor excuted in thread %08x,param=%08x\n",(unsigned int)pthread_self(),(unsigned int)t);
}

void * child1(void *arg)
{
        pthread_t tid = pthread_self();
        printf("thread %08x enter\n",(unsigned int)tid);
        pthread_setspecific(key,(void *)&tid);
        printf("thread %08x returns %08x\n",tid,pthread_getspecific(key));

}

int main(void)
{

        pthread_t tid = pthread_self();
        printf("mainthread %08x enter\n",(unsigned int)tid);

        pthread_t tid1;
        printf("hello\n");

        pthread_key_create(&key,echomsg);
        pthread_create(&tid1,NULL,child1,NULL);
        //sleep(10);//.....echomsg(int t).....

        pthread_join(tid1,NULL);//....echomsg(int t).....


        pthread_key_delete(key);
        printf("main thread exit\n");
        return 0;
}
mainthread 40041280 enter
hello
thread 40841cc0 enter
thread 40841cc0 returns 408418b0
destructor excuted in thread 40041280,param=408418b0
main thread exit

论坛徽章:
0
28 [报告]
发表于 2008-09-03 11:39 |只看该作者
原帖由 wangxiaoguang 于 2008-9-3 11:22 发表
在我的机器上不是这样的结果,而是下面的结果,唉,太郁闷了
#include
#include

pthread_key_t   key;

void echomsg(void* t)
{
        printf("destructor excuted in thread %08x,param=%08x\n" ...


什么平台,不会是cygwin吧

论坛徽章:
0
29 [报告]
发表于 2008-09-03 12:37 |只看该作者
我用的vmware,应该不是这个原因吧

析构函数不应该是子线程调用的吧,子线程结束之后才会调用析构函数啊

论坛徽章:
0
30 [报告]
发表于 2008-09-03 13:09 |只看该作者
原帖由 wangxiaoguang 于 2008-9-3 11:22 发表
在我的机器上不是这样的结果,而是下面的结果,唉,太郁闷了
#include
#include

pthread_key_t   key;

void echomsg(void* t)
{
        printf("destructor excuted in thread %08x,param=%08x\n" ...



有没有发现:
mainthread 40041280 enter
hello
thread 40841cc0 enter
thread 40841cc0 returns 408418b0
destructor excuted in thread 40041280,param=408418b0
main thread exit


在这里主线程的TID和实际的TID居然不一样。

我刚刚GDB了一下,在echomsg的这个函数的参数t,其实就是把主线程的TID给传入了进去。

莫非,在调用析构函数的时候就是把主线程的TID作为参数传递进去的。

在析构函数中的输出pthread_self(),大致可以推断出,调用析构函数的还是线程本身。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP