多线程编程之私有数据(Thread-Specific-Data,或TSD)
来源:互联网 发布:用js修改css样式 编辑:程序博客网 时间:2024/06/06 12:35
在多线程环境下,全局变量为所有线程共享。然而,在程序设计中有时需要保存线程自己的全局变量,
这种特殊的变量尽在某个线程内部有效。为此引入私有数据,在线程内部,线程私有数据可以被各个
函数访问,但它对其他线程是屏蔽的。
线程私有数据采用了一种被称为一键多值的技术,即一个键对应多个数值。访问数据时都是通过键值
来访问,好像是在对一个变量进行访问,其实在访问不同的数据。使用线程私有数据时,首先要为每个线
程数据创建一个相关联的键。在各个线程内部,都是用这个公用的键来指代线程数据,但是在不同线程中,
这个键代表的数据是不同的。操作线程私有数据的函数主要有4个:pthread_key_create(创建一个键),
pthread_setspecific(为一个键设置线程私有数据),pthread_getspecific(从一个键读取私有数据),
pthread_key_delete(删除一个键)。这几个函数声明如下:
#include <pthread.h>
int pthread_key_create(pthread_key_t *key,void (*destr_function)(void *));
int pthread_setspecific(pthread_key_t key,const void *pointer));
void *pthread_getspecific(pthread_key_t key);
int pthread_key_delete(pthread_key_t key);
pthread_key_create:从linux的TSD池中分配一项,将其值赋给key供以后访问使用,他的第一个参数key为指
向键值的指针,第二个参数为一个函数指针,如果指针不为空,则在线程退出时将以
key所关联的数据位参数调用destr_function(),释放分配的缓冲区。
key一旦被创建,所有线程都可以访问它,但各个线程可根据自己的需要往key中填入不同的值,这就相
当于提供了一个同名而不同值的全局变量,一键多值。一键多值靠的是一个关键数据结构数组,即TSD池,
其结构如下:
static struct pthread_key_struct pthread_keys [PTHREAD_KEYS_MAX] = {{0,NULL}};
pthread_setspecific:该函数将pointer的值(不是内容)与key关联。用pthread_setspecific为一个键指定新的
线程数据时,线程必须先释放原有的线程数居用以回收空间。
pthread_getspecific:通过该函数得到与key相关联的数据。
pthread_key_delete:该函数用来删除一个键,键所占用的内存将被释放。需要注意的是,键所占用的内存将
被释放,与该键关联的线程数据所占用的内存并不被释放。因此,线程数据的释放必须
在释放键之前完成。
示例代码:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
pthread_key_t key;
void private_data2()
{
int tsd = 2;
pthread_t chthread;
printf("Thread %u is running...\n",pthread_self());
pthread_setspecific(key,(void *)tsd);
printf("Thread %u return %d\n",pthread_self(),pthread_getspecific(key));
}
void private_data1()
{
int tsd = 1;
pthread_t chthread;
printf("Thread %u is running...\n",pthread_self());
pthread_setspecific(key,(void *)tsd);
pthread_create(&chthread,NULL,(void *)private_data2,NULL);
sleep(1);
printf("Thread %u return %d\n",pthread_self(),pthread_getspecific(key));
}
int main()
{
pthread_t thread;
int ret;
pthread_key_create(&key,NULL);
ret = pthread_create(&thread,NULL,(void *)private_data1,NULL);
if(ret)
{
printf("Create pthread failed!\n");
return -1;
}
sleep(3);
pthread_key_delete(key);
return 0;
}
编译:gcc -o teat test.c -lpthread
运行结果:
Thread 3078716272 is running...
Thread 3070323568 is running...
Thread 3070323568 return 2
Thread 3078716272 return 1
可以看出private_data2对tsd的修改并没有影响到private_data1的tsd的取值。
- 多线程编程之私有数据(Thread-Specific-Data,或TSD)
- 线程私有数据(Thread-specific Data,或TSD)
- Thread-Specific Data(TSD)线程私有数据
- 线程的私有数据(TSD-Thread-Specific Data)
- linux 多线程编程 Thread Specific Data (TSD)
- TSD(Thread Specific Data)线程专有数据
- 【多线程编程】线程私有数据(TSD)
- Thread-Specific Data(线程私有数据)
- 线程私有数据(Thread-Specific Data)
- 线程私有数据thread-specific
- 线程专有数据(Thread-Specific Data)
- linux网络编程之posix 线程(二):线程的属性和 线程特定数据 Thread-specific Data
- 多线程中的第三种变量类型:Thread-Specific Data (线程专有数据)
- 线程的私有数据(TSD)
- 线程私有数据TSD
- 线程私有数据TSD
- Thread-specific data
- Thread-specific data
- 关于最近
- ee电子商务项目中遇到的一些问题
- linux下的“分离控制”思想
- google地图密钥申请与将google地图集成到自己的程序中(附源代码)
- 单例模式
- 多线程编程之私有数据(Thread-Specific-Data,或TSD)
- 远程主机强迫关闭了一个现有的连接。
- 并查集
- 编译C++与Fortran
- UVa197 - Cube(TLE)
- BSS段,数据段,代码段,堆内存和栈
- hibernate配置文件中重复定义导致的错误:should be mapped with insert="false" update="false"
- 其实,你不快乐的根源,是因为你不知道要什么!
- socket 通信 Windos 和 Linux之间 Big endian 和 little endian的转换策略