- 论坛徽章:
- 0
|
int pthread_key_create(pthread_key_t *key, void (*destr_function) (void *)) ;
其中,void (*destr_function)(void *)是什么意思?
我写了这么一个程序,怎么没有看到运行这个函数。
代码如下:
#include<pthread.h>;
#include<stdio.h>;
pthread_key_t key;
void echomsg(void *t)
{
printf("destructor excuted in thread %d, param=%d,", pthread_self(), (int *)t);
}
void *child1(void *arg)
{
int tid = pthread_self();
char buf[10];
memcpy(buf, "child1", 6);
printf("thread %d enter\n", tid);
pthread_setspecific(key, (void*)buf);
sleep(2);
printf("thread %d return %s\n", tid, pthread_getspecific(key));
sleep(2);
pthread_exit(0);
}
void *child2(void *arg)
{
int tid = pthread_self();
char buf[10];
memcpy(buf, "child2", 6);
printf("thread %d enter\n", tid);
pthread_setspecific(key, (void*)buf);
sleep(2);
printf("thread %d return %s \n", tid, pthread_getspecific(key));
sleep(2);
pthread_exit(0);
}
int main()
{
int tid1,tid2;
printf("hello\n" ;
pthread_key_create(&key, (void *)echomsg);
pthread_create(&tid1, NULL, child1, NULL);
pthread_create(&tid2, NULL, child2, NULL);
sleep(4);
// pthread_key_delete(key);
printf("main thread exit\n" ;
exit(0);
}
这个程序运行的时候,看不到echomsg函数中的打印。 请那位大哥解释一下,echomsg在什么时候运行? |
|