POSIX线程的私有数据

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

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

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

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

#ifdef WIN32
	#include <windows.h>
	#define SLEEP(ms) Sleep(ms)
#else if defined(LINUX)
	#include <stdio.h>
	#define SLEEP(ms) sleep(ms)
#endif

#include <pthread.h>

pthread_key_t key;

void echomsg(void * value)
{
	printf("[CHILD THREAD] Destructor excuted, param=%s\n", (char *)value);
}

void * child1(void *arg)
{
	printf("[CHILD THREAD - 1] Thread enter\n");
	pthread_setspecific(key, arg);
	SLEEP(2);
	printf("[CHILD THREAD - 1] Thread returns %s\n", (char *)pthread_getspecific(key));
	pthread_exit(NULL);
	return NULL;
}

void * child2(void *arg)
{
	printf("[CHILD THREAD - 2] Thread enter\n");
	pthread_setspecific(key, arg);
	SLEEP(1);
	printf("[CHILD THREAD - 2] Thread returns %s\n", (char *)pthread_getspecific(key));
	return NULL;
}

static const char * msg[2] =
{
	"Lazy cat",
	"Brown dog"
};

int main(int argc, char* argv[])
{
	printf("[MAIN THREAD] Hello\n");

	pthread_key_create(&key, echomsg);

	pthread_t tid1,tid2;
	pthread_create(&tid1, NULL, child1, (void *)msg[0]);
	pthread_create(&tid2, NULL, child2, (void *)msg[1]);

	pthread_join(tid1, NULL);
	pthread_join(tid2, NULL);
	pthread_key_delete(key);

	printf("[MAIN THREAD] Exit\n");
	return 0;
}

● 创建
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)


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值