How to do an atomic increment and fetch in C?

后端 未结 2 1844
离开以前
离开以前 2020-12-03 14:59

I\'m looking for a way to atomically increment a short, and then return that value. I need to do this both in kernel mode and in user mode, so it\'s in C, under Linux, on In

相关标签:
2条回答
  • 2020-12-03 15:07

    GCC __atomic_* built-ins

    As of GCC 4.8, __sync built-ins have been deprecated in favor of the __atomic built-ins: https://gcc.gnu.org/onlinedocs/gcc-4.8.2/gcc/_005f_005fatomic-Builtins.html

    They implement the C++ memory model, and std::atomic uses them internally.

    The following POSIX threads example fails consistently with ++ on x86-64, and always works with _atomic_fetch_add.

    main.c

    #include <assert.h>
    #include <pthread.h>
    #include <stdlib.h>
    
    enum CONSTANTS {
        NUM_THREADS = 1000,
        NUM_ITERS = 1000
    };
    
    int global = 0;
    
    void* main_thread(void *arg) {
        int i;
        for (i = 0; i < NUM_ITERS; ++i) {
            __atomic_fetch_add(&global, 1, __ATOMIC_SEQ_CST);
            /* This fails consistently. */
            /*global++*/;
        }
        return NULL;
    }
    
    int main(void) {
        int i;
        pthread_t threads[NUM_THREADS];
        for (i = 0; i < NUM_THREADS; ++i)
            pthread_create(&threads[i], NULL, main_thread, NULL);
        for (i = 0; i < NUM_THREADS; ++i)
            pthread_join(threads[i], NULL); 
        assert(global == NUM_THREADS * NUM_ITERS);
        return EXIT_SUCCESS;
    }
    

    Compile and run:

    gcc -std=c99 -Wall -Wextra -pedantic -o main.out ./main.c -pthread
    ./main.out
    

    Disassembly analysis at: How do I start threads in plain C?

    Tested in Ubuntu 18.10, GCC 8.2.0, glibc 2.28.

    C11 _Atomic

    In 5.1, the above code works with:

    _Atomic int global = 0;
    global++;
    

    And C11 threads.h was added in glibc 2.28, which allows you to create threads in pure ANSI C without POSIX, minimal runnable example: How do I start threads in plain C?

    0 讨论(0)
  • 2020-12-03 15:08

    GCC supports atomic operations:

    gcc atomics

    0 讨论(0)
提交回复
热议问题