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 의 비트 OR 연산 결과로 대체합니다. 이전에 보유하고 있던 obj 값을 반환합니다.

이 작업은 다음과 같이 실행되는 것처럼 수행됩니다:

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

만약 std::atomic<T> fetch_or 멤버가 없는 경우(이 멤버는 integral types bool 을 제외한 타입에만 제공됨), 프로그램은 ill-formed입니다.

목차

매개변수

obj - 수정할 원자적 객체에 대한 포인터
arg - 원자적 객체에 저장된 값에 비트 OR 연산을 수행할 값
order - 메모리 동기화 순서

반환값

이 함수의 효과가 적용되기 직전의 modification 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 에서만 추론됨

참고 항목

인수와 원자 객체의 값 사이에 비트 OR를 원자적으로 수행하고 이전에 보유한 값을 획득합니다
( std::atomic<T> 의 public member function)
원자 객체를 비원자 인수와의 비트 AND 결과로 대체하고 원자 객체의 이전 값을 획득합니다
(function template)
원자 객체를 비원자 인수와의 비트 XOR 결과로 대체하고 원자 객체의 이전 값을 획득합니다
(function template)
C documentation for atomic_fetch_or , atomic_fetch_or_explicit