1.当多个线程共享一个变量时,将该变量定义为静态或外部变量,使用互斥量确保共享变量的安全访问。
如果每个线程都需要一个私有变量值,则该值成为线程的私有数据。程序创建一个键,每个线程独立地设定或得到自己的键值,各线程间私有数据互不影响。

2.建立线程私有数据
int pthread_key_create(pthread_key_t *key,void (*destructor)(void *));
int pthread_key_delete(pthread_key_t key);
int pthread_setspecific(pthread_key_t key,void *value);
void *pthread_getspecific(pthread_key_t key);

私有数据键,若多次创建,会覆盖掉前面创建的键,对应的键值也将永远丢失。

使用pthread_key_delete释放一个私有数据键时,必须保证所有线程都不在持有该键的值。
更好的做法是不释放线程私有数据键,因为线程私有数据键最多可达128个,很少有程序需要这么多的树木,没有必要手动释放。

代码示例如下:
创建数据键,设置键值,获取键值

#include<stdio.h>
#include<pthread.h>
typedef struct tsd_tag
{
pthread_t tid;
char *string;
}tsd_t;
pthread_key_t tsd_key;
pthread_once_t key_once = PTHREAD_ONCE_INIT;
void one_routine(void)
{
pthread_key_create(&tsd_key,NULL);;
}
void *thread_routine(void *arg)
{
pthread_once(&key_once,one_routine);
tsd_t *value;
value = (tsd_t*)malloc(sizeof(tsd_t));
pthread_setspecific(tsd_key,value);
value->tid = pthread_self();
value->string = (char*)arg;
value=(tsd_t*)pthread_getspecific(tsd_key);
sleep();
value=(tsd_t*)pthread_getspecific(tsd_key);
printf("%s done...\n",value->string);
}
int main(void)
{
pthread_t tid1,tid2;
pthread_create(&tid1,NULL,thread_routine,"thread 1");
pthread_create(&tid2,NULL,thread_routine,"thread 2");
pthread_exit(NULL);
}

3.destructor函数
destructor函数仅仅当线程终止时被调用。当线程终止时,pthreads调用键的destructor函数前,将线程私有数据置为NULL,所以若线程私有数据值是堆存储的地址,并且想在destructor函数中释放,必须使用传递给destructor的参数,而不是pthread_getspecific的参数。可以自定义destructor函数。

键值为NULL意味着线程对应该键值不再有值,而不是赋空值。否则若随后调用pthread_key_create建立该键,线程将会收到旧值。

线程私有数据键的destructor函数在键值替换的时候不会被调用。即,如果在堆中将一个结构的指针作为键值,又分配一个新结构,并将新结构指针赋给相同数据键,则指向旧结构的指针不会调用destructor函数。

代码示例如下:

三个线程的私有变量数据,自定义destructor函数,在线程结束后,释放堆存储,在所有线程结束后,销毁私有数据键。

#include<stdio.h>
#include<pthread.h>
typedef struct private_tag
{
pthread_t tid;
char *string;
}private_t;
pthread_key_t key;
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
long counter = ;
void key_destructor(void *value)
{
private_t *private = (private_t*)value;
printf("thread \"%s\" exiting...\n",private->string);
free(value);
pthread_mutex_lock(&mutex);
counter--;
if(counter<=)
{
pthread_key_delete(key);
printf("key deleted...\n");
}
pthread_mutex_unlock(&mutex);
}
void *key_get(void)
{
void *value;
value = pthread_getspecific(key);
if(value==NULL)
{
printf("malloc\n");
value = malloc(sizeof(private_t));
pthread_setspecific(key,(void*)value);
}
return value;
}
void *thread_routine(void *arg)
{
private_t *value;
value = (private_t*)key_get();
value->tid = pthread_self();
value->string = (char*)arg;
printf("thread \"%s\" starting...\n",value->string);
sleep();
return NULL;
}
int main(void)
{
pthread_t tid1,tid2;
private_t *value;
pthread_key_create(&key,key_destructor);
counter = ;
value = (private_t *)key_get();
value->tid = pthread_self();
value->string = "Main thread";
pthread_create(&tid1,NULL,thread_routine,"Thread 1");
pthread_create(&tid2,NULL,thread_routine,"Thread 2");
printf("exiting\n");
pthread_exit(NULL);
}

