seq_cst排序是否保证立即可见? [英] Does seq_cst ordering guarantee immediate visibility?

查看:278
本文介绍了seq_cst排序是否保证立即可见?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

N3243 1.10.21说


可以证明正确使用互斥量和memory_order_
seq_cst操作的程序可以防止所有数据争用并且不使用其他同步操作,就好像它们的组成线程执行的
操作只是交错进行一样,每个
对象的值计算均取自该对象的最后一个副作用交织。这通常称为顺序一致性。


这是否意味着任何 seq_cst 都写在原子对象对于其他按 seq_cst 顺序读取原子对象的线程是立即可见的?

解决方案

否,C ++标准中没有任何东西可以保证立即可见。



原子写入应该在合理的时间内对其他线程可见。

所保证的是单个总订单 memory_order_seq_cst 操作。因此,未看到写入值的读取必须以总顺序早于写入的顺序发生。由于此总订单包含所有变量和所有 memory_order_seq_cst 操作,因此,如果线程之间完全没有通信,然后必须很快就能看到写入内容。


N3243 1.10.21 says

It can be shown that programs that correctly use mutexes and memory_order_ seq_cst operations to prevent all data races and use no other synchronization operations behave as if the operations executed by their constituent threads were simply interleaved, with each value computation of an object being taken from the last side effect on that object in that interleaving. This is normally referred to as "sequential consistency".

Does this mean that any seq_cst writes on an atomic object are immediately visible to other threads which read the atomic object with seq_cst ordering ?

解决方案

No, there is nothing in the C++ standard that guarantees immediate visibility.

Atomic writes should become visible to other threads within a "reasonable" period of time, but they do not have to be immediate, and there is no precise definition of "reasonable".

What is guaranteed is that there is a single total order of memory_order_seq_cst operations. A read that does not see the value written must therefore occur earlier in that total order than the write. Since this total order encompasses all variables and all memory_order_seq_cst operations, if there is any communication between the threads at all, then writes must become visible pretty quickly.

这篇关于seq_cst排序是否保证立即可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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