分享

多线程开发时线程局部变量的使用

 lycbje 2014-02-19

一、概述

  现在多核时代多线程开发越来越重要了,多线程相比于多进程有诸多优势(当然也有诸多劣势)。在早期C的库中,有许多函数是线程不安全的,因为内部用到了静态变量,比如:char *strtok(char *s, const char *delim); 该函数内部就有一个静态指针,如果多个线程同时调用此函数时,可能就会出现奇怪的结果,当然也不是我们所想要的,现在LINUX对此函数功能有一个线程安全版本的接口:char *strtok_r(char *s, const char *delim, char **ptrptr),这就避免了多个线程同时访问的冲突问题。其实,如果保持 strtok()/2 接口不变,同时还要保证线程安全,还有一个解决办法,那就是采用线程局部变量。

  使用线程局部变量有两种使用方式,一个稍微麻烦些,一个比较简单,下面一一做个介绍(以LINUX为例)

二、线程局部变量的使用

      比较麻烦些的使用方法用到的函数主要有三个:pthread_once(pthread_once_t*, void (*init_routine)(void)), pthread_key_create()/2, pthread_setspecific()/2, pthread_getspecific()/1,其中 pthread_once 可以保证在整个进程空间init_routine函数仅被调用一次(它解决了多线程环境中使得互斥量和初始化代码都仅被初始化一次的问题);pthread_key_create 的参数之一指一个析构函数指针,当某个线程终止时该析构函数将被调用,并用对于一个进程内的给定键,该函数只能被调用一次;pthread_sespecific 和 pthread_getspecific 用来存放和获取与一个键关联的值。例子如下:

 

C代码  收藏代码
  1. pthread_key_t key;  
  2. pthread_once_t once = PTHREAD_ONCE_INIT;  
  3.   
  4. static void destructor(void *ptr)  
  5. {  
  6.     free(ptr);  
  7. }  
  8.   
  9. void init_once(void)  
  10. {  
  11.     pthread_key_create(&key, destructor);  
  12. }  
  13.   
  14. static void *get_buf(void)  
  15. {  
  16.     pthread_once(&once, init_once);  
  17.   
  18.     if ((ptr = pthread_getspecific(key)) == NULL) {  
  19.         ptr = malloc(1024);  
  20.         pthread_setspecific(key, ptr);  
  21.     }  
  22.     return (ptr);  
  23. }  
  24.   
  25. static void *thread_fn(void *arg)  
  26. {  
  27.     char *ptr = (char*) get_buf();  
  28.   
  29.     sprintf(ptr, "hello world");  
  30.     printf(">>%s\n", ptr);  
  31.     return (NULL);  
  32. }  
  33.   
  34. void test(void)  
  35. {  
  36.     int   i, n = 10;  
  37.     pthread_t tids[10];  
  38.   
  39.     for (i = 0; i < n; i++) {  
  40.         pthread_create(&tids[i], NULL, thread_fn, NULL);  
  41.     }  
  42.   
  43.     for (i = 0; i < n; i++) {  
  44.         pthread_join(&tids[i], NULL);  
  45.     }  
  46. }  

 

   另外,还有一个更加简单使用线程局部变量的方法:__thread 修饰符, (在WIN32平台下需要用: __declspec(thread) 修饰符,WIN32的东东总得要多写几笔,呵呵),于是上述代码可以修改如下:

 

C代码  收藏代码
  1. static void *get_buf(void)  
  2. {  
  3.     static __thread void *ptr = malloc(1024);  
  4.     return (ptr);  
  5. }  
  6.   
  7. static void *thread_fn(void *arg)  
  8. {  
  9.     char *ptr = (char*) get_buf();  
  10.   
  11.     sprintf(ptr, "hello world");  
  12.     printf(">>%s\n", ptr);  
  13.     return (NULL);  
  14. }  
  15.   
  16. void test(void)  
  17. {  
  18.     int   i, n = 10;  
  19.     pthread_t tids[10];  
  20.   
  21.     for (i = 0; i < n; i++) {  
  22.         pthread_create(&tids[i], NULL, thread_fn, NULL);  
  23.     }  
  24.   
  25.     for (i = 0; i < n; i++) {  
  26.         pthread_join(&tids[i], NULL);  
  27.     }  
  28. }  

 

    看到没有,这段代码比前面一个简单许多,但却有一个问题,它存在内存泄露问题,因为当线程退出时各个线程分配的动态内存(ptr = malloc(1024)) 并没有被释放。

三、用ACL线程接口操作线程局部变量

    为了解决上述问题,ACL库中实现了线程局部变量的简单释放功能:acl_pthread_atexit_add(void *arg, void (*free_callback)(void*)),修改上述代码如下:

 

C代码  收藏代码
  1. static void free_fn(void *ptr)  
  2. {  
  3.     free(ptr);  
  4. }  
  5.   
  6. static void *get_buf(void)  
  7. {  
  8.     static __thread void *ptr = malloc(1024);  
  9.   
  10.     acl_pthread_atexit_add(ptr, free_fn);  
  11.     return (ptr);  
  12. }  
  13.   
  14. static void *thread_fn(void *arg)  
  15. {  
  16.     char *ptr = (char*) get_buf();  
  17.   
  18.     sprintf(ptr, "hello world");  
  19.     printf(">>%s\n", ptr);  
  20.     return (NULL);  
  21. }  
  22.   
  23. void test(void)  
  24. {  
  25.     int   i, n = 10;  
  26.     pthread_t tids[10];  
  27.   
  28.     for (i = 0; i < n; i++) {  
  29.         acl_pthread_create(&tids[i], NULL, thread_fn, NULL);  
  30.     }  
  31.   
  32.     for (i = 0; i < n; i++) {  
  33.         acl_pthread_join(&tids[i], NULL);  
  34.     }  
  35. }  

 

    ok, 一切问题得到解决。细心的读者会发现 pthread_create, pthread_join 前面都加了前缀: acl_, 这是因为 ACL库对线程库进行了封装,以适应不同平台下(UNIX、WIN32)下的使用,这个例子是跨平台的,WIN32下同样可用。

 

    个人微博:http://weibo.com/zsxxsz

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多