atomic_atomic_ref
atomic_atomic_ref (원자적 참조 래퍼)
std::atomic_ref는 기존 객체를 원자적으로 접근할 수 있게 하는 클래스 템플릿이에요. 객체를 복사하지 않고 원자적 연산을 적용할 수 있어요.
출처: cppreference
본문
std::atomic_ref는 <atomic> 헤더에 정의된 클래스 템플릿이에요. C++20부터 사용 가능해요.
template< class T >
class atomic_ref;
std::atomic_ref는 기존 비원자적 객체에 대한 원자적 참조를 제공해요. 객체를 소유하지 않으며, 참조하는 객체에 대한 원자적 연산(load, store, fetch_add, compare_exchange 등)을 지원해요. T는 trivially copyable이어야 해요.
std::atomic_ref의 수명 동안 같은 객체를 참조하는 다른 atomic_ref로도 접근할 수 있어요. 참조한 객체는 atomic_ref가 존재하는 동안 다른 스레드로부터 atomic_ref들을 통해서만 접근해야 해요.
예제 (Example)
#include <atomic>
#include <iostream>
int counter = 0;
int main()
{
std::atomic_ref<int> ref(counter);
ref.fetch_add(5);
std::cout << counter << '\n'; // 5
}