免费注册 查看新帖 |

Chinaunix

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

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

论坛徽章:
0
1 [报告]
发表于 2008-09-02 23:10 |显示全部楼层
突然发现我这也可以,可是我在编译LZ的代码的时候改了点东西。否则都编译不过阿。

其实sleep还是可以通过的哈,这个10有10S呢。肯定不短了。

#include <stdio.h>
#include <pthread.h>
pthread_key_t   key;
void * echomsg(int t)   //这里改了下
{
        printf("destructor excuted in thread %d,param=%d\n",pthread_self(),t);
}
void * child1(void *arg)
{
        int tid=pthread_self();
        printf("thread %d enter\n",&tid);
        pthread_setspecific(key,&tid);
        printf("thread %d returns %d\n",tid,pthread_getspecific(key));
      
}

int main(void)
{

        pthread_t tid1;   //还有这里
        printf("hello\n");

        pthread_key_create(&key,(void *)echomsg);   //AND这里
        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;
}

论坛徽章:
0
2 [报告]
发表于 2008-09-03 08:12 |显示全部楼层
原帖由 xi2008wang 于 2008-9-3 00:30 发表

等过了10秒后, 它不是会自己动醒过来


答案是肯定能醒来啊。。。

sleep()就是用来定时的,而pthread_join是要等待到指定的线程结束才可以继续执行的。否则就挂起吧。。。

论坛徽章:
0
3 [报告]
发表于 2008-09-03 09:12 |显示全部楼层
那你这个问题和sleep和pthread_join没有关系了。

是这个线程私有数据的内容,今天早上才看的书,忘了。T.T

同一个变量名,在每个线程里有不一样的值。

你在调用这个函数后面加个sleep(1)试试。↓
pthread_setspecific(key,(void *)tid);

论坛徽章:
0
4 [报告]
发表于 2008-09-03 09:38 |显示全部楼层
对不起,看来是我理解错LZ的意思了。我那个代码完全没有经过考虑,只是调试过,并能运行而已。

我找到个帖子。LZ你可以看看。我里面有写错东西。可是我不明白为什么我家里的编译不过去。可能改错了。

点这里

应该还是调用的时候错了。T.T

论坛徽章:
0
5 [报告]
发表于 2008-09-03 09:45 |显示全部楼层
原来是这样的啊。学习了。原来是时间太长了。呼呼。

论坛徽章:
0
6 [报告]
发表于 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);
}

...



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

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

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


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

论坛徽章:
0
8 [报告]
发表于 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(),大致可以推断出,调用析构函数的还是线程本身。

论坛徽章:
0
9 [报告]
发表于 2008-09-03 13:37 |显示全部楼层
pthread_setspecific(key,(void *)&tid);



问题在这边。。。

这边设置这个KEY的时候错了。@。@

应该这么写:

pthread_setspecific(key,(void *)tid);



呼呼。。。其实这个KEY赋值上去应该是自己的TID才是正确的。我们缺把地址给赋值进去了@。@

相关还是看  这里

[ 本帖最后由 benbenr 于 2008-9-3 13:38 编辑 ]

论坛徽章:
0
10 [报告]
发表于 2008-09-04 19:54 |显示全部楼层
原帖由 chenzhanyiczy 于 2008-9-4 16:53 发表


为什么int tid可以转换成void*,pthread_getspecifc取出来的却是tid的值


这边的void *是一个无类型的指针,任何类型都能转换成这个。应该就是存放这个所强制转换类型的地址。

不是在pthread_getspecific里获取,这个函数是把这个tid的值赋值给key。

在析构函数void echomsg(int t)这个里面的型参t就是key的值,是传递过来的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP