pthread_key_t和pthread_key_create()详解

来源:互联网 发布:规模以上 知乎 编辑:程序博客网 时间:2024/03/29 20:55

pthread_key_t和pthread_key_create()详解


下面说一下线程中特有的线程存储, Thread Specific Data 。线程存储有什么用了?他是什么意思了?大家都知道,在多线程程序中,所有线程共享程序中的变量。现在有一全局变量,所有线程都可以使用它,改变它的值。而如果每个线程希望能单独拥有它,那么就需要使用线程存储了。表面上看起来这是一个全局变量,所有线程都可以使用它,而它的值在每一个线程中又是单独存储的。这就是线程存储的意义。


下面说一下线程存储的具体用法。

1. 创建一个类型为 pthread_key_t 类型的变量。

2. 调用 pthread_key_create() 来创建该变量。该函数有两个参数,第一个参数就是上面声明的 pthread_key_t 变量,第二个参数是一个清理函数,用来在线程释放该线程存储的时候被调用。该函数指针可以设成 NULL ,这样系统将调用默认的清理函数。

3. 当线程中需要存储特殊值的时候,可以调用 pthread_setspcific() 。该函数有两个参数,第一个为前面声明的 pthread_key_t 变量,第二个为 void* 变量,这样你可以存储任何类型的值。

4. 如果需要取出所存储的值,调用 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*));

下面是一个如何使用线程存储的例子:

[cpp] view plaincopyprint?
  1. #include <malloc.h>  
  2.   
  3. #include <pthread.h>  
  4.   
  5. #include <stdio.h>  
  6.   
  7. /* The key used to associate a log file pointer with each thread. */  
  8.   
  9. static pthread_key_t thread_log_key;  
  10.   
  11. /* Write MESSAGE to the log file for the current thread. */  
  12.   
  13. void write_to_thread_log (const char* message)  
  14.   
  15. {  
  16.     FILE* thread_log = (FILE*) pthread_getspecific (thread_log_key);  
  17.   
  18.     fprintf (thread_log, “%s\n”, message);  
  19. }  
  20.   
  21. /* Close the log file pointer THREAD_LOG. */  
  22.   
  23. void close_thread_log (void* thread_log)  
  24.   
  25. {  
  26.     fclose ((FILE*) thread_log);      
  27. }  
  28.   
  29. void* thread_function (void* args)  
  30.   
  31. {  
  32.     char thread_log_filename[20];  
  33.   
  34.     FILE* thread_log;  
  35.   
  36.     /* Generate the filename for this thread’s log file. */  
  37.   
  38.     sprintf (thread_log_filename, “thread%d.log”, (int) pthread_self ());  
  39.   
  40.     /* Open the log file. */  
  41.   
  42.     thread_log = fopen (thread_log_filename, “w”);  
  43.   
  44.     /* Store the file pointer in thread-specific data under thread_log_key. */  
  45.   
  46.     pthread_setspecific (thread_log_key, thread_log);  
  47.   
  48.     write_to_thread_log (“Thread starting.”);  
  49.   
  50.     /* Do work here... */  
  51.   
  52.     return NULL;  
  53. }  
  54.   
  55. int main ()  
  56.   
  57. {  
  58.   
  59.     int i;  
  60.   
  61.     pthread_t threads[5];  
  62.   
  63.     /* Create a key to associate thread log file pointers in 
  64.  
  65.     thread-specific data. Use close_thread_log to clean up the file 
  66.  
  67.     pointers. */  
  68.   
  69.     pthread_key_create (&thread_log_key, close_thread_log);  
  70.   
  71.     /* Create threads to do the work. */  
  72.   
  73.     for (i = 0; i < 5; ++i)  
  74.   
  75.         pthread_create (&(threads[i]), NULL, thread_function, NULL);  
  76.   
  77.     /* Wait for all threads to finish. */  
  78.   
  79.     for (i = 0; i < 5; ++i)  
  80.   
  81.         pthread_join (threads[i], NULL);  
  82.   
  83.     return 0;  
  84.   
  85. }    



最后说一下线程的本质。

其实在Linux 中,新建的线程并不是在原先的进程中,而是系统通过一个系统调用clone() 。该系统copy 了一个和原先进程完全一样的进程,并在这个进程中执行线程函数。不过这个copy 过程和fork 不一样。copy 后的进程和原先的进程共享了所有的变量,运行环境(clone的实现是可以指定新进程与老进程之间的共享关系,100%共享就表示创建了一个线程)。这样,原先进程中的变量变动在copy 后的进程中便能体现出来。
0 0