C++原子操作中的内存顺序 头文件 <atomic>
C++11形式
typedef enum memory_order { memory_order_relaxed, memory_order_consume, memory_order_acquire, memory_order_release, memory_order_acq_rel, memory_order_seq_cst } memory_order; C++20形式
enum class memory_order : /*unspecified*/ { relaxed, consume, acquire, release, acq_rel, seq_cst }; inline constexpr memory_order memory_order_relaxed = memory_order::relaxed; inline constexpr memory_order memory_order_consume = memory_order::consume; inline constexpr memory_order memory_order_acquire = memory_order::acquire; inline constexpr memory_order memory_order_release = memory_order::release; inline constexpr memory_order memory_order_acq_rel = memory_order::acq_rel; inline constexpr memory_order memory_order_seq_cst = memory_order::seq_cst; std::memory_order指定了怎样访问内存,包括常规的、非原子的内存访问、如何围绕原子操作排序。在多核系统中如果没有任何约束,当多个线程同时读写一些变量,一个线程可能以一种顺序观察值的改变不同于其他写入这些值的顺序。确实,这个很明显的顺序改变可能存在于在不同的读取线程之间。一些相似的影响甚至可能发生在单处理器系统,由于编译器内存模型允许的转换。
...