std::atomic...,--,--(int)(3)
okay so bestie, welcome to the C++ Standard Libary manual π this page is all about std::atomic::operator++,++(int),--,--(int), and itβs literally the atomic glow-up of increment/decrement. no cap, every overload is here.
π NAME π
std::atomic::operator++,++(int),--,--(int) β std::atomic::operator++,++(int),--,--(int)
π¦ Synopsis π¦
member only of atomic<Integral > specializations
T operator++() noexcept; (1) (since C++11)
T operator++() volatile noexcept; (2) (since C++11)
T operator++( int ) noexcept; (3) (since C++11)
T operator++( int ) volatile noexcept; (4) (since C++11)
T operator--() noexcept; (5) (since C++11)
T operator--() volatile noexcept; (6) (since C++11)
T operator--( int ) noexcept; (7) (since C++11)
T operator--( int ) volatile noexcept; (8) (since C++11)
member only of atomic<T*> partial specialization
T* operator++() noexcept; (9) (since C++11)
T* operator++() volatile noexcept; (10) (since C++11)
T* operator++( int ) noexcept; (11) (since C++11)
T* operator++( int ) volatile noexcept; (12) (since C++11)
T* operator--() noexcept; (13) (since C++11)
T* operator--() volatile noexcept; (14) (since C++11)
T* operator--( int ) noexcept; (15) (since C++11)
T* operator--( int ) volatile noexcept; (16) (since C++11)
okay so what does it do? It atomically increments or decrements the current value. The operation is a read-modify-write operation. π«Ά
operator++()performs atomic pre-increment. Equivalent toreturn fetch_add(1) + 1;.operator++(int)performs atomic post-increment. Equivalent toreturn fetch_add(1);.operator--()performs atomic pre-decrement. Equivalent toreturn fetch_sub(1) - 1;.operator--(int)performs atomic post-decrement. Equivalent toreturn fetch_sub(1);.
1-8) For signed integral types, arithmetic is defined to use twoβs complement representation. There are no undefined results.
9-16) The result may be an undefined address, but the operations otherwise have no undefined behavior. If T is not a complete object type, the program is ill-formed.
β οΈ It is deprecated if std::atomic<T>::is_always_lock_free is false and (since C++20) any volatile overload participates in overload resolution.
π« Return value π«
operator++() and operator--() return the value of the atomic variable after the modification. Formally, the result of incrementing/decrementing the value immediately preceding the effects of this function in the modification order of *this.
operator++(int) and operator--(int) return the value of the atomic variable before the modification. Formally, the value immediately preceding the effects of this function in the modification order of *this.
πΈ Notes πΈ
Unlike most pre-increment and pre-decrement operators, the pre-increment and pre-decrement operators for atomic types do not return a reference to the modified object. They return a copy of the stored value instead. fr fr, thatβs the whole vibe.
π Example π
this example is iconic, bestie β hereβs the code exactly:
// Run this code
#include <atomic>
#include <chrono>
#include <iomanip>
#include <iostream>
#include <mutex>
#include <random>
#include <string>
#include <thread>
std::atomic<int> atomic_count{0};
std::mutex cout_mutex;
int completed_writes{0};
constexpr int global_max_count{72};
constexpr int writes_per_line{8};
constexpr int max_delay{100};
template<int Max>
int random_value()
{
static std::uniform_int_distribution<int> distr{1, Max};
static std::random_device engine;
static std::mt19937 noise{engine()};
static std::mutex rand_mutex;
std::lock_guard lock{rand_mutex};
return distr(noise);
}
int main()
{
auto work = [](const std::string id)
{
for (int count{}; (count = ++atomic_count) <=
global_max_count;)
{
std::this_thread::sleep_for(
std::chrono::milliseconds(random_value<max_delay>()));
// print thread `id` and `count` value
{
std::lock_guard lock{cout_mutex};
const bool new_line = ++completed_writes %
writes_per_line == 0;
std::cout << id << std::setw(3) << count << " "
<< (new_line ? "\n" : "") << std::flush;
}
}
};
std::jthread j1(work, "β"), j2(work, "β"), j3(work, "β"), j4(work,
"β");
}
π Possible output π
β 2 β 1 β 5 β 7 β 4 β 6 β 3 β 8
β 11 β 9 β 13 β 10 β 14 β 12 β 16 β 19
β 15 β 18 β 21 β 22 β 17 β 25 β 24 β 26
β 20 β 29 β 27 β 23 β 31 β 33 β 32 β 28
β 30 β 37 β 34 β 35 β 36 β 41 β 40 β 39
β 38 β 43 β 42 β 46 β 48 β 47 β 50 β 45
β 44 β 53 β 54 β 49 β 55 β 51 β 57 β 58
β 52 β 56 β 61 β 59 β 60 β 62 β 64 β 63
β 68 β 66 β 65 β 71 β 67 β 70 β 69 β 72
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
P0558R1β applied to C++11. Behavior as published: arithmetic permitted on pointers to (possibly cv-qualified) void or function. Correct behavior: made ill-formed.
π 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-=β adds to or subtracts from the atomic value (public member function).operator&=,operator|=,operator^=β performs bitwise AND, OR, XOR with the atomic value (public member function).
okay bye bestie π
source/version vibes: http://cppreference.com, date 2024.06.10, and this whole page is std::atomic...,--,--(int)(3). periodt. π
π¦ 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