posix多线程--线程私有数据的更多相关文章

  1. Posix线程编程指南(2) 线程私有数据

    概念及作用 在单线程程序中,我们经常要用到"全局变量"以实现多个函数间共享数据.在多线程环境下,由于数据空间是共享的,因此全局变量也为所有线程所共有.但有时应用程序设计中有必要提供 ...

  2. linux多线程学习笔记六--一次性初始化和线程私有数据【转】

    转自:http://blog.csdn.net/kkxgx/article/details/7513278 版权声明:本文为博主原创文章,未经博主允许不得转载. 一,一次性初始化 以保证线程在调用资源 ...

  3. 【C/C++多线程编程之十】pthread线程私有数据

    多线程编程之线程私有数据      Pthread是 POSIX threads 的简称.是POSIX的线程标准.         线程同步从相互排斥量[C/C++多线程编程之六]pthread相互排 ...

  4. pthread线程私有数据

    进程内所有的线程共享地址空间,任何声明为静态或外部的变量,或在进程堆声明的变量都可以被进程内的所有线程读写. static,extern,或堆变量的值是上次线程改写的值 一个线程真正拥有的唯一私有存储 ...

  5. pthread_getspecific()--读线程私有数据|pthread_setspecific()--写线程私有数据

    原型: #include <pthread.h> void *pthread_getspecific(pthread_key_t key); int pthread_setspecific ...

  6. Linux系统编程——线程私有数据

    在多线程程序中.常常要用全局变量来实现多个函数间的数据共享.因为数据空间是共享的,因此全局变量也为全部线程共同拥有. 測试代码例如以下: #include <stdio.h> #inclu ...

  7. ZT linux 线程私有数据之 一键多值技术

    这个原作者的这个地方写错了 且他举的例子非常不好.最后有我的修正版本 pthread_setspecific(key, (void *)&my_errno); linux 线程私有数据之一键多 ...

  8. Thread-specific data(TSD)线程私有数据

    Thread-specific data(TSD)线程私有数据 http://blog.chinaunix.net/uid-26885237-id-3209913.html linux多线程编程中引入 ...

  9. 线程私有数据TSD——一键多值技术,线程同步中的互斥锁和条件变量

    一:线程私有数据: 线程是轻量级进程,进程在fork()之后,子进程不继承父进程的锁和警告,别的基本上都会继承,而vfork()与fork()不同的地方在于vfork()之后的进程会共享父进程的地址空 ...

随机推荐

  1. JSP实现数据保存(web基础学习笔记四)

    session对象: //服务端设置Session属性 session.setAttribute("user", username); //客户端接收Session Object ...

  2. HDOJ 4686 Arc of Dream 矩阵高速幂

    矩阵高速幂: 依据关系够建矩阵 , 高速幂解决. Arc of Dream Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65535/ ...

  3. kinect v1+ ubuntu 配置问题

    从github上下载openni 以及 SensorKinect. install 之后 启动openni下的例子 NiViewer发生错误. Open failed: Failed to set U ...

  4. CSS3 GPU硬件加速

    1.代码(未添加GPU加速代码) <!DOCTYPE html> <html lang="zh-CN"> <head> <meta cha ...

  5. idea 配置多个jdk

      1.File -->Project Structure 2.SDKs-->点击+号-->JDK-->目录选择到jdk文件夹所在的位置 3.选择jdk所在路径 4.可以配置多 ...

  6. Java基础1-反射篇

    1.页首请关注 思维导航大纲 2.大牛文章学习: 序号 博主 主要内容 1  sinat_38259539 总结的较全面的反射内容 2     3.自己的理解: 3.1.本文大纲 3.2.正文 1.获 ...

  7. chardet 模块

    #coding:utf-8 #指定本文件编码为utf-8 #python 27 #xiaodeng #chardet模块 #chardet模块下载地址: #1)http://pan.baidu.com ...

  8. linux修改 时间 时区

    linux系统修改系统时间与时区 | 浏览:3486 | 更新:2014-06-18 19:36 1 2 3 4 5 6 7 分步阅读 有装过Linux系统的人,可能都会有这样的经历,就是该机器安装w ...

  9. 【vue.js】windows下安装vue.js

    windows下搭建vue开发环境 Vue.js是一套构建用户界面的 “渐进式框架”.与其他重量级框架不同的是,Vue 采用自底向上增量开发的设计.Vue 的核心库只关注视图层,并且非常容易学习,非常 ...

  10. message 弹出窗口

    import  javax.swing.JOptionPane;public class gong {    public static void main(String [] args){      ...