linux多執行緒學習筆記六--一次性初始化和執行緒私有資料
阿新 • • 發佈:2019-01-31
pthread_key_t key;
int pthread_key_create(pthread_key *key,void (*destructor)(void*));
int pthread_key_delete(pthread_key_t key);
該函式從TSD池中分配一項,將其值賦給key供以後訪問使用。如果destr_function不為空,線上程退出(pthread_exit())時將以key所關聯的資料為引數呼叫destr_function(),以釋放分配的緩衝區。不論哪個執行緒呼叫pthread_key_create(),所建立的key都是所有執行緒可訪問的,但各個執行緒可根據自己的需要往key中填入不同的值,這就相當於提供了一個同名而不同值的全域性變數.
pthread_key_delete這個函式並不檢查當前是否有執行緒正使用該TSD,也不會呼叫清理函式(destr_function),而只是將TSD釋放以供下一次呼叫pthread_key_create()使用。因為只有你肯定沒有執行緒持有該鍵值時,才能刪除執行緒私有資料鍵,故通常的做法是不釋放執行緒私有資料鍵。
2)使用執行緒私有資料
int pthread_setspecific(pthread_key_t key, const void *value)
void * pthread_getspecific(pthread_key_t key)
可以使用pthread_getspecific函式來獲取執行緒當前鍵值,或通過第一個函式改變當前的鍵值。執行結果如下:/* * tsd_once.c * * Demonstrate use of pthread_once to initialize something * exactly once within a multithreaded program. * * Note that it is often easier to use a statically initialized * mutex to accomplish the same result. */ #include <pthread.h> #include <stdio.h> /* * Structure used as the value for thread-specific data key. */ typedef struct tsd_tag { pthread_t thread_id; char *string; } tsd_t; pthread_key_t tsd_key; /* Thread-specific data key */ pthread_once_t key_once = PTHREAD_ONCE_INIT; /* * One-time initialization routine used with the pthread_once * control block. */ void once_routine (void) { int status; printf ("initializing key\n"); status = pthread_key_create (&tsd_key, NULL); if (status != 0) printf("error Create key"); } /* * Thread start routine that uses pthread_once to dynamically * create a thread-specific data key. */ void *thread_routine (void *arg) { tsd_t *value; int status; status = pthread_once (&key_once, once_routine);//一次性初始化 if (status != 0) printf("error Once init"); value = (tsd_t*)malloc (sizeof (tsd_t)); if (value == NULL) printf("error Allocate key value"); status = pthread_setspecific (tsd_key, value); if (status != 0) printf("error Set tsd"); printf ("%s set tsd value %p\n", arg, value); value->thread_id = pthread_self (); value->string = (char*)arg; value = (tsd_t*)pthread_getspecific (tsd_key); printf ("%s starting...\n", value->string); sleep (2); value = (tsd_t*)pthread_getspecific (tsd_key); printf ("%s done...\n", value->string); return NULL; } void main (int argc, char *argv[]) { pthread_t thread1, thread2; int status; status = pthread_create ( &thread1, NULL, thread_routine, "thread 1"); if (status != 0) printf("error Create thread 1"); status = pthread_create ( &thread2, NULL, thread_routine, "thread 2"); if (status != 0) printf("error Create thread 2"); pthread_exit; }
thread 2 set tsd value 0x9de24f0
thread 2 starting...
thread 1 set tsd value 0x9de2500
thread 1 starting...
thread 2 done...
thread 1 done...
部分內容摘自:http://www.ibm.com/developerworks/cn/linux/thread/posix_threadapi/part2/