線程資料處理 之 線程資料
為了保護變數,我們必須使用訊號量、互斥等方法來保證我們對變數的正確使用。
1) 線程資料
在單線程的程式裡,有兩種基本的資料:全域變數和局部變數。但在多線程程式裡,還有第三種資料類型:線程資料(TSD: Thread-Specific Data)。它和全域變數很象,線上程內部,各個函數可以象使用全域變數一樣調用它,但它對線程外部的其它線程是不可見的.
線程儲存的具體用法
建立一個類型為 pthread_key_t 類型的變數。
調用 pthread_key_create() 來建立該變數。該函數有兩個參數,第一個參數就是上面聲明的 pthread_key_t 變數,第二個參數是一個清理函數,用來線上程釋放該線程儲存的時候被調用。該函數指標可以設成 NULL ,這樣系統將調用預設的清理函數。
當線程中需要儲存特殊值的時候,可以調用 pthread_setspcific() 。該函數有兩個參數,第一個為前面聲明的 pthread_key_t 變數,第二個為 void* 變數,這樣你可以儲存任何類型的值。
如果需要取出所儲存的值,調用 pthread_getspecific() 。該函數的參數為前面提到的 pthread_key_t 變數,該函數返回 void * 類型的值。
下面是前面提到的函數的原型:
int pthread_setspecific(pthread_key_t key, const void *value);
void *pthread_getspecific(pthread_key_t key);
int pthread_key_create(pthread_key_t *key, void (*destructor)(void*));
下面是一個如何使用線程儲存的例子:
#include <malloc.h>
#include <pthread.h>
#include <stdio.h>
/* The key used to associate a log file pointer with each thread. */
static pthread_key_t thread_log_key;
/* Write MESSAGE to the log file for the current thread. */
void write_to_thread_log (const char* message)
{
FILE* thread_log = (FILE*) pthread_getspecific (thread_log_key);
fprintf (thread_log, “%s\n”, message);
}
/* Close the log file pointer THREAD_LOG. */
void close_thread_log (void* thread_log)
{
fclose ((FILE*) thread_log);
}
void* thread_function (void* args)
{
char thread_log_filename[20];
FILE* thread_log;
/* Generate the filename for this thread’s log file. */
sprintf (thread_log_filename, “thread%d.log”, (int) pthread_self ());
/* Open the log file. */
thread_log = fopen (thread_log_filename, “w”);
/* Store the file pointer in thread-specific data under thread_log_key. */
pthread_setspecific (thread_log_key, thread_log);
write_to_thread_log (“Thread starting.”);
/* Do work here... */
return NULL;
}
int main ()
{
int i;
pthread_t threads[5];
/* Create a key to associate thread log file pointers in
thread-specific data. Use close_thread_log to clean up the file pointers. */
pthread_key_create (&thread_log_key, close_thread_log);
/* Create threads to do the work. */
for (i = 0; i < 5; ++i)
{
pthread_create (&(threads[i]), NULL, thread_function, NULL);
}
/* Wait for all threads to finish. */
for (i = 0; i < 5; ++i)
{
pthread_join (threads[i], NULL);
}
return 0;
}