命名空间
变体
操作

std::atomic<T>::operator++,++(int),--,--(int)

来自 cppreference.cn
< cpp‎ | atomic‎ | atomic
 
 
并发支持库
线程
(C++11)
(C++20)
this_thread 命名空间
(C++11)
(C++11)
(C++11)
协同取消
互斥
(C++11)
通用锁管理
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
条件变量
(C++11)
信号量
门闩和屏障
(C++20)
(C++20)
期值
(C++11)
(C++11)
(C++11)
(C++11)
安全回收
(C++26)
危险指针
原子类型
(C++11)
(C++20)
原子类型的初始化
(C++11)(C++20 中已弃用)
(C++11)(C++20 中已弃用)
内存排序
(C++11)(C++26 中已弃用)
原子操作的自由函数
原子标志的自由函数
 
 
仅是 atomic<Integral > 特化
T operator++() noexcept;
(1) (C++11 起)
T operator++() volatile noexcept;
(2) (C++11 起)
T operator++( int ) noexcept;
(3) (C++11 起)
T operator++( int ) volatile noexcept;
(4) (C++11 起)
T operator--() noexcept;
(5) (C++11 起)
T operator--() volatile noexcept;
(6) (C++11 起)
T operator--( int ) noexcept;
(7) (C++11 起)
T operator--( int ) volatile noexcept;
(8) (C++11 起)
仅是 atomic<T*> 偏特化成员
T* operator++() noexcept;
(9) (C++11 起)
T* operator++() volatile noexcept;
(10) (C++11 起)
T* operator++( int ) noexcept;
(11) (C++11 起)
T* operator++( int ) volatile noexcept;
(12) (C++11 起)
T* operator--() noexcept;
(13) (C++11 起)
T* operator--() volatile noexcept;
(14) (C++11 起)
T* operator--( int ) noexcept;
(15) (C++11 起)
T* operator--( int ) volatile noexcept;
(16) (C++11 起)

原子地递增或递减当前值。此操作是读-修改-写操作。

  • operator++() 执行原子前置递增。等价于 return fetch_add(1) + 1;
  • operator++(int) 执行原子后置递增。等价于 return fetch_add(1);
  • operator--() 执行原子前置递减。等价于 return fetch_sub(1) - 1;
  • operator--(int) 执行原子后置递减。等价于 return fetch_sub(1);
1-8) 对于有符号整数类型,算术运算定义为使用二补数表示。没有未定义的结果。
9-16) 结果可能是一个未定义的地址,但操作本身没有未定义行为。
如果 T 不是完整对象类型,则程序格式错误。

如果 std::atomic<T>::is_always_lock_free 为 false 并且任何 volatile 重载参与重载决议,则此操作已弃用。

(C++20 起)

目录

[编辑] 返回值

operator++()operator--() 返回修改后的原子变量的值。形式上,它们返回在 *this修改顺序中紧接此函数效果之前的值递增/递减后的结果。

operator++(int)operator--(int) 返回修改前的原子变量的值。形式上,它们返回在 *this修改顺序中紧接此函数效果之前的值。

[编辑] 注意

与大多数前置递增和前置递减运算符不同,原子类型的前置递增和前置递减运算符不返回对修改后对象的引用。它们返回存储值的副本。

[编辑] 示例

#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, "█");
}

可能的输出

▒  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

[编辑] 缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

缺陷报告 应用于 发布时的行为 正确的行为
P0558R1 C++11 允许对(可能带 cv 限定符的)void 或函数指针进行算术运算 导致格式错误

[编辑] 参阅

原子地将参数添加到原子对象中存储的值,并获取之前持有的值
(公有成员函数) [编辑]
原子地从原子对象中存储的值中减去参数,并获取之前持有的值
(公有成员函数) [编辑]
对原子值进行加减
(公有成员函数) [编辑]
对原子值执行按位与、或、异或运算
(公有成员函数) [编辑]
English Deutsch 日本語 中文(简体) 中文(繁體)