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

linux学习之十五—多线程私有数据

2017年02月05日 ⁄ 综合 ⁄ 共 2606字 ⁄ 字号 评论关闭

linux多线程 私有数据

在多线程环境下,进程内的所有线程共享进程的数据空间,因此全局变量为所有线程共有。在程序设计中有时需要保存线程自己的全局变量,这种特殊的变量仅在某个线程内部有效。
线程私有数据采用立一种被称为一键多值的技术,即一个键对应多个数值。访问数据都是通过键值来访问,好像是对一个变量进行访问,其实是在访问不同的数据。使用线程私有数据时,首先要为每个线程数据创建一个相关联的键。在各个线程内部,都使用这个公用的键来指代线程数据,但是在不同的线程中,这个键代表的数据是不同的。

操作线程私有数据的函数主要有4个:

pthread_key_create(创建一个键);
pthread_setspecific(为一个键设置线程私有数据);
pthread_getspecific(从一个键读取线程私有数据);
pthread_key_delete(删除一个键)。

函数参数:

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

调用 pthread_key_create() 来创建该变量,第一个参数就是上面声明的 pthread_key_t 变量,第二个参数是一个清理函数,用来在线程释放该线程存储的时候被调用。该函数指针可以设成 NULL ,这样系统将调用默认的清理函数。

int pthread_setspecific(pthread_key_t key, const void *value)

当线程中需要存储特殊值的时候,可以调用 pthread_setspcific() 。该函数有两个参数,第一个为前面声明的 pthread_key_t 变量,第二个为 void* 变量,这样你可以存储任何类型的值。

void *pthread_getspecific(pthread_key_t key)

如果需要取出所存储的值,调用 pthread_getspecific() 。该函数的参数为前面提到的 pthread_key_t 变量,该函数返回 void * 类型的值。

int pthread_key_delete(pthread_key_t key)

注销一个 TSD 使用 pthread_key_delete() 函数。该函数并不检查当前是否有线程正在使用该 TSD,也不会调用清理函数(destructor function),而只是将 TSD 释放以供下一次调用 pthread_key_create() 使用。

用法示例:

#include<stdio.h>
#include<string.h>
#include<pthread.h>

pthread_key_t key;

void* thread2(void*arg)
{
   int  tsd=2;
   printf("thread2 %d is running\n",pthread_self());
   pthread_setspecific(key,(void*)tsd);
   sleep(1);
   printf("thread22 %d returns %d addr:%p\n",pthread_self(),pthread_getspecific(key),&tsd);
}

void* thread1(void*arg)
{
   int tsd=5;
   pthread_t thid2;
   printf("thread1 %d is running\n",pthread_self());
   pthread_setspecific(key,(void*)tsd);
   pthread_create(&thid2,NULL,thread2,NULL);
   sleep(2);
   printf("thread11 %d returns %d addr:%p\n",pthread_self(),pthread_getspecific(key),&tsd);
}

int main()
{
   pthread_t thid1;
   printf("main thread begins running\n");
   pthread_key_create(&key,NULL);
   pthread_create(&thid1,NULL,thread1,NULL);
   sleep(3);
   pthread_key_delete(key);
   printf("main thread exit\n");
   return 0;
}

运行结果:

这个程序只是单纯调用了设置私有数据的变量,但是这个没有说明全局变量如何在每个线程中保持独立。

在多线程中使用私有数据:

#include<stdio.h>
#include<string.h>
#include<pthread.h>

pthread_key_t key;

int tsd=0;

void* thread2(void*arg)
{
 //  tsd=2;
   printf("thread2 %d is running\n",pthread_self());
   sleep(1);
   printf("thread22 %d returns %d\n",pthread_self(),tsd);
}

void* thread1(void*arg)
{
   tsd=5;
  // tsd+=100;
   pthread_t thid2;
   printf("thread1 %d is running\n",pthread_self());
   pthread_setspecific(key,(void*)tsd);
   tsd+=100;//仅在该线程内加100
   pthread_create(&thid2,NULL,thread2,NULL);
   sleep(2);
   printf("thread11 %d returns %d\n",pthread_self(),pthread_getspecific(key));
}

int main()
{
   pthread_t thid1;
   printf("main thread begins running\n");
   pthread_key_create(&key,NULL);
   pthread_create(&thid1,NULL,thread1,NULL);
   sleep(3);
   pthread_key_delete(key);
   printf("main thread exit\n");
   return 0;
}

运行结果:

分析:

运行可见,在tsd与thread1中的key绑定后,执行tsd+=100操作后,只对thread1有效,即返回105,但是对thread2函数来说,依然是5。

抱歉!评论已关闭.