Namespaces
Variants

std:: atomic_fetch_or, std:: atomic_fetch_or_explicit

From cppreference.net
Concurrency support library
Threads
(C++11)
(C++20)
this_thread namespace
(C++11)
(C++11)
Cooperative cancellation
Mutual exclusion
Generic lock management
Condition variables
(C++11)
Semaphores
Latches and Barriers
(C++20)
(C++20)
Futures
(C++11)
(C++11)
(C++11)
Safe reclamation
Hazard pointers
Atomic types
(C++11)
(C++20)
Initialization of atomic types
(C++11) (deprecated in C++20)
(C++11) (deprecated in C++20)
Memory ordering
(C++11) (deprecated in C++26)
Free functions for atomic operations
atomic_fetch_or atomic_fetch_or_explicit
(C++11) (C++11)
Free functions for atomic flags
定义于头文件 <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 - 要与原子对象中存储的值进行按位或运算的值
order - 内存同步排序方式

返回值

* obj 修改顺序 中,此函数生效前紧邻的数值。

示例

#include <atomic>
#include <chrono>
#include <functional>
#include <iostream>
#include <thread>
// 二进制信号量仅用于演示目的。
// 这是一个简单但有意义的示例:如果没有线程,原子操作是不必要的。
class Semaphore
{
    std::atomic_char m_signaled;
public:
    Semaphore(bool initial = false)
    {
        m_signaled = initial;
    }
    // 阻塞直到信号量被触发
    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(); // 触发打印操作
                m_count_sem.take(); // 等待打印完成后再继续
            }
        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++标准。

DR 适用范围 发布时的行为 正确行为
P0558R1 C++11 由于
T 需要从多个参数进行推导,因此要求精确类型匹配
T 仅从
obj 进行推导

参见

原子地在参数和原子对象的值之间执行按位或运算,并获取先前持有的值
( std::atomic<T> 的公开成员函数)
用与非原子参数的按位与结果替换原子对象,并获取原子的先前值
(函数模板)
用与非原子参数的按位异或结果替换原子对象,并获取原子的先前值
(函数模板)
C 文档 用于 atomic_fetch_or , atomic_fetch_or_explicit