pthread_once_t once_control = PTHREAD_ONCE_INIT;
int pthread_once(pthread_once_t *once_control, void (*init_routine)(void));
这个api的主要用途是确保所有的线程传入同一个once_control时,init_routine只会被执行一次。后续线程再对同一个once_control调用pthread_once时,init_routine不会再被执行。这样可以保证多线程并发的执行初始化序列函数的时候是安全的。
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_setspecific(pthread_key_t key, const void *value);
这组api的主要用途是创建一个线程私有数据,设置和获取线程私有数据。也就是说,对同于同一个key,所有线程都可以设置和获取不同的value,类似于TLS的概念。这个私有数据的默认值是NULL,在线程的生命周期中都是有效的。创建私有数据的时候可以指定一个析构函数,当线程结束的时候,如果当前线程的value不为null,则会调用这个析构函数,并将当前的value作为参数传入。调用pthread_key_delete之后,任何的pthread_setspecific和pthread_getspecific都是未定义的。在析构函数中调用pthread_getspecific得到的结果应该是NULL,不应该在析构函数中再调用pthread_setspecific设置非NULL值,否则可能引起死循环。
最后上一段测试的代码:
#include <malloc.h>
#include <pthread.h>
#include <stdio.h>
#include <assert.h>
static pthread_key_t thread_key;
static pthread_once_t once_control = PTHREAD_ONCE_INIT;
static pthread_mutex_t mutex;
static unsigned int total = 0;
#define THREAD_NUM 10
void init_thread();
void destroy_key(void*);
void* work_func();
void init_thread()
{
printf("execute once\n");
total = 0;
assert(0 == pthread_key_create(&thread_key, destroy_key));
assert(0 == pthread_mutex_init(&mutex, NULL));
}
void destroy_key(void* p)
{
void *p_value = pthread_getspecific(thread_key);
assert(0 == p_value);
printf("destroy_key, pid = %d\n", (unsigned long)p);
}
void* work_func(void *args)
{
assert(0 == pthread_once(&once_control, init_thread));
assert(0 == pthread_setspecific(thread_key, (void*)pthread_self()));
void *p_value = pthread_getspecific(thread_key);
assert(0 == pthread_mutex_lock(&mutex));
printf("work_func, pid = %lu\n", (unsigned long)p_value);
++total;
assert(0 == pthread_mutex_unlock(&mutex));
return NULL;
}
int main()
{
pthread_t thread_id[THREAD_NUM];
int i ;
for (i = 0; i < THREAD_NUM; i++)
pthread_create(thread_id + i, NULL, work_func, NULL);
for (i = 0; i < THREAD_NUM; i++)
pthread_join(thread_id[i], NULL);
printf("total = %u\n", total);
assert(0 == pthread_key_delete(thread_key));
return 0;
}
执行结果如下:
execute once
work_func, pid = 1111857504
destroy_key, pid = 1111857504
work_func, pid = 1097083232
destroy_key, pid = 1097083232
work_func, pid = 1143327072
destroy_key, pid = 1143327072
work_func, pid = 1132837216
destroy_key, pid = 1132837216
work_func, pid = 1122347360
destroy_key, pid = 1122347360
work_func, pid = 1174796640
destroy_key, pid = 1174796640
work_func, pid = 1164306784
work_func, pid = 1195776352
destroy_key, pid = 1195776352
destroy_key, pid = 1164306784
work_func, pid = 1153816928
destroy_key, pid = 1153816928
work_func, pid = 1185286496
destroy_key, pid = 1185286496
total = 10
init_thread确实只执行了一次,而对于同一个key,pthread_getspecific在每一个线程的执行结果也确实都是不一样的。