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

一、概述

  现在多核时代多线程开发越来越重要了,多线程相比于多进程有诸多优势(当然也有诸多劣势)。在早期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 用来存放和获取与一个键关联的值。例子如下:

 

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);
    }
}

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

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

    为了解决上述问题,ACL库中实现了线程局部变量的简单释放功能: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, 一切问题得到解决。细心的读者会发现 pthread_create, pthread_join 前面都加了前缀: acl_, 这是因为 ACL库对线程库进行了封装,以适应不同平台下(UNIX、WIN32)下的使用,这个例子是跨平台的,WIN32下同样可用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值