pthread_key_create
2013-10-28 12:43
239 查看
原文地址:http://blog.csdn.net/yangzhiloveyou/article/details/8043573
/*
* int pthread_key_create(pthread_key_t *key, void (*destr_function) (void *))
*
* 该函数从TSD池中分配一项,将其值赋给key供以后访问使用。如果destr_function不为空,在线程退出(pthread_exit())时将以key所关联的数据为参数调用destr_function(),以释放分配的缓冲区。
*
* 注销一个TSD采用如下API:
* int pthread_key_delete(pthread_key_t key)
* 这个函数并不检查当前是否有线程正使用该TSD,也不会调用清理函数(destr_function),而只是将TSD释放以供下一次调用 pthread_key_create()使用。
*/
#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;
}
hello
thread -1210004592 enter
thread -1218397296 enter
thread -1218397296 returns -1218397296
thread -1210004592 returns -1210004592
destructor excuted in thread -1218397296,param=-1218397296
destructor excuted in thread -1210004592,param=-1210004592
main thread exit
/*
* int pthread_key_create(pthread_key_t *key, void (*destr_function) (void *))
*
* 该函数从TSD池中分配一项,将其值赋给key供以后访问使用。如果destr_function不为空,在线程退出(pthread_exit())时将以key所关联的数据为参数调用destr_function(),以释放分配的缓冲区。
*
* 注销一个TSD采用如下API:
* int pthread_key_delete(pthread_key_t key)
* 这个函数并不检查当前是否有线程正使用该TSD,也不会调用清理函数(destr_function),而只是将TSD释放以供下一次调用 pthread_key_create()使用。
*/
#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;
}
hello
thread -1210004592 enter
thread -1218397296 enter
thread -1218397296 returns -1218397296
thread -1210004592 returns -1210004592
destructor excuted in thread -1218397296,param=-1218397296
destructor excuted in thread -1210004592,param=-1210004592
main thread exit
相关文章推荐
- linux pthread_once和pthread_atfork,pthread_key_create
- TSD相关的 4 个函数: pthread_key_create(); pthread_key_delete(); pthread_getspecific(); pthread_sets
- POSIX线程专有数据的空间释放问题,pthread_key_create
- pthread_key_t和pthread_key_create()的使用
- 多线程私有数据pthread_key_create
- POSIX线程专有数据的空间释放问题,pthread_key_create
- liblog4cpp.so: undefined reference to `pthread_key_create'
- pthread_key_t和pthread_key_create()详解
- POSIX线程专有数据的空间释放问题,pthread_key_create……(
- pthread_key_t和pthread_key_create()的使用
- Linux多线程pthread_key_create
- pthread_key_t和pthread_key_create()详解
- pthread_key_t和pthread_key_create()详解
- pthread_once、pthread_key_create和pthread_getspecific和pthread_setspecific
- pthread_key_t和pthread_key_create()详解
- pthread_key_t和pthread_key_create()详解
- pthread_key_create用法
- TLS2之pthread_key_create
- pthread_once和pthread_key_create的用法演示
- 线程存储pthread_key_create