这个原作者的这个地方写错了 且他举的例子非常不好。最后有我的修正版本

pthread_setspecific(key, (void *)&my_errno);

linux 线程私有数据之一键多值技术TSD池 2012-09-15 16:22:08

分类: LINUX

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

进程内的所有线程共享进程的数据空间,因此全局变量为所有线程所共有。但有时线程也需要保存自己的私有数据,这时可以创建线程私有数据(Thread-
specific Date)TSD来解决。在线程内部,私有数据可以被各个函数访问,但对其他线程是屏蔽的。一个明显的例子是errno,每个线程都有自己的副本,不然由于线程间的切换,在一个线程里输出的很可能是令一线程的出错信息。

线程私有数据采用了一种一键多值的技术,即一个键对应多个数值。访问数据时都是通过键值来访问,好像是对一个变量进行访问,其实是在访问不同的数据。使用
线程私有数据时,首先要为每个线程数据创建一个相关联的键。POSIX中操作线程私有数据的主要通过以下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_key_delete(pthread_key_t key);

int pthread_setspecific(pthread_key_t key, const void *pointer);

void * pthread_getspecific(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}};

创建一个TSD就相当于将结构数组中的某一项设置为“in_use”,并将其索引返回给*key,然后设置destructor函数
destr_function。pthread_key_create 创建一个新的线程特定数据Key时,系统搜索其所在进程的 Key
结构数组,找出其中第一个不在使用的元素,并返回该元素的索引键。
    pthread_setspecific:该函数将pointer的值(不是内容)与key相关联。用pthread_setspecific为一个键指定新的线程数据时,线程必须先释放原有的线程数据用以回收空间。
    pthread_getspecific:通过该函数得到与key相关联的数据。
    pthread_key_delete:该函数用来删除一个键,键所占用的内存将被释放。需要注意的是,键占用的内存被释放,与该键关联的线程数据所占用的内存并不被释放。因此,线程数据的释放必须在释放键之前完成。
    下面我们通过一个例子来学习使用这几个函数的使用,功能类似errno在线程的使用,我猜想全局变量errno是在创建一个线程pthread_create时利用TSD池为该线程分配一个errno的副本的。

点击(此处)折叠或打开

  1. /*   my_errno.c
  2. 编译链接:gcc my_errno.c -o my_errno -lpthread
  3. */
  4. // 线程私有数据,一键多值,tsd池
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <pthread.h>
  8. int my_errno = 0;
  9. pthread_key_t key;
  10. void print_errno(char *str)
  11. {
  12. printf("%s my_errno:%d\n",str, my_errno);
  13. }
  14. void *thread2(void *arg)
  15. {
  16. printf("thread2 %ld is running\n",pthread_self());
  17. pthread_setspecific(key, (void *)my_errno);
  18. printf("thread2 %ld returns %d\n",pthread_self(),\
  19. pthread_getspecific(key));
  20. my_errno = 2;
  21. print_errno("thread2");
  22. }
  23. void *thread1(void *arg)
  24. {
  25. pthread_t thid2;
  26. printf("thread1 %ld is running\n",pthread_self());
  27. pthread_setspecific(key, (void *)my_errno);
  28. my_errno = 1;
  29. pthread_create(&thid2, NULL, thread2, NULL);
  30. sleep(2);
  31. printf("thread1 %ld returns %d\n",pthread_self(),\
  32. pthread_getspecific(key));
  33. print_errno("thread1");
  34. }
  35. void destr(void *arg)
  36. {
  37. printf("destroy memory\n");
  38. }
  39. int main(void)
  40. {
  41. pthread_t thid1;
  42. printf("main thread begins running. my_errno=%d\n",my_errno);
  43. pthread_key_create(&key, destr);
  44. pthread_create(&thid1, NULL, thread1, NULL);
  45. sleep(4);
  46. pthread_key_delete(key);
  47. printf("main thread exit\n");
  48. return 0;
  49. }

./my_errno 后你会发现虽然my_errno是全局的,但在thread1与thread2保留的是私有数据。

/*   my_errno.c
编译链接:gcc my_errno.c -o my_errno -lpthread
*/ // 线程私有数据,一键多值,tsd池
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <errno.h> extern int errno ; pthread_key_t key; void print_errno(char *str)
{
printf("%s my_errno:%d\n",str, errno);
}
void *thread2(void *arg)
{
errno = ;
printf("thread2 %ld is running\n",pthread_self());
pthread_setspecific(key, (void *)&errno);
printf("thread2 %ld returns %d\n",pthread_self(),\
pthread_getspecific(key)); print_errno("thread2");
} void *thread1(void *arg)
{
errno = ;
pthread_t thid2; printf("thread1 %ld is running\n",pthread_self());
pthread_setspecific(key, (void *)&errno); pthread_create(&thid2, NULL, thread2, NULL);
sleep();
printf("thread1 %ld returns %d\n",pthread_self(),\
pthread_getspecific(key)); print_errno("thread1");
} void destr(void *arg)
{
printf("destroy memory\n");
} int main(void)
{
pthread_t thid1; printf("main thread begins running. my_errno=%d\n",errno);
pthread_key_create(&key, destr);
pthread_create(&thid1, NULL, thread1, NULL);
sleep();
pthread_key_delete(key); printf("main thread exit\n"); return ;
}

UE$ ./1
main thread begins running. my_errno=0
thread1 139672638236416 is running
thread2 139672629843712 is running
thread2 139672629843712 returns 293377696
thread2 my_errno:2
destroy memory

thread1 139672638236416 returns 301770400
thread1 my_errno:1
destroy memory

main thread exit

ZT linux 线程私有数据之 一键多值技术的更多相关文章

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

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

  2. linux线程私有数据---TSD池

    进程内的所有线程共享进程的数据空间,所以全局变量为所有线程共有.在某些场景下,线程需要保存自己的私有数据,这时可以创建线程私有数据(Thread-specific Data)TSD来解决.在线程内部, ...

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

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

  4. pthread线程私有数据

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

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

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

  6. [转] unix/linux下线程私有数据实现原理及使用方法

     在维护每个线程的私有数据的时候,我们可能会想到分配一个保存线程数据的数组,用线程的ID作为数组的索引来实现访问,但是有一个问题是系统生成的线程 ID不能保证是一个小而连续的整数,并且用数组实现的时候 ...

  7. UNIX环境高级编程——线程私有数据

    线程私有数据(Thread-specific data,TSD):存储和查询与某个线程相关数据的一种机制. 在进程内的所有线程都共享相同的地址空间,即意味着任何声明为静态或外部变量,或在进程堆声明的变 ...

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

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

  9. 线程私有数据和pthread_once

    #include <stdio.h> #include <pthread.h> pthread_key_t key; pthread_once_t ponce = PTHREA ...

随机推荐

  1. 安装多个PHP环境会导致phpinfo和php -v中查看到的PHP版本不一致

    以前在上一个公司用的是集成环境wamp,PHP版本是5.5.后面换了一个公司,项目用的是PHP版本是5.2.今天想打开以前的项目想优化一下,发现pdo_mysql.dll扩展无法加载,于是想看看是不是 ...

  2. 通过Jscript中@cc_on 语句识别IE浏览器及版本的代码

    激活条件编译支持. @cc_on 备注 @cc_on 语句激活脚本引擎中的条件编译. 强烈推荐在注释中使用 @cc_on 语句,以使不支持条件编译的浏览器将您的脚本视为有效语法而接受它: /*@cc_ ...

  3. jdk 自动化脚本

    添加没有登录权限 需要理解linux用户,首先登陆的是root用户,拥有所有的权限,但是该权限太大,一般都会分配其他用户使用,并且在部署程序时,需要分配一个没有登录权限的用户,这样改程序不能随意被修改 ...

  4. C++公有继承、保护继承和私有继承

    C++中的继承方式有: public.private.protected三种(它们直接影响到派生类的成员.及其对象对基类成员访问的规则). (1)public(公有继承):继承时保持基类中各成员属性不 ...

  5. window.open以post方式提交(转)

    function openWindowWithPost(url,name,keys,values) { var newWindow = window.open(url, name); if (!new ...

  6. Azure 上 Linux 虚拟机 Mac 地址的持久化

    有些用户在使用 Azure Linux 虚拟机安装软件时,有些软件的 license 会和当前系统的 mac 地址绑定,那么在 Azure VM 重启,reszie(改变尺寸大小),停止然后再启动的时 ...

  7. webservice随记

    WebService:跨平台.系统.跨语言间相互调用 CXF:Axis(Apache)-> Axis2(Apache)XFire -> CXF(Celtrix + XFire)(Apach ...

  8. eclipse中php项目开发的环境配置说明

    PHP开发的环境配置比Java开发要简单点,也就是我们不用安装jdk了,我们不用安装tomcat了,仅仅通过一种集成环境来安装就好了. PHP开发,其实有很多种环境配置方式,我这里使用了XAMPP进行 ...

  9. poj 1947 树形背包 (删边)

    http://blog.csdn.net/woshi250hua/article/details/7632785 这道题我一开始想的dp[i][j],i是节点,j是删除的点数,dp是最少删边的个数,然 ...

  10. IDEA 2017的插件mybatis plugin(绿色免安装)

    https://blog.csdn.net/u014365133/article/details/78885189 插件下载 https://files.cnblogs.com/files/techl ...