gpt4 book ai didi

c++ - seq_cst 排序是否保证立即可见?

转载 作者:太空狗 更新时间:2023-10-29 20:27:55 24 4
gpt4 key购买 nike

N3243 1.10.21 说

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”.

这是否意味着在原子对象上的任何 seq_cst 写入对其他线程立即可见,这些线程使用 seq_cst 顺序读取原子对象?

最佳答案

不,C++ 标准中没有任何内容可以保证立即可见。

原子写入应该在“合理”的时间段内对其他线程可见,但它们不必是立即的,并且没有“合理”的精确定义。

保证的是memory_order_seq_cst 操作的单一总顺序。因此,看不到写入值的读取必须在该总顺序中比写入更早发生。由于此总顺序包含所有 变量和所有 memory_order_seq_cst 操作,如果线程之间存在任何通信,那么写入必须变得非常可见很快。

关于c++ - seq_cst 排序是否保证立即可见?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14846494/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com