名稱空間
變體
操作

atomic_fetch_add, atomic_fetch_add_explicit

來自 cppreference.com
< c‎ | atomic
在標頭檔案 <stdatomic.h> 中定義
C atomic_fetch_add( volatile A* obj, M arg );
(1) (C11 起)
C atomic_fetch_add_explicit( volatile A* obj, M arg, memory_order order );
(2) (C11 起)

原子地將 `arg` 新增到 `obj` 指向的舊值,並返回 `obj` 先前持有的值。該操作是讀-改-寫操作。第一個版本按照 memory_order_seq_cst 排序記憶體訪問,第二個版本按照 `order` 排序記憶體訪問。

這是一個為所有原子物件型別 `A` 定義的泛型函式。引數是指向 volatile 原子型別的指標,以接受非 volatile 和 volatile(例如記憶體對映 I/O)原子物件的地址,並且當將此操作應用於 volatile 原子物件時,volatile 語義得以保留。如果 `A` 是原子整數型別,則 `M` 是與 `A` 對應的非原子型別;如果 `A` 是原子指標型別,則 `M` 是 ptrdiff_t

泛型函式的名稱是宏還是具有外部連結的識別符號是未指定的。如果為了訪問實際函式而抑制宏定義(例如用括號括起來,如 (atomic_fetch_add)(...)),或者程式定義了一個與泛型函式同名的外部識別符號,則行為是未定義的。

對於有符號整數型別,算術運算定義為使用二進位制補碼錶示。沒有未定義的結果。對於指標型別,結果可能是一個未定義的地址,但操作本身沒有未定義的行為。

目錄

[編輯] 引數

obj - 指向要修改的原子物件的指標
arg - 要新增到原子物件中儲存的值的值
順序 - 此操作的記憶體同步順序:允許所有值

[編輯] 返回值

由 `obj` 指向的原子物件先前持有的值。

[編輯] 示例

#include <stdio.h>
#include <threads.h>
#include <stdatomic.h>
 
atomic_int acnt;
int cnt;
 
int f(void* thr_data)
{
    for(int n = 0; n < 1000; ++n) {
        atomic_fetch_add_explicit(&acnt, 1, memory_order_relaxed); // atomic
        ++cnt; // undefined behavior, in practice some updates missed
    }
    return 0;
}
 
int main(void)
{
    thrd_t thr[10];
    for(int n = 0; n < 10; ++n)
        thrd_create(&thr[n], f, NULL);
    for(int n = 0; n < 10; ++n)
        thrd_join(thr[n], NULL);
 
    printf("The atomic counter is %u\n", acnt);
    printf("The non-atomic counter is %u\n", cnt);
}

可能的輸出

The atomic counter is 10000
The non-atomic counter is 9511

[編輯] 參考

  • C17 標準 (ISO/IEC 9899:2018)
  • 7.17.7.5 The atomic_fetch and modify generic functions (p: 208)
  • C11 標準 (ISO/IEC 9899:2011)
  • 7.17.7.5 The atomic_fetch and modify generic functions (p: 284-285)

[編輯] 另請參閱

原子減法
(函式) [編輯]
C++ 文件 for atomic_fetch_add, atomic_fetch_add_explicit