std::memory_order 概述

这是一种原子对象的枚举类型.

memory order

Used as an argument to functions that conduct atomic operations to specify how other operations on different threads are synchronized.

用做执行原子操作的函数的参数, 以指定如何同步不同线程上的其他操作.

定义如下:

1 typedef enum memory_order
2 {
3     memory_order_relaxed,   // relaxed
4     memory_order_consume,   // consume
5     memory_order_acquire,   // acquire
6     memory_order_release,   // release
7     memory_order_acq_rel,   // acquire/release
8     memory_order_sql_cst    // sequentially consistent 
9 };

All atomic operations produce well-defined behavior with respect to an atomic object when multiple threads access it: each atomic operation is entirely performed on the object before any other atomic operation can access it. This guarantees no data races on these objects, and this is precisely the feature that defines atomicity.

在多线程访问一个原子对象时, 所有的原子操作在遵从这个原子对象的规则后都会产生良好定义的行为, 即: 所有的原子操作都会在其他原子操作访问原子对象之前完整地执行. 这保证了在这些原子对象上不会发生数据竞争,  这就是定义原子性最准确的特征.

But each thread may perform operations on memory locations other than the atomic object itself: and these other operations may produce visible side effects on other threads. Arguments of this type allow to specify a memory orderfor the operation that determines how these (possibly non-atomic) visible side effects are synchronized among threads, using the atomic operations as synchronization points:

但是每一个线程可能会在不同于原子对象本身的内存地址上执行这些操作. 类似的其他操作可能会在其线程上产生不可忽视的副作用. 这些枚举类型的参数允许在用原子操作作为同步点时, 指定某一种内存指令, 这些内存指定都将保证如何在不同的线程中同步这些不可忽视的副作用(可能是非原子性的).

memory_order_relaxed

The operation is ordered to happen atomically at some point.
This is the loosest memory order, providing no guarantees on how memory accesses in different threads are ordered with respect to the atomic operation.

 操作被设定在某一时刻自动进行.

这是最不精准的一种 memory order, 对不同线程中内存的访问应该如何按照原子操作进行设定, 没有提供任何保证.

memory_order_consume

[Applies to loading operations]
The operation is ordered to happen once all accesses to memory in the releasing thread that carry a dependency on the releasing operation (and that have visible side effects on the loading thread) have happened.

 适用于启动操作

该操作被设定为: 正在释放的线程中一旦所有内存访问都执行了, 并且该操作依赖于释放这个动作(释放操作也会对启动线程有不可忽视的副作用), 那么该操作将执行.

 memory_order_acquire

[Applies to loading operations]
The operation is ordered to happen once all accesses to memory in the releasing thread (that have visible side effects on the loading thread) have happened.

 适用于启动操作

该操作被设定为: 正在释放的线程中一旦所有内存访问都执行了(结束线程也会对启动线程有不可忽视的副作用), 那么该操作将执行.

memory_order_release

 [Applies to storing operations]
The operation is ordered to happen before a consume or acquire operation, serving as a synchronization point for other accesses to memory that may have visible side effects on the loading thread.

 适用于保存操作

该操作被设定为: 在一个消费操作或者获取操作之前, 作为其他访问内存的同步点会对启动线程造成不可忽视的副作用.

memory_order_acq_rel

[Applies to loading/storing operations]
The operation loads acquiring and stores releasing (as defined above for   memory_order_acquire  and  memory_order_release  ).

适用于启动操作和存储操作

 该操作被定义为: 同时启动acquiring类型的操作以及保存releasing类型的操作(正如上面memory_order_acquire和memory_order_release定义的那样).

memory_order_seq_cst

The operation is ordered in a sequentially consistent manner: All operations using this memory order are ordered to happen once all accesses to memory that may have visible side effects on the other threads involved have already happened.
This is the strictest memory order, guaranteeing the least unexpected side effects between thread interactions though the non-atomic memory accesses.
For consume and acquire loads, sequentially consistent store operations are considered releasing operations.

该操作被定义为一种顺序一致性的方式: 一旦所有对内存的访问(可能会对其他线程有不可忽视的副作用)都已经发生了,  则使用使用这种内存指令的所有操作都将按照顺序一致性的方式执行.

这是最严格的一种内存指令, 通过非原子的内存访问, 保证了在线程活动中产生最少的异常副作用.

对于消费方式的启动以及获取方式的启动而言 顺序一致性地的存储操作被认为是一种释放方式的操作.

[p.s.] 本文来自自己对 http://www.cplusplus.com/reference/atomic/memory_order/ 里面内容的翻译, 因为对内存指令以及程序的内存模型认识很浅, 所以翻译地有些拙劣, 有见解的朋友可以在后面留言, 我们一起讨论和完善.

转载请注明出处。

猜你喜欢

转载自www.cnblogs.com/ILoveSouthPark/p/10725661.html
今日推荐