在 C 语言中使用原子类型

Jinku Hu 2021年3月21日
在 C 语言中使用原子类型

本文将演示有关如何在 C 语言中使用原子类型的多种方法。

使用原子类型隐式同步对共享资源的访问

原子类型的对象是唯一可以被多个线程同时访问和修改而不会发生竞争条件的对象。这个特性对于从不同线程访问的全局变量和静态变量至关重要,它会保留程序的正确性。通常,使用原子类型对象可以替代锁定互斥对象之类的对象以及它们的标准 API 函数(如 mtx_lockmtx_unlock 等)。以下代码示例演示了计数问题的简单情况,其中多个线程递增一个共享的全局计数器变量。最后,在程序末尾将总和打印到 stdout。请注意,我们将 counter 声明为通常的 int 类型。遗憾的是,即使某些执行可能会产生正确的结果,该程序还是有缺陷的。

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

#ifndef NUM_THREADS
#define NUM_THREADS 4
#endif

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

    thrd_exit(EXIT_SUCCESS);
}

int main(int argc, char const *argv[]) {
    thrd_t threads[NUM_THREADS];
    int rc;
    long t;

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

    for (t = 0; t < NUM_THREADS; t++) {
        thrd_join(threads[t], NULL);
    }
    printf("count = %d\n", counter);

    thrd_exit(EXIT_SUCCESS);
}

输出:

thread 0 started incrementing ID - 140097636923136
thread 2 started incrementing ID - 140097620137728
thread 1 started incrementing ID - 140097628530432
thread 3 started incrementing ID - 140097611745024
count = 18851

请注意,使用 thrd_create 调用创建其他线程的主线程不会增加计数器变量,因此总和应为 MAX_ITER 常量和 NUM_THREADS 的倍数,表示线程数。可以通过用互斥锁/解锁功能或信号量动作将 counter += 1 行包围来解决此问题,但是在这种情况下,我们仅将 counter 声明为 atomic_int 的类型。现在,这个整数对象具有原子属性,这意味着对它的任何访问都将作为一条指令进行而不会中断,并保证了程序的顺序执行。

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

#ifndef NUM_THREADS
#define NUM_THREADS 4
#endif

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

    thrd_exit(EXIT_SUCCESS);
}

int main(int argc, char const *argv[]) {
    thrd_t threads[NUM_THREADS];
    int rc;
    long t;

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

    for (t = 0; t < NUM_THREADS; t++) {
        thrd_join(threads[t], NULL);
    }
    printf("count = %d\n", counter);

    thrd_exit(EXIT_SUCCESS);
}

输出:

thread 0 started incrementing ID - 140125987915520
thread 1 started incrementing ID - 140125979522816
thread 2 started incrementing ID - 140125971130112
thread 3 started incrementing ID - 140125962737408
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