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. CSS3去除手机浏览器button点击出现的高亮框

    在工作中常常遇到在手机浏览器中浏览网页时.点击页面中的button或者是具备点击事件的元素,就会出现一个默认的高亮框.影响总体的感官体验. 能够用一个简单的css3属性来解决:tap-highligh ...

  2. mavern安装方法

    Installation Instructions Maven is a Java tool, so you must have Java installed in order to proceed. ...

  3. MySQL 联合索引测试3

    接上一篇文章: http://www.cnblogs.com/xiaoit/p/4430387.html 有时候会出现某字段建立一个索引,但是查看执行计划的时候发现还是全扫了表? 可以强制走下索引看看 ...

  4. windows 配置ftp server

  5. 实现锁死的有滚动条的div的表格(datagird)

    JS框架使用Jquery 最终效果: 代码结构: 代码: <HEAD><TITLE>new document</TITLE> <META name=Gener ...

  6. 新浪微博api出现认证失败问题 (获取code字段值的问题)

    出现该提示的原因:`` - 说: (2015-10-30 18:06:14)回调地址不一致,`` - 说: (2015-10-30 18:07:38)请在编辑开发者信息中将网站地址和应用信息--高级信 ...

  7. 类的专有方法(__getattr__和__setattr__、__delattr__)

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #http://www.360doc.com/content/15/0413/19/12067640_4629 ...

  8. 类的专有方法(__del__)

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #http://www.bubuko.com/infodetail-313791.html #类的专有方法(_ ...

  9. Start-Sleep 帮助信息

    如下说明是翻译: help Start-Sleep 产生的帮助信息.译者: Edengundam(马涛) Start-Sleep 大纲使shell, 脚本, 或运行空间的活动挂起指定的时间. 语法St ...

  10. oper

    package main.java.com.zte.controller.ems; import java.util.HashMap; import java.util.List; import ja ...