std::atomic::operator+=,-=(3)
π std::atomic::operator+=,-= π
Bestie, this page is from the C++ Standard Libary manual (yes, that's the manual name, we keep it π
), and it's all about the atomic += and -= operators. Read-modify-write main character energy, no cap. π¦
πΈ NAME πΈ
std::atomic::operator+=,-= β std::atomic::operator+=,-=
β¨ SYNOPSIS β¨
Okay so first: these are member only of atomic<Integral > specializations and atomic<Floating > specializations (since C++20). π
T operator+=( T arg ) noexcept; (1) (since C++11)
T operator+=( T arg ) volatile noexcept; (2) (since C++11)
T operator-=( T arg ) noexcept; (3) (since C++11)
T operator-=( T arg ) volatile noexcept; (4) (since C++11)
And these are member only of the atomic<T*> partial specialization:
T* operator+=( std::ptrdiff_t arg ) noexcept; (5) (since C++11)
T* operator+=( std::ptrdiff_t arg ) volatile noexcept; (6) (since C++11)
T* operator-=( std::ptrdiff_t arg ) noexcept; (7) (since C++11)
T* operator-=( std::ptrdiff_t arg ) volatile noexcept; (8) (since C++11)
So what's the vibe? It atomically replaces the current value with the result of computation involving the previous value and arg. The operation is a read-modify-write operation. π«
operator+=performs atomic addition. Equivalent toreturn fetch_add(arg) + arg;.operator-=performs atomic subtraction. Equivalent toreturn fetch_sub(arg) - arg;.
Here's the breakdown, bestie:
1-4)For signed integral types, arithmetic is defined to use twoβs complement representation. There are no undefined results. π«Ά
For floating-point types, the floating-point environment in effect may be different from the calling thread's floating-point environment. The operation need not be conform to the corresponding std::numeric_limits (since C++20) traits but is encouraged to do so. If the result is not a representable value for its type, the result is unspecified but the operation otherwise has no undefined behavior.
5-8)The result may be an undefined address, but the operations otherwise have no undefined behavior. IfTis not a complete object type, the program is ill-formed.
π¨ Warning, bestie: it is deprecated ifstd::atomic<T>::is_always_lock_freeis false and (since C++20) anyvolatileoverload participates in overload resolution. Like, noted. π
π PARAMETERS π
argβ the argument for the arithmetic operation.
π« RETURN VALUE π«
The resulting value β that is, the result of applying the corresponding binary operator to the value immediately preceding the effects of the corresponding member function in the modification order of *this. β¨
π NOTES π
Unlike most compound assignment operators, the compound assignment operators for atomic types do not return a reference to their left-hand arguments. They return a copy of the stored value instead. Iconic behavior, honestly. π
π DEFECT REPORTS π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. History really said "let's fix it later" π
P0558R1β applied toC++11.- Behavior as published: arithmetic permitted on pointers to (possibly cv-qualified)
voidor function. - Correct behavior: made ill-formed.
- Behavior as published: arithmetic permitted on pointers to (possibly cv-qualified)
π₯ SEE ALSO π₯
fetch_addβ atomically adds the argument to the value stored in the atomic object and obtains the value held previously (public member function).fetch_subβ atomically subtracts the argument from the value stored in the atomic object and obtains the value held previously (public member function).operator++,operator++(int),operator--,operator--(int)β increments or decrements the atomic value by one (public member function).operator&=,operator|=,operator^=β performs bitwise AND, OR, XOR with the atomic value (public member function).
Source/version: http://cppreference.com β’ date: 2024.06.10 β’ page: std::atomic::operator+=,-=(3). Go atomically slay, bestie π
π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms