如何memory_order_seq_cst和memory_order_acq_rel不同? [英] How do memory_order_seq_cst and memory_order_acq_rel differ?

查看:1602
本文介绍了如何memory_order_seq_cst和memory_order_acq_rel不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

存储是释放操作,加载是两者的获取操作。我知道 memory_order_seq_cst 意味着对所有操作强加一个额外的总排序,但我没有建立一个例子,如果所有的 memory_order_seq_cst 替换为 memory_order_acq_rel

Stores are release operations and loads are acquire operations for both. I know that memory_order_seq_cst is meant to impose an additional total ordering for all operations, but I'm failing to build an example where it isn't the case if all the memory_order_seq_cst are replaced by memory_order_acq_rel.

或者差异只是一个文档效应,即如果有人打算不使用更宽松的模型,并使用 memory_order_acq_rel memory_order_seq_cst c $ c>当约束宽松的模型?

Do I miss something, or the difference is just a documentation effect, i.e. one should use memory_order_seq_cst if one intend not to play with a more relaxed model and use memory_order_acq_rel when constraining the relaxed model?

推荐答案

http://en.cppreference.com/w/cpp/atomic/memory_order 在底部有一个很好的例子,只有 memory_order_seq_cst 。基本上 memory_order_acq_rel 提供相对于原子变量的读取和写入顺序,而 memory_order_seq_cst 提供全局读取和写入顺序。也就是说,顺序一致的操作在所有线程中以相同的顺序可见。

http://en.cppreference.com/w/cpp/atomic/memory_order has a good example at the bottom that only works with memory_order_seq_cst. Essentially memory_order_acq_rel provides read and write orderings relative to the atomic variable, while memory_order_seq_cst provides read and write ordering globally. That is, the sequentially consistent operations are visible in the same order across all threads.

示例归结为:

bool x= false;
bool y= false;
int z= 0;

a() { x= true; }
b() { y= true; }
c() { while (!x); if (y) z++; }
d() { while (!y); if (x) z++; }

// kick off a, b, c, d, join all threads
assert(z!=0);

z 上的操作受两个原子变量,而不是一个,所以你不能使用acquire-release语义来强制 z 总是递增。

Operations on z are guarded by two atomic variables, not one, so you can't use acquire-release semantics to enforce that z is always incremented.

这篇关于如何memory_order_seq_cst和memory_order_acq_rel不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