[英]When is a memory_order_seq_cst fence useful?
C++ supported atomic thread fences, that is fences guaranteeing properties for thread that use std::atomic<>
operations, with the function atomic_thread_fence
. C++ 支持原子线程栅栏,即通过函数atomic_thread_fence
保证使用std::atomic<>
操作的线程的属性的栅栏。 It takes a memory order parameter to adjust the "strength" of the fence.它需要一个记忆顺序参数来调整围栏的“强度”。
I understand that fences are useful when not all atomic operations are done with a "strong" order :我知道当并非所有原子操作都以“强”顺序完成时,围栏很有用:
(1) that includes RMW operations (1) 包括 RMW 操作
So the usefulness of all these (acquire, release and acq_rel fences) is obvious: they allow threads that use atomic operations weaker than acq/rel (respectively) to synchronize properly.所以所有这些(acquire、release 和 acq_rel 栅栏)的用处是显而易见的:它们允许使用比 acq/rel 弱的原子操作的线程(分别)正确同步。
But I don't understand where memory_order_seq_cst
could be specifically needed as a fence:但我不明白在哪里特别需要memory_order_seq_cst
作为围栏:
What's the implication of using weaker than memory_order_seq_cst
atomic operations and a memory_order_seq_cst
fence?使用比memory_order_seq_cst
原子操作和memory_order_seq_cst
栅栏弱的含义是什么?
What would specifically be guaranteed (in term of possible ordering of atomic operations) by a memory_order_seq_cst
fence that wouldn't be guaranteed by memory_order_acq_rel
?什么将专门由保证(以原子操作的可能的排序的项) memory_order_seq_cst
,才不会被保证围栏memory_order_acq_rel
?
No, a seq-cst-fence is not only both a release and an acquire-fence, but also provides some additional properties (see Working Draft, Standard for Programming Language C++, 32.4.4-32.4.8 ).不,一个 seq-cst-fence 不仅是一个发布和一个获取围栏,而且还提供了一些额外的属性(参见工作草案,编程语言 C++ 标准,32.4.4-32.4.8 )。 A seq-cst fence is also part of the single total order of all sequentially consistent operations, enforcing the following observations: seq-cst 栅栏也是所有顺序一致操作的单一总顺序的一部分,强制执行以下观察:
memory_order_seq_cst
fence X sequenced before B , then B observes either the last memory_order_seq_cst
modification of M preceding X in the total order S or a later modification of M in its modification order.对于读取原子对象M值的原子操作B ,如果存在一个在B之前排序的memory_order_seq_cst
栅栏X ,则B观察到在总顺序S 中X之前M的最后一个memory_order_seq_cst
修改或M在后面的修改它的修改顺序。memory_order_seq_cst
fence X such that A is sequenced before X and B follows X in S , then B observes either the effects of A or a later modification of M in its modification order.对于原子对象M上的原子操作A和B ,其中A修改M并且B取其值,如果存在memory_order_seq_cst
栅栏X使得A在X之前排序并且B在S 中的X之后,那么B观察到A或M在其修改顺序中的后续修改。memory_order_seq_cst
fences X and Y such that A is sequenced before X , Y is sequenced before B , and X precedes Y in S , then B observes either the effects of A or a later modification of M in its modification order.对于原子对象M上的原子操作A和B ,其中A修改M并且B取其值,如果存在memory_order_seq_cst
栅栏X和Y使得A在X之前被排序, Y在B之前被排序,并且X在S 中位于Y之前,然后B观察A的影响或M在其修改顺序中的后续修改。 For example, I am using seq-cst fences in my hazard pointer implementation: https://github.com/mpoeter/xenium/blob/master/xenium/reclamation/impl/hazard_pointer.hpp例如,我在我的危险指针实现中使用了 seq-cst 围栏: https : //github.com/mpoeter/xenium/blob/master/xenium/reclamation/impl/hazard_pointer.hpp
The thread acquiring a safe reference to some object uses seq-cst fence after storing the hazard pointer, but before re-reading the pointer to the object.在存储危险指针之后,但在重新读取指向该对象的指针之前,获取对某个对象的安全引用的线程使用 seq-cst 栅栏。 The thread trying to reclaim some objects uses a seq-cst fence before gathering the active hazard pointers from all threads.尝试回收某些对象的线程在从所有线程收集活动危险指针之前使用 seq-cst 栅栏。 Based on the rules above this ensures that either the thread trying to reclaim the object sees that some other thread has a HP for this object (ie, the object is used), or the reload of thread trying to acquire the safe reference to the object returns a different pointer, indicating to that thread that the object has been removed and it has to perform a retry.基于上述规则,这确保了尝试回收对象的线程看到某个其他线程对此对象具有 HP(即,该对象已被使用),或者重新加载尝试获取该对象的安全引用的线程返回一个不同的指针,向该线程指示该对象已被删除并且它必须执行重试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.