gpt4 book ai didi

c++ - C/C++ 基本类型是原子的吗?

转载 作者:IT老高 更新时间:2023-10-28 13:22:40 26 4
gpt4 key购买 nike

是 C/C++ 基本类型,如 intdouble 等,是原子的,例如线程安全?

他们没有数据竞争吗?也就是说,如果一个线程写入这种类型的对象,而另一个线程从中读取,行为是否定义良好?

如果不是,它取决于编译器还是其他什么?

最佳答案

不,基本数据类型(例如 intdouble )不是原子的,请参阅 std::atomic

您可以改为使用 std::atomic<int>std::atomic<double>

注意: std::atomic 是在 C++11 中引入的,我的理解是在 C++11 之前,C++ 标准根本不承认多线程的存在。


正如@Josh 所指出的, std::atomic_flag 是一种原子 bool 类型。与 std::atomic 特化不同,它保证无锁


引用的文档来自:http://open-std.org/JTC1/SC22/WG21/docs/papers/2015/n4567.pdf。我很确定该标准不是免费的,因此这不是最终/官方版本。

1.10 多线程执行和数据竞争

  1. Two expression evaluations conflict if one of them modifies a memory location (1.7) and the other one reads or modifies the same memory location.
  2. The library defines a number of atomic operations (Clause 29) and operations on mutexes (Clause 30) that are specially identified as synchronization operations. These operations play a special role in making assignments in one thread visible to another. A synchronization operation on one or more memory locations is either a consume operation, an acquire operation, a release operation, or both an acquire and release operation. A synchronization operation without an associated memory location is a fence and can be either an acquire fence, a release fence, or both an acquire and release fence. In addition, there are relaxed atomic operations, which are not synchronization operations, and atomic read-modify-write operations, which have special characteristics.


  1. Two actions are potentially concurrent if
    (23.1) — they are performed by different threads, or
    (23.2) — they are unsequenced, and at least one is performed by a signal handler.
    The execution of a program contains a data race if it contains two potentially concurrent conflicting actions, at least one of which is not atomic, and neither happens before the other, except for the special case for signal handlers described below. Any such data race results in undefined behavior.

29.5 原子类型

  1. There shall be explicit specializations of the atomic template for the integral types ``char, signed char, unsigned char, short, unsigned short, int, unsigned int, long, unsigned long, long long, unsigned long long, char16_t, char32_t, wchar_t, and any other types needed by the typedefs in the header <cstdint>. For each integral type integral, the specialization atomic<integral> provides additional atomic operations appropriate to integral types. There shall be a specialization atomic<bool> which provides the general atomic operations as specified in 29.6.1..


  1. There shall be pointer partial specializations of the atomic class template. These specializations shall have standard layout, trivial default constructors, and trivial destructors. They shall each support aggregate initialization syntax.

29.7 标志类型和操作

  1. Operations on an object of type atomic_flag shall be lock-free. [ Note: Hence the operations should also be address-free. No other type requires lock-free operations, so the atomic_flag type is the minimum hardware-implemented type needed to conform to this International standard. The remaining types can be emulated with atomic_flag, though with less than ideal properties. — end note ]

关于c++ - C/C++ 基本类型是原子的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35226128/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com