在 C 语言中使用 thread_local 变量

Jinku Hu 2023年1月30日 2021年3月21日
  1. 使用 _Thread_local 类型声明带有线程存储持续时间的变量
  2. 使用 thread_local 类型声明具有线程存储持续时间的变量
在 C 语言中使用 thread_local 变量

本文将介绍几种如何在 C 语言中使用 thread_local 变量的方法。

使用 _Thread_local 类型声明带有线程存储持续时间的变量

C 语言为不同的存储类别定义了多个关键字,例如 autostaticregisterextern。从 C11 标准的规范开始,添加了 _Thread_local 指定符。_Thread_local 存储持续时间始于线程创建时,并终止于线程终止。启动线程时,将初始化存储在 _Thread_local 对象中的值,并在线程终止时对其进行清理。通常,线程局部对象是避免共享资源中竞争条件的另一种选择。也就是说,我们隐式地将线程之间的数据分开,因为 _Thread_local 合格的对象为每个线程具有单独的实例。

#include <stdio.h>
#include <stdlib.h>
#include <threads.h>
#include <stdatomic.h>

#ifndef NUM_THREADS
#define NUM_THREADS 4
#endif

_Thread_local int counter = 0;

enum {MAX_ITER = 10000};

void *incrementCounter(void *thr_id) {
    long tid;
    tid = (long)thr_id;
    printf("thread %ld started incrementing ID - %lu\n", tid, thrd_current());

    for (int i = 0; i < MAX_ITER; ++i) {
        counter += 1;
    }

    return (void *)counter;
}

int main(int argc, char const *argv[]) {
    thrd_t threads[NUM_THREADS];
    int rc, sum = 0;

    for (int i = 0; i < NUM_THREADS; ++i) {
        rc = thrd_create(&threads[i], (thrd_start_t) incrementCounter, (void *)i);
        if (rc == thrd_error) {
            printf("ERORR; thrd_create() call failed\n");
            exit(EXIT_FAILURE);
        }
    }

    int retval;
    for (int i = 0; i < NUM_THREADS; ++i) {
        thrd_join(threads[i], &retval);
        sum += retval;
    }
    printf("count = %d\n", sum);

    thrd_exit(EXIT_SUCCESS);
}

输出:

thread 1 started incrementing ID - 140162648991488
thread 0 started incrementing ID - 140162657384192
thread 2 started incrementing ID - 140162640598784
thread 3 started incrementing ID - 140162632206080
count = 40000

使用 thread_local 类型声明具有线程存储持续时间的变量

另外,C 语言定义了一个宏表达式 thread_local,以将说明符表示为 _Thread_local。注意,应该在文件作用域中声明 thread_local 变量,以使其对所有线程可见,否则用户也可以显式添加 static 说明符,以将其作用域扩展到文件级。由于线程应将 thread_local 对象的值传达给主线程,因此需要修改程序结构。应该修改实现简单计数器程序的先前示例代码,以将递增的计数器值传递回调用线程。因此,我们需要利用 thrd_join 函数及其第二个参数来存储线程例程的返回值。请注意,所有递增的值都应在主线程中求和,这也负责将最终结果打印到 stdout

#include <stdio.h>
#include <stdlib.h>
#include <threads.h>
#include <stdatomic.h>

#ifndef NUM_THREADS
#define NUM_THREADS 4
#endif

thread_local int counter = 0;

enum {MAX_ITER = 10000};

void *incrementCounter(void *thr_id) {
    long tid;
    tid = (long)thr_id;
    printf("thread %ld started incrementing ID - %lu\n", tid, thrd_current());

    for (int i = 0; i < MAX_ITER; ++i) {
        counter += 1;
    }

    return (void *)counter;
}

int main(int argc, char const *argv[]) {
    thrd_t threads[NUM_THREADS];
    int rc, sum = 0;

    for (int i = 0; i < NUM_THREADS; ++i) {
        rc = thrd_create(&threads[i], (thrd_start_t) incrementCounter, (void *)i);
        if (rc == thrd_error) {
            printf("ERORR; thrd_create() call failed\n");
            exit(EXIT_FAILURE);
        }
    }

    int retval;
    for (int i = 0; i < NUM_THREADS; ++i) {
        thrd_join(threads[i], &retval);
        sum += retval;
    }
    printf("count = %d\n", sum);

    thrd_exit(EXIT_SUCCESS);
}

输出:

thread 1 started incrementing ID - 140162648991488
thread 2 started incrementing ID - 140162640598784
thread 0 started incrementing ID - 140162657384192
thread 3 started incrementing ID - 140162632206080
count = 40000
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn

相关文章 - C Thread