名稱空間
變體
操作

std::atomic_fetch_add, std::atomic_fetch_add_explicit

來自 cppreference.com
< cpp‎ | atomic
 
 
併發支援庫
執行緒
(C++11)
(C++20)
this_thread 名稱空間
(C++11)
(C++11)
(C++11)
協同取消
互斥
(C++11)
通用鎖管理
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
條件變數
(C++11)
訊號量
門閂和屏障
(C++20)
(C++20)
期值
(C++11)
(C++11)
(C++11)
(C++11)
安全回收
(C++26)
危險指標
原子型別
(C++11)
(C++20)
原子型別的初始化
(C++11)(C++20 中已棄用)
(C++11)(C++20 中已棄用)
記憶體排序
(C++11)(C++26 中已棄用)
原子操作的自由函式
atomic_fetch_addatomic_fetch_add_explicit
(C++11)(C++11)
原子標誌的自由函式
 
定義於標頭檔案 <atomic>
template< class T >

T atomic_fetch_add( std::atomic<T>* obj,

                    typename std::atomic<T>::difference_type arg ) noexcept;
(1) (C++11 起)
template< class T >

T atomic_fetch_add( volatile std::atomic<T>* obj,

                    typename std::atomic<T>::difference_type arg ) noexcept;
(2) (C++11 起)
template< class T >

T atomic_fetch_add_explicit( std::atomic<T>* obj,
                             typename std::atomic<T>::difference_type arg,

                             std::memory_order order ) noexcept;
(3) (C++11 起)
template< class T >

T atomic_fetch_add_explicit( volatile std::atomic<T>* obj,
                             typename std::atomic<T>::difference_type arg,

                             std::memory_order order ) noexcept;
(4) (C++11 起)

執行原子加法操作。原子地將 arg 加到 obj 指向的值,並返回 obj 之前持有的值。操作的執行方式如同以下程式碼被執行:

1,2) obj->fetch_add(arg)
3,4) obj->fetch_add(arg, order)

如果 std::atomic<T> 沒有 fetch_add 成員(該成員僅為整型浮點型(C++20 起)指標型別提供,除了 bool),則程式格式不正確。

目錄

[編輯] 引數

obj - 指向要修改的原子物件的指標
arg - 要加到原子物件中儲存的值上的值
順序 - 記憶體同步順序

[編輯] 返回值

*obj修改順序中,此函式效果緊前的值。

[編輯] 示例

可以使用 std::atomic_fetch_add 實現單寫入器/多讀取器鎖。請注意,這種簡單的實現並非無飢餓(lockout-free)。

#include <atomic>
#include <chrono>
#include <iostream>
#include <string>
#include <thread>
#include <vector>
 
using namespace std::chrono_literals;
 
// meaning of cnt:
//  5: readers and writer are in race. There are no active readers or writers.
//  4...0: there are 1...5 active readers, The writer is blocked.
// -1: writer won the race and readers are blocked.
 
const int N = 5; // four concurrent readers are allowed
std::atomic<int> cnt(N);
 
std::vector<int> data;
 
void reader(int id)
{
    for (;;)
    {
        // lock
        while (std::atomic_fetch_sub(&cnt, 1) <= 0)
            std::atomic_fetch_add(&cnt, 1);
 
        // read
        if (!data.empty())
            std::cout << ("reader " + std::to_string(id) +
                          " sees " + std::to_string(*data.rbegin()) + '\n');
        if (data.size() == 25)
            break;
 
        // unlock
        std::atomic_fetch_add(&cnt, 1);
 
        // pause
        std::this_thread::sleep_for(1ms);
    }
}
 
void writer()
{
    for (int n = 0; n < 25; ++n)
    {
        // lock
        while (std::atomic_fetch_sub(&cnt, N + 1) != N)
            std::atomic_fetch_add(&cnt, N + 1);
 
        // write
        data.push_back(n);
        std::cout << "writer pushed back " << n << '\n';
 
        // unlock
        std::atomic_fetch_add(&cnt, N + 1);
 
        // pause
        std::this_thread::sleep_for(1ms);
    }
}
 
int main()
{
    std::vector<std::thread> v;
    for (int n = 0; n < N; ++n)
        v.emplace_back(reader, n);
    v.emplace_back(writer);
 
    for (auto& t : v)
        t.join();
}

輸出

writer pushed back 0
reader 2 sees 0
reader 3 sees 0
reader 1 sees 0
<...>
reader 2 sees 24
reader 4 sees 24
reader 1 sees 24

[編輯] 缺陷報告

下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
P0558R1 C++11 因為要求精確型別匹配
T 是從多個引數推匯出來的
T 僅從
obj 中推導

[編輯] 參閱

原子地將引數新增到原子物件中儲存的值,並獲取之前持有的值
(std::atomic<T> 的公開成員函式) [編輯]
從原子物件中減去非原子值並獲取原子的先前值
(函式模板) [編輯]
C 文件 用於 atomic_fetch_add, atomic_fetch_add_explicit