線程局部變量的使用與多線程開發(fā)
多線程,現(xiàn)在多核時代來臨之際,多線程編程已經(jīng)成為了時髦語。有專門應(yīng)運(yùn)而生的Erlang,以及以Actor著稱的Scala語言。下面介紹線程局部變量的使用,對于學(xué)習(xí)使用多線程會有一些幫助。
一、概述
現(xiàn)在多核時代多線程開發(fā)越來越重要了,多線程相比于多進(jìn)程有諸多優(yōu)勢(當(dāng)然也有諸多劣勢)。在早期C的庫中,有許多函數(shù)是線程不安全的,因?yàn)閮?nèi)部用到了靜態(tài)變量,比如:char *strtok(char *s, const char *delim); 該函數(shù)內(nèi)部就有一個靜態(tài)指針,如果多個線程同時調(diào)用此函數(shù)時,可能就會出現(xiàn)奇怪的結(jié)果,當(dāng)然也不是我們所想要的,現(xiàn)在LINUX對此函數(shù)功能有一個線程安全版本的接口:char *strtok_r(char *s, const char *delim, char **ptrptr),這就避免了多個線程同時訪問的沖突問題。其實(shí),如果保持 strtok()/2 接口不變,同時還要保證線程安全,還有一個解決辦法,那就是采用線程局部變量。
使用線程局部變量有兩種使用方式,一個稍微麻煩些,一個比較簡單,下面一一做個介紹(以LINUX為例)
二、線程局部變量的使用
比較麻煩些的使用方法用到的函數(shù)主要有三個:pthread_once(pthread_once_t*, void (*init_routine)(void)), pthread_key_create()/2, pthread_setspecific()/2, pthread_getspecific()/1,其中 pthread_once 可以保證在整個進(jìn)程空間init_routine函數(shù)僅被調(diào)用一次(它解決了多線程環(huán)境中使得互斥量和初始化代碼都僅被初始化一次的問題);pthread_key_create 的參數(shù)之一指一個析構(gòu)函數(shù)指針,當(dāng)某個線程終止時該析構(gòu)函數(shù)將被調(diào)用,并用對于一個進(jìn)程內(nèi)的給定鍵,該函數(shù)只能被調(diào)用一次;pthread_sespecific 和 pthread_getspecific 用來存放和獲取與一個鍵關(guān)聯(lián)的值。例子如下:
- pthread_key_t key;
- pthread_once_t once = PTHREAD_ONCE_INIT;
- static void destructor(void *ptr)
- {
- free(ptr);
- }
- void init_once(void)
- {
- pthread_key_create(&key, destructor);
- }
- static void *get_buf(void)
- {
- pthread_once(&once, init_once);
- if ((ptr = pthread_getspecific(key)) == NULL) {
- ptr = malloc(1024);
- pthread_setspecific(key, ptr);
- }
- return (ptr);
- }
- static void *thread_fn(void *arg)
- {
- char *ptr = (char*) get_buf();
- sprintf(ptr, "hello world");
- printf(">>%s\n", ptr);
- return (NULL);
- }
- void test(void)
- {
- int i, n = 10;
- pthread_t tids[10];
- for (i = 0; i < n; i++) {
- pthread_create(&tids[i], NULL, thread_fn, NULL);
- }
- for (i = 0; i < n; i++) {
- pthread_join(&tids[i], NULL);
- }
- }
另外,還有一個更加簡單使用線程局部變量的方法:__thread 修飾符, (在WIN32平臺下需要用: __declspec(thread) 修飾符,WIN32的東東總得要多寫幾筆,呵呵),于是上述代碼可以修改如下:
- static void *get_buf(void)
- {
- static __thread void *ptr = malloc(1024);
- return (ptr);
- }
- static void *thread_fn(void *arg)
- {
- char *ptr = (char*) get_buf();
- sprintf(ptr, "hello world");
- printf(">>%s\n", ptr);
- return (NULL);
- }
- void test(void)
- {
- int i, n = 10;
- pthread_t tids[10];
- for (i = 0; i < n; i++) {
- pthread_create(&tids[i], NULL, thread_fn, NULL);
- }
- for (i = 0; i < n; i++) {
- pthread_join(&tids[i], NULL);
- }
- }
看到?jīng)]有,這段代碼比前面一個簡單許多,但卻有一個問題,它存在內(nèi)存泄露問題,因?yàn)楫?dāng)線程退出時各個線程分配的動態(tài)內(nèi)存(ptr = malloc(1024)) 并沒有被釋放。
三、用ACL線程接口操作線程局部變量
為了解決上述問題,ACL庫中實(shí)現(xiàn)了線程局部變量的簡單釋放功能:acl_pthread_atexit_add(void *arg, void (*free_callback)(void*)),修改上述代碼如下:
- static void free_fn(void *ptr)
- {
- free(ptr);
- }
- static void *get_buf(void)
- {
- static __thread void *ptr = malloc(1024);
- acl_pthread_atexit_add(ptr, free_fn);
- return (ptr);
- }
- static void *thread_fn(void *arg)
- {
- char *ptr = (char*) get_buf();
- sprintf(ptr, "hello world");
- printf(">>%s\n", ptr);
- return (NULL);
- }
- void test(void)
- {
- int i, n = 10;
- pthread_t tids[10];
- for (i = 0; i < n; i++) {
- acl_pthread_create(&tids[i], NULL, thread_fn, NULL);
- }
- for (i = 0; i < n; i++) {
- acl_pthread_join(&tids[i], NULL);
- }
- }
ok, 一切問題得到解決。細(xì)心的讀者會發(fā)現(xiàn) pthread_create, pthread_join 前面都加了前綴: acl_, 這是因?yàn)?ACL庫對線程庫進(jìn)行了封裝,以適應(yīng)不同平臺下(UNIX、WIN32)下的使用,這個例子是跨平臺的,WIN32下同樣可用。
【編輯推薦】