TSD相关的 4 个函数: pthread_key_create(); pthread_key_delete(); pthread_getspecific(); pthread_sets
2012-06-15 11:08
666 查看
在多线程程序中,经常要用全局变量来实现多个函数间的数据共享。由于数据空间是共享的,因此全局变量也为所有进程共有。但有时应用程序设计中必要提供线程私有的全局变量,这个变量仅在线程中有效,但却可以跨过多个函数访问。
比如在程序里可能需要每个线程维护一个链表,而会使用相同的函数来操作这个链表,最简单的方法就是使用同名而不同变量地址的线程相关数据结构。这样的数据结构可以由 Posix 线程库维护,成为线程私有数据 (Thread-specific Data,或称为 TSD)。
这里主要测试和线程私有数据有关的 4 个函数:
pthread_key_create();
pthread_key_delete();
pthread_getspecific();
pthread_setspecific();
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
pthread_key_t key;
struct
test_struct {
int
i;
float
k;
};
void
*child1 (void*arg)
{
struct
test_struct struct_data;
struct_data.i=
10;
struct_data.k=
3.1415;
pthread_setspecific (key,&struct_data);
printf ("结构体struct_data的地址为 0x%p\n",&(struct_data));
printf ("child1 中 pthread_getspecific(key)返回的指针为:0x%p\n", (structtest_struct
*)pthread_getspecific(key));
printf ("利用 pthread_getspecific(key)打印 child1 线程中与key关联的结构体中成员值:\nstruct_data.i:%d\nstruct_data.k:
%f\n", ((structtest_struct
*)pthread_getspecific (key))->i, ((struct
test_struct *)pthread_getspecific(key))->k);
printf ("------------------------------------------------------\n");
}
void
*child2 (void*arg)
{
int
temp = 20;
sleep (2);
printf ("child2 中变量 temp 的地址为 0x%p\n", &temp);
pthread_setspecific (key,&temp);
printf ("child2 中 pthread_getspecific(key)返回的指针为:0x%p\n", (int*)pthread_getspecific(key));
printf ("利用 pthread_getspecific(key)打印 child2 线程中与key关联的整型变量temp 值:%d\n",*((int*)pthread_getspecific(key)));
}
int
main (void)
{
pthread_t tid1,tid2;
pthread_key_create (&key,NULL);
pthread_create (&tid1,NULL, (void*)child1,NULL);
pthread_create (&tid2,NULL, (void*)child2,NULL);
pthread_join (tid1,NULL);
pthread_join (tid2,NULL);
pthread_key_delete (key);
return (0);
}
运行与输出:
./pthread_key
结构体struct_data的地址为 0x0xb7699388
child1 中 pthread_getspecific(key)返回的指针为:0x0xb7699388
利用 pthread_getspecific(key)打印 child1 线程中与key关联的结构体中成员值:
struct_data.i:10
struct_data.k: 3.141500
------------------------------------------------------
child2 中变量 temp 的地址为 0x0xb6e9838c
child2 中 pthread_getspecific(key)返回的指针为:0x0xb6e9838c
由输出可见,pthread_getspecific() 返回的是与key 相关联数据的指针。需要注意的是,在利用这个返回的指针时,它首先是 void 类型的,它虽然指向关联的数据地址处,但并不知道指向的数据类型,所以在具体使用时,要对其进行强制类型转换。
其次,两个线程对自己的私有数据操作是互相不影响的。也就是说哦,虽然 key 是同名且全局,但访问的内存空间并不是相同的一个。key 就像是一个数据管理员,线程的私有数据只是到他那去注册,让它知道你这个数据的存在。
出处:http://www.groad.net/bbs/read.php?tid-2179.html
另外:而一个进行中多个线程同时访问一个全局变量用TLS实现,详细见下一篇TLS
比如在程序里可能需要每个线程维护一个链表,而会使用相同的函数来操作这个链表,最简单的方法就是使用同名而不同变量地址的线程相关数据结构。这样的数据结构可以由 Posix 线程库维护,成为线程私有数据 (Thread-specific Data,或称为 TSD)。
这里主要测试和线程私有数据有关的 4 个函数:
pthread_key_create();
pthread_key_delete();
pthread_getspecific();
pthread_setspecific();
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
pthread_key_t key;
struct
test_struct {
int
i;
float
k;
};
void
*child1 (void*arg)
{
struct
test_struct struct_data;
struct_data.i=
10;
struct_data.k=
3.1415;
pthread_setspecific (key,&struct_data);
printf ("结构体struct_data的地址为 0x%p\n",&(struct_data));
printf ("child1 中 pthread_getspecific(key)返回的指针为:0x%p\n", (structtest_struct
*)pthread_getspecific(key));
printf ("利用 pthread_getspecific(key)打印 child1 线程中与key关联的结构体中成员值:\nstruct_data.i:%d\nstruct_data.k:
%f\n", ((structtest_struct
*)pthread_getspecific (key))->i, ((struct
test_struct *)pthread_getspecific(key))->k);
printf ("------------------------------------------------------\n");
}
void
*child2 (void*arg)
{
int
temp = 20;
sleep (2);
printf ("child2 中变量 temp 的地址为 0x%p\n", &temp);
pthread_setspecific (key,&temp);
printf ("child2 中 pthread_getspecific(key)返回的指针为:0x%p\n", (int*)pthread_getspecific(key));
printf ("利用 pthread_getspecific(key)打印 child2 线程中与key关联的整型变量temp 值:%d\n",*((int*)pthread_getspecific(key)));
}
int
main (void)
{
pthread_t tid1,tid2;
pthread_key_create (&key,NULL);
pthread_create (&tid1,NULL, (void*)child1,NULL);
pthread_create (&tid2,NULL, (void*)child2,NULL);
pthread_join (tid1,NULL);
pthread_join (tid2,NULL);
pthread_key_delete (key);
return (0);
}
运行与输出:
./pthread_key
结构体struct_data的地址为 0x0xb7699388
child1 中 pthread_getspecific(key)返回的指针为:0x0xb7699388
利用 pthread_getspecific(key)打印 child1 线程中与key关联的结构体中成员值:
struct_data.i:10
struct_data.k: 3.141500
------------------------------------------------------
child2 中变量 temp 的地址为 0x0xb6e9838c
child2 中 pthread_getspecific(key)返回的指针为:0x0xb6e9838c
由输出可见,pthread_getspecific() 返回的是与key 相关联数据的指针。需要注意的是,在利用这个返回的指针时,它首先是 void 类型的,它虽然指向关联的数据地址处,但并不知道指向的数据类型,所以在具体使用时,要对其进行强制类型转换。
其次,两个线程对自己的私有数据操作是互相不影响的。也就是说哦,虽然 key 是同名且全局,但访问的内存空间并不是相同的一个。key 就像是一个数据管理员,线程的私有数据只是到他那去注册,让它知道你这个数据的存在。
出处:http://www.groad.net/bbs/read.php?tid-2179.html
另外:而一个进行中多个线程同时访问一个全局变量用TLS实现,详细见下一篇TLS
相关文章推荐
- TSD中的几个函数,pthread_key_create pthread_getspecific
- pthread库中操作线程专有数据的函数:pthread_key_create,pthread_setspecific,pthread_gtespecific,pthread_key_delete
- pthread_create,pthread_once,pthread_key_create,pthread_key_delete,pthread_getspecific,pthread_setspecific()
- pthread_key_create()--创建线程私有数据|pthread_key_delete()--注销线程私有数据
- pthread_once、pthread_key_create和pthread_getspecific和pthread_setspecific
- pthread_key_delete函数
- pthread_once,pthread_key_create,pthread_setspecific,pthread_getspecific
- liblog4cpp.so: undefined reference to `pthread_key_create'
- pthread_create()函数
- 多线程私有数据pthread_key_create
- Linux多线程函数pthread_create()函数
- pthread_key_t和pthread_key_create()详解
- C++中使用pthread.h头文件报错 - 无法解析的外部符号 __imp__pthread_create,该符号在函数 _main 中被引用
- pthread_create()函数用法
- [转]pthread_create函数编译时报错:undefined reference to 'pthread_c...
- pthread_create()函数样例
- pthread_key_t和pthread_key_create()详解
- pthread_key_t和pthread_key_create()
- pthread_key_t和pthread_key_create()详解
- pthread_key_t和pthread_key_create()的使用