POSIX线程的私有数据

来源:互联网 发布:村雨妖刀赤瞳淘宝 编辑:程序博客网 时间:2024/05/01 19:47
多线程环境下,数据空间由所有线程共享。所以,一般意义上的全局变量也为所有的线程所共享。

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

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

这样的数据结构可由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)


原创粉丝点击