- 论坛徽章:
- 0
|
突然发现我这也可以,可是我在编译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;
} |
|
|