现在的位置: 首页 > 综合 > 正文

线程私有数据

2019年01月02日 ⁄ 综合 ⁄ 共 4870字 ⁄ 字号 评论关闭

这是一个关于Posix线程编程的专栏。作者在阐明概念的基础上,将向您详细讲述Posix线程库API。本文是第二篇将向您讲述线程的私有数据。

杨沙洲 (pubb@163.net),
工程师, 自由撰稿人

2001 年 10 月 01 日

  • +内容

概念及作用

在单线程程序中,我们经常要用到"全局变量"以实现多个函数间共享数据。在多线程环境下,由于数据空间是共享的,因此全局变量也为所有线程所共有。但有时应用程序设计中有必要提供线程私有的全局变量,仅在某个线程中有效,但却可以跨多个函数访问,比如程序可能需要每个线程维护一个链表,而使用相同的函数操作,最简单的办法就是使用同名而不同变量地址的线程相关数据结构。这样的数据结构可以由Posix线程库维护,称为线程私有数据(Thread-specific Data,或TSD)。

创建和注销

Posix定义了两个API分别用来创建和注销TSD:

int pthread_key_create(pthread_key_t *key, void (*destr_function) (void *))

该函数从TSD池中分配一项,将其值赋给key供以后访问使用。如果destr_function不为空,在线程退出(pthread_exit())时将以key所关联的数据为参数调用destr_function(),以释放分配的缓冲区。

不论哪个线程调用pthread_key_create(),所创建的key都是所有线程可访问的,但各个线程可根据自己的需要往key中填入不同的值,这就相当于提供了一个同名而不同值的全局变量。在LinuxThreads的实现中,TSD池用一个结构数组表示:

static struct pthread_key_struct pthread_keys[PTHREAD_KEYS_MAX] = { { 0, NULL } };

创建一个TSD就相当于将结构数组中的某一项设置为"in_use",并将其索引返回给*key,然后设置destructor函数为destr_function。

注销一个TSD采用如下API:

int pthread_key_delete(pthread_key_t key)

这个函数并不检查当前是否有线程正使用该TSD,也不会调用清理函数(destr_function),而只是将TSD释放以供下一次调用pthread_key_create()使用。在LinuxThreads中,它还会将与之相关的线程数据项设为NULL(见"访问")。

访问

TSD的读写都通过专门的Posix Thread函数进行,其API定义如下:

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

写入(pthread_setspecific())时,将pointer的值(不是所指的内容)与key相关联,而相应的读出函数则将与key相关联的数据读出来。数据类型都设为void *,因此可以指向任何类型的数据。

在LinuxThreads中,使用了一个位于线程描述结构(_pthread_descr_struct)中的二维void *指针数组来存放与key关联的数据,数组大小由以下几个宏来说明:

#define PTHREAD_KEY_2NDLEVEL_SIZE       32
#define PTHREAD_KEY_1STLEVEL_SIZE   \
((PTHREAD_KEYS_MAX + PTHREAD_KEY_2NDLEVEL_SIZE - 1)
/ PTHREAD_KEY_2NDLEVEL_SIZE)
    其中在/usr/include/bits/local_lim.h中定义了PTHREAD_KEYS_MAX为1024,
    因此一维数组大小为32。而具体存放的位置由key值经过以下计算得到:
idx1st = key / PTHREAD_KEY_2NDLEVEL_SIZE
idx2nd = key % PTHREAD_KEY_2NDLEVEL_SIZE

也就是说,数据存放与一个32×32的稀疏矩阵中。同样,访问的时候也由key值经过类似计算得到数据所在位置索引,再取出其中内容返回。

使用范例

以下这个例子没有什么实际意义,只是说明如何使用,以及能够使用这一机制达到存储线程私有数据的目的。

#include <stdio.h>
#include <pthread.h>
pthread_key_t   key;
void echomsg(int t)
{
        printf("destructor excuted in thread %d,param=%d\n",pthread_self(),t);
}
void * child1(void *arg)
{
        int tid=pthread_self();
        printf("thread %d enter\n",tid);
        pthread_setspecific(key,(void *)tid);
        sleep(2);
        printf("thread %d returns %d\n",tid,pthread_getspecific(key));
        sleep(5);
}
void * child2(void *arg)
{
        int tid=pthread_self();
        printf("thread %d enter\n",tid);
        pthread_setspecific(key,(void *)tid);
        sleep(1);
        printf("thread %d returns %d\n",tid,pthread_getspecific(key));
        sleep(5);
}
int main(void)
{
        int tid1,tid2;
        printf("hello\n");
        pthread_key_create(&key,echomsg);
        pthread_create(&tid1,NULL,child1,NULL);
        pthread_create(&tid2,NULL,child2,NULL);
        sleep(10);
        pthread_key_delete(key);
        printf("main thread exit\n");
        return 0;
}

给例程创建两个线程分别设置同一个线程私有数据为自己的线程ID,为了检验其私有性,程序错开了两个线程私有数据的写入和读出的时间,从程序运行结果可以看出,两个线程对TSD的修改互不干扰。同时,当线程退出时,清理函数会自动执行,参数为tid。

from:http://www.ibm.com/developerworks/cn/linux/thread/posix_threadapi/part2/

多线程环境下,数据空间由所有线程共享。所以,一般意义上的全局变量也为所有的线程所共享。

有时需要提供线程私有的全局变量:
- 可以跨多个函数访问(全局);
- 仅在某个线程有效(私有)。

比如程序可能需要每个线程都维护一个链表,维护手段相同,链表内的数据却不同。

这样的数据结构可由POSIX线程库维护,称为Thread Specific Data,简称TSD。

  1. #ifdef WIN32   
  2.     #include <windows.h>   
  3.     #define SLEEP(ms) Sleep(ms)   
  4. #else if defined(LINUX)   
  5.     #include <stdio.h>   
  6.     #define SLEEP(ms) sleep(ms)   
  7. #endif   
  8.   
  9. #include <pthread.h>   
  10.   
  11. pthread_key_t key;  
  12.   
  13. void echomsg(void * value)  
  14. {  
  15.     printf("[CHILD THREAD] Destructor excuted, param=%s\n", (char *)value);  
  16. }  
  17.   
  18. void * child1(void *arg)  
  19. {  
  20.     printf("[CHILD THREAD - 1] Thread enter\n");  
  21.     pthread_setspecific(key, arg);  
  22.     SLEEP(2);  
  23.     printf("[CHILD THREAD - 1] Thread returns %s\n", (char *)pthread_getspecific(key));  
  24.     pthread_exit(NULL);  
  25.     return NULL;  
  26. }  
  27.   
  28. void * child2(void *arg)  
  29. {  
  30.     printf("[CHILD THREAD - 2] Thread enter\n");  
  31.     pthread_setspecific(key, arg);  
  32.     SLEEP(1);  
  33.     printf("[CHILD THREAD - 2] Thread returns %s\n", (char *)pthread_getspecific(key));  
  34.     return NULL;  
  35. }  
  36.   
  37. static const char * msg[2] =  
  38. {  
  39.     "Lazy cat",  
  40.     "Brown dog"  
  41. };  
  42.   
  43. int main(int argc, char* argv[])  
  44. {  
  45.     printf("[MAIN THREAD] Hello\n");  
  46.   
  47.     pthread_key_create(&key, echomsg);  
  48.   
  49.     pthread_t tid1,tid2;  
  50.     pthread_create(&tid1, NULL, child1, (void *)msg[0]);  
  51.     pthread_create(&tid2, NULL, child2, (void *)msg[1]);  
  52.   
  53.     pthread_join(tid1, NULL);  
  54.     pthread_join(tid2, NULL);  
  55.     pthread_key_delete(key);  
  56.   
  57.     printf("[MAIN THREAD] Exit\n");  
  58.     return 0;  
  59. }  

● 创建
int pthread_key_create (pthread_key_t * key, void (*destructor) (void *))

在Linux中,TSD池用一个结构数组实现:
static struct pthread_key_struct pthread_keys[PTHREAD_KEYS_MAX] = {{0, NULL}};

创建一个TSD就相当于将结构数组中的某一项设置为"in use"状态,并将其索引返回给*key,然后设置destructor函数。

从中也可以看出,TSD的数目有上限:PTHREAD_KEYS_MAX。定义于/usr/include/bits/local_lim.h,一般为1024。

不要在destructor里调用pthread_exit函数。

● 注销
int pthread_key_delete (pthread_key_t key)

该函数并不检查当前是否有线程正使用该TSD,也不会调用清理函数(destructor),只是将TSD释放以供下一次pthread_key_create()使用。
在LinuxThreads中,它还会将与之相关的线程数据项设为NULL。

● 读写
int pthread_setspecific (pthread_key_t key, const void *value)
void * pthread_getspecific (pthread_key_t key)linux

from:http://www.linuxidc.com/Linux/2011-09/43785.htm

抱歉!评论已关闭.