名稱空間
變體
操作

std::atomic_fetch_or, std::atomic_fetch_or_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_oratomic_fetch_or_explicit
(C++11)(C++11)
原子標誌的自由函式
 
定義於標頭檔案 <atomic>
template< class T >

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

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

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

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

T atomic_fetch_or_explicit( std::atomic<T>* obj,
                            typename std::atomic<T>::value_type arg,

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

T atomic_fetch_or_explicit( volatile std::atomic<T>* obj,
                            typename std::atomic<T>::value_type arg,

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

原子地將 obj 指向的值替換為 obj 的舊值與 arg 進行按位或操作的結果。返回 obj 先前持有的值。

該操作的執行方式等同於以下程式碼:

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

如果 std::atomic<T> 沒有 fetch_or 成員(此成員僅為整型型別提供,除了 bool),則程式格式錯誤。

目錄

[編輯] 引數

obj - 指向要修改的原子物件的指標
arg - 要與原子物件中儲存的值進行按位或操作的值
順序 - 記憶體同步順序

[編輯] 返回值

*obj修改順序中,此函式效果之前立即持有的值。

[編輯] 示例

#include <atomic>
#include <chrono>
#include <functional>
#include <iostream>
#include <thread>
 
// Binary semaphore for demonstrative purposes only.
// This is a simple yet meaningful example: atomic operations
// are unnecessary without threads. 
class Semaphore
{
    std::atomic_char m_signaled;
public:
    Semaphore(bool initial = false)
    {
        m_signaled = initial;
    }
    // Block until semaphore is signaled
    void take() 
    {
        while (!std::atomic_fetch_and(&m_signaled, false))
        {
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
        }
    }
 
    void put() 
    {
        std::atomic_fetch_or(&m_signaled, true);
    }
};
 
class ThreadedCounter
{
    static const int N = 100;
    static const int REPORT_INTERVAL = 10;
    int m_count;
    bool m_done;
    Semaphore m_count_sem;
    Semaphore m_print_sem;
 
    void count_up() 
    {
        for (m_count = 1; m_count <= N; ++m_count)
            if (m_count % REPORT_INTERVAL == 0)
            {
                if (m_count == N)
                    m_done = true;
                m_print_sem.put(); // signal printing to occur
                m_count_sem.take(); // wait until printing is complete proceeding
            }
        std::cout << "count_up() done\n";
        m_done = true;
        m_print_sem.put();
    }
 
    void print_count() 
    {
        do
        {
            m_print_sem.take();
            std::cout << m_count << '\n';
            m_count_sem.put();
        }
        while (!m_done);
        std::cout << "print_count() done\n";
    }
 
public:
    ThreadedCounter() : m_done(false) {}
    void run() 
    {
        auto print_thread = std::thread(&ThreadedCounter::print_count, this);
        auto count_thread = std::thread(&ThreadedCounter::count_up, this);
        print_thread.join();
        count_thread.join();
    }
};
 
int main() 
{
    ThreadedCounter m_counter;
    m_counter.run();
}

輸出

10
20
30
40
50
60
70
80
90
100
print_count() done
count_up() done

[編輯] 缺陷報告

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

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

[編輯] 參閱

原子地在引數和原子物件的值之間執行按位或運算,並獲取之前持有的值
(std::atomic<T> 的公共成員函式) [編輯]
將原子物件替換為與非原子引數進行位 AND 運算的結果,並獲取原子的先前值
(函式模板) [編輯]
將原子物件替換為與非原子引數進行位 XOR 運算的結果,並獲取原子的先前值
(函式模板) [編輯]
C 文件 用於 atomic_fetch_or, atomic_fetch_or_explicit