一,一次性初始化
以保證線程在調用資源時,確保資源已經被初始化,並且只初始化一次。
在傳統的順序編程中,一次性初始化經常通過使用布爾變量來管理。控制變量被靜態初始化爲0,而任何依賴於初始化的代碼都能測試該變量。如果變量值仍然爲0,則它能實行初始化,然後將變量置爲1。以後檢查的代碼將跳過初始化。
但是在多線程程序設計中,事情就變的複雜的多。如果多個線程併發地執行初始化序列代碼,2個線程可能發現控制變量爲0,並且都實行初始話,而該過程本該僅僅執行一次。初始化的狀態必須由互斥量保護。之所以使用pthread_once,主要原因是原來不能靜態的初始化一個互斥量,這樣如果要使用一個互斥量,必須調用pthread_mutex_init函數初始化互斥量,並且必須僅僅初始化一次,因此初始化調用應該在一次性初始化代碼中完成,pthread_once就解決在多線程環境中使得互斥量和初始化代碼都僅僅被初始化一次的問題;
#include <pthread.h>
pthread_once_t once_control=PTHREAD_ONCE_INIT;
int pthread_once(pthread_once_t *once_control,void(*init_routine)(void));
init_routine 初始化函數
實例代碼如下:#include <pthread.h>
#include <stdio.h>
pthread_once_t once_block = PTHREAD_ONCE_INIT;
pthread_mutex_t mutex;
/*
* This is the one-time initialization routine. It will be
* called exactly once, no matter how many calls to pthread_once
* with the same control structure are made during the course of
* the program.
*/
void once_init_routine (void)
{
int status;
status = pthread_mutex_init (&mutex, NULL);
if (status != 0)
printf("error Init Mutex");
}
/*
* Thread start routine that calls pthread_once.
*/
void *thread_routine (void *arg)
{
int status;
status = pthread_once (&once_block, once_init_routine);
if (status != 0)
printf("error Once init");
status = pthread_mutex_lock (&mutex);
if (status != 0)
printf("error Lock mutex");
printf ("thread_routine has locked the mutex.\n");
status = pthread_mutex_unlock (&mutex);
if (status != 0)
printf("error Unlock mutex");
return NULL;
}
int main (int argc, char *argv[])
{
pthread_t thread_id;
char *input, buffer[64];
int status;
status = pthread_create (&thread_id, NULL, thread_routine, NULL);
if (status != 0)
printf("error Create thread");
status = pthread_once (&once_block, once_init_routine);
if (status != 0)
printf("error Once init");
status = pthread_mutex_lock (&mutex);
if (status != 0)
printf("error Lock mutex");
printf ("Main has locked the mutex.\n");
status = pthread_mutex_unlock (&mutex);
if (status != 0)
printf("error Unlock mutex");
status = pthread_join (thread_id, NULL);
if (status != 0)
printf("error Join thread");
return 0;
}
程序輸出:
Main has locked the mutex.
thread_routine has locked the mutex.
二,線程私有數據
在單線程程序中,我們經常要用到"全局變量"以實現多個函數間共享數據。在多線程環境下,由於數據空間是共享的,因此全局變量也爲所有線程所共有。但有時應用程序設計中有必要提供線程私有的全局變量,僅在某個線程中有效,但卻可以跨多個函數訪問,比如程序可能需要每個線程維護一個鏈表,而使用相同的函數操作,最簡單的辦法就是使用同名而不同變量地址的線程相關數據結構。這樣的數據結構可以由Posix線程庫維護,稱爲線程私有數據(Thread-specific Data,或TSD)。
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/