博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
std::memory_order 概述
阅读量:4683 次
发布时间:2019-06-09

本文共 3900 字,大约阅读时间需要 13 分钟。

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

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_order2 {3     memory_order_relaxed,   // relaxed4     memory_order_consume,   // consume5     memory_order_acquire,   // acquire6     memory_order_release,   // release7     memory_order_acq_rel,   // acquire/release8     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.] 本文来自自己对  里面内容的翻译, 因为对内存指令以及程序的内存模型认识很浅, 所以翻译地有些拙劣, 有见解的朋友可以在后面留言, 我们一起讨论和完善.

 

转载请注明出处。

转载于:https://www.cnblogs.com/ILoveSouthPark/p/10725661.html

你可能感兴趣的文章
小组成员及其git链接
查看>>
SQL case when else
查看>>
MVc Identity登陆锁定
查看>>
cdn连接失败是什么意思_关于CDN的原理、术语和应用场景那些事
查看>>
ultraedit26 运行的是试用模式_免费试用U盘数据恢复工具 – 轻松找回U盘丢失的各种数据!...
查看>>
bootstrap table 收缩_bootstrap-table方法之:expandRow-collapseRow,展开或关闭当前行数据...
查看>>
mysql r_mysql:’r’是什么意思?
查看>>
无法加载 mysql 扩展_请检查您的 php 配置. - 文档_无法载入 mysql 扩展 请检查 PHP 配置...
查看>>
python sum函数导入list_python sum函数iterable参数为二维list,start参数为“[]”该如何理解...
查看>>
mysqlin会使用索引吗_被面试官虐了,索引为何使用B+树,你知道吗
查看>>
mysql8单节点500m_Kubernetes 部署 Mysql 8.0 数据库(单节点)
查看>>
mysql数据工厂生产_MySQL超时与天蓝色数据工厂副本
查看>>
python缩进可以用在任何语句之后_每天一道Python选择题--python缩进
查看>>
微信小程序获取用户信息解密AES并且注意如何获取unionid
查看>>
JavaScript设计模式----1
查看>>
Qt实现半透明遮罩效果
查看>>
erlang调优方法
查看>>
Mysql linux -N命令
查看>>
daily scrum 12.5
查看>>
linux-ftp install
查看>>