线程私有数据可以用来存储线程本地的信息,tcmalloc就是用了该机制来减少锁的开销,加快内存分配。
下面给出了一个测试用例
通过pthread_key_create创建一个线程私有数据的key,并设置这个key对应私有数据的释放函数;调用pthread_setspecific将key和私有数据进行关联;可以调用pthread_getspecific进行获取私有数据。
#include <stdio.h> #include <string.h> #include <pthread.h> #include <stdlib.h> static pthread_once_t once = PTHREAD_ONCE_INIT; static pthread_key_t str_test_key; #define MAX_STR_TEST_LEN 256 static void destructor(void * buf) { free(buf); } static void create_key(void) { int s; s = pthread_key_create(&str_test_key, destructor); if (s != 0) { printf("pthread_key_create failed"); exit(-1); } } char * str_test(char * arg) { int s; char *buf; s = pthread_once(&once, create_key); if (s != 0) { printf("pthread_once failed"); exit(-1); } buf = pthread_getspecific(str_test_key); if (buf == NULL) { buf = malloc(MAX_STR_TEST_LEN); if (buf == NULL) { printf("pthread_once failed"); exit(-1); } s = pthread_setspecific(str_test_key, buf); if (s != 0) { printf("pthread_setspecific failed"); exit(-1); } } strncpy(buf, arg, MAX_STR_TEST_LEN - 1); } static void * thread_func(void * arg) { char *str; printf("Other thread about to call str_test()\n"); str = str_test((char *) arg); printf("Other thread: str (%p) = %s\n", str, str); str = pthread_getspecific(str_test_key); printf("Other thread: tsd (%p) = %s\n", str, str); return NULL; } int main(int argc, char *argv[]) { pthread_t t; int s; char *str; str = str_test("hello world"); printf("Main thread has called str_test()\n"); s = pthread_create(&t, NULL, thread_func, "this is a test"); if (s != 0) { printf("pthread_create failed"); return -1; } s = pthread_join(t, NULL); if (s != 0) { printf("pthread_join failed"); return -1; } printf("Main thread: str (%p) = %s\n", str, str); return 0; }结果:
