4. POSIX线程库通过维护一定的数据结构来解决这个问题,这些个数据成为TSD(Thread-specific Data)。
相关函数:
int pthread_key_create(pthread_key_t *key, void (*destructor)(void*)); int pthread_key_delete(pthread_key_t key); void *pthread_getspecific(pthread_key_t key); int pthread_setsepcific(pthread_key_t key, const void *value); int pthread_once(pthread_once_t *once_control, void(*init_routine)(void)); pthread_once_t once_control=PTHREAD_ONCE_INIT;
#include<pthread.h> #include<unistd.h> #include<stdio.h> #include<sys/types.h> #include<stdlib.h> #include<errno.h> #include<string.h> #define ERR_EXIT(m) do{perror(m);exit(EXIT_FAILURE);}while(0) pthread_key_t key_tsd; pthread_once_t once_control = PTHREAD_ONCE_INIT; struct tsd { pthread_t tid; char *str; }; void destroy_routine(void *value) { printf("destroy\n"); free(value); } void once_routine(void) { pthread_key_create(&key_tsd,destroy_routine); printf("key init....\n"); } void* thread_routine(void *arg) { // pthread_once(&once_control,once_routine); tsd *value=(tsd*)malloc(sizeof(tsd)); value->tid=pthread_self(); value->str=(char*)arg; pthread_setspecific(key_tsd, value); printf("%s setspecific %p\n",(char*)arg,value); value=(tsd*)pthread_getspecific(key_tsd); printf("tid=%x,str=%s\n",(int)value->tid,value->str); sleep(2); value=(tsd*)pthread_getspecific(key_tsd); printf("tid=%x,str=%s\n",(int)value->tid,value->str); return NULL; } int main() { pthread_key_create(&key_tsd,destroy_routine); pthread_t tid1,tid2; char ch1[]="thread1"; char ch2[]="thread2"; pthread_create(&tid1,NULL,thread_routine,(void*)ch1); pthread_create(&tid2,NULL,thread_routine,(void*)ch2); pthread_join(tid1,NULL); pthread_join(tid2,NULL); pthread_key_delete(key_tsd); return 0; } 程序运行结果:可以发现程序分别输出了自己的线程名称和自己value的地址,说明value=(tsd*)pthread_getspecific(key_tsd)输入了不同的key_tsd,互不影响,各自有自己的“全局变量”。pthread_once(&once_control,once_routine);这个程序只会在进入的第一个线程里执行once_routine函数。