- 论坛徽章:
- 0
|
- #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,(void *)tid);
- sleep(2);
- printf("thread %d returns %d\n",tid,pthread_getspecific(key));
- sleep(5);
- }
-
- void * child2(void *arg)
- {
- int tid=pthread_self();
- printf("thread %d enter\n",tid);
- pthread_setspecific(key,(void *)tid);
- sleep(1);
- printf("thread %d returns %d\n",tid,pthread_getspecific(key));
- sleep(5);
- }
- int main(void)
- {
- int tid1,tid2;
- printf("hello\n");
- pthread_key_create(&key,echomsg);
- pthread_create(&tid1,NULL,child1,NULL);
- pthread_create(&tid2,NULL,child2,NULL);
- sleep(10);
- pthread_key_delete(key);
- printf("main thread exit\n");
- return 0;
- }
复制代码
该代码执行结果并没有执行echomsg函数,在书上也谈到上述程序说是程序错开了两个线程私有数据的写入和读出时间,实在不明白这是什么原因,为什么没执行echomsg函数?谢谢!!! |
|