Namespaces
Variants

std:: atomic_fetch_sub, std:: atomic_fetch_sub_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_sub atomic_fetch_sub_explicit
(C++11) (C++11)
Free functions for atomic flags
헤더 파일에 정의됨 <atomic>
template < class T >

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

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

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

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

T atomic_fetch_sub_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_sub_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_sub ( arg )
3,4) obj - > fetch_sub ( arg, order )

만약 std::atomic<T> fetch_sub 멤버가 없는 경우(이 멤버는 정수형 , 부동소수점형 (C++20부터) 포인터 타입에만 제공됨, bool 제외), 프로그램은 형식에 맞지 않습니다.

목차

매개변수

obj - 수정할 원자적 객체에 대한 포인터
arg - 원자적 객체에 저장된 값에서 뺄 값
order - 메모리 동기화 순서

반환값

이 함수의 효과가 적용되기 직전의 값, 즉 modification order 에서 * obj 의 값.

예제

여러 스레드가 인덱스된 컨테이너를 동시에 처리하기 위해 std::atomic_fetch_sub 을 사용할 수 있습니다.

#include <atomic>
#include <iostream>
#include <numeric>
#include <string>
#include <thread>
#include <vector>
const int N = 50;
std::atomic<int> cnt;
std::vector<int> data(N);
void reader(int id) 
{
    for (;;)
    {
        int idx = atomic_fetch_sub_explicit(&cnt, 1, std::memory_order_relaxed);
        if (idx >= 0)
            std::cout << "reader " << std::to_string(id) << " processed item "
                      << std::to_string(data[idx]) << '\n';
        else
        {
            std::cout << "reader " << std::to_string(id) << " done\n";
            break;
        }
    }
}
int main()
{
    std::iota(data.begin(), data.end(), 1);
    cnt = data.size() - 1;
    std::vector<std::thread> v;
    for (int n = 0; n < 5; ++n)
        v.emplace_back(reader, n);
    for (auto& t : v)
        t.join();
}

출력:

reader 2 processed item 50
reader 1 processed item 44
reader 4 processed item 46
<....>
reader 0 done
reader 4 done
reader 3 done

결함 보고서

다음 동작 변경 결함 보고서는 이전에 게시된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
P0558R1 C++11 정확한 타입 일치가 요구되었음
T 가 여러 인수에서 추론되었기 때문
T 는 오직
obj 에서만 추론됨

참고 항목

원자적 객체에 저장된 값에서 인자를 원자적으로 빼고 이전에 보유한 값을 얻음
( std::atomic<T> 의 public member function)
원자적 객체에 비원자적 값을 더하고 원자적 객체의 이전 값을 얻음
(function template)
C documentation for atomic_fetch_sub , atomic_fetch_sub_explicit