std::atomic_fetch_add,std::atomic_fetch_add_explicit (3) - Linux Manuals
std::atomic_fetch_add,std::atomic_fetch_add_explicit: std::atomic_fetch_add,std::atomic_fetch_add_explicit
NAME
std::atomic_fetch_add,std::atomic_fetch_add_explicit - std::atomic_fetch_add,std::atomic_fetch_add_explicit
Synopsis
Defined in header <atomic>
template< class T >
T atomic_fetch_add( std::atomic<T>* obj,
typename std::atomic<T>::difference_type arg ) noexcept;
template< class T >
T atomic_fetch_add( volatile std::atomic<T>* obj,
typename std::atomic<T>::difference_type arg ) noexcept;
template< class T > (1)
T atomic_fetch_add_explicit( std::atomic<T>* obj,
typename std::atomic<T>::difference_type arg,
std::memory_order order ) noexcept; (2)
template< class T >
T atomic_fetch_add_explicit( volatile std::atomic<T>* obj,
typename std::atomic<T>::difference_type arg,
std::memory_order order ) noexcept;
Performs atomic addition. Atomically adds arg to the value pointed to by obj and returns the value obj held previously. The operation is performed as if the following was executed:
1) obj->fetch_add(arg)
2) obj->fetch_add(arg, order)
Parameters
obj - pointer to the atomic object to modify
arg - the value to add to the value stored in the atomic object
order - the memory synchronization ordering for this operation: all values are permitted.
Return value
The value immediately preceding the effects of this function in the modification_order of *obj.
Possible implementation
Example
Single-writer/multiple-reader lock can be made with fetch_add. Note that this simplistic implementation is not lockout-free
// Run this code