简体   繁体   English

memory_order_seq_cst 栅栏什么时候有用?

[英]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 :我知道当并非所有原子操作都以“强”顺序完成时,围栏很有用

  • when not all atomic reads (1) in a thread are acquire operations, you may find a use for an acquire fence;当线程中并非所有原子读取 (1) 都是获取操作时,您可能会发现获取栅栏的用途;
  • when not all atomic modifications (1) in a thread are release operations, you may find a use for a release fence.当线程中并非所有原子修改 (1) 都是释放操作时,您可能会发现释放栅栏的用途。

(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 栅栏也是所有顺序一致操作的单一总顺序的一部分,强制执行以下观察:

  • For an atomic operation B that reads the value of an atomic object M , if there is a 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在后面的修改它的修改顺序。
  • For atomic operations A and B on an atomic object M , where A modifies M and B takes its value, if there is a 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上的原子操作AB ,其中A修改M并且B取其值,如果存在memory_order_seq_cst栅栏X使得AX之前排序并且BS 中的X之后,那么B观察到AM在其修改顺序中的后续修改。
  • For atomic operations A and B on an atomic object M , where A modifies M and B takes its value, if there are 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上的原子操作AB ,其中A修改M并且B取其值,如果存在memory_order_seq_cst栅栏XY使得AX之前被排序, YB之前被排序,并且XS 中位于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.

相关问题 atomic_thread_fence(memory_order_seq_cst) 是否具有完整内存屏障的语义? - Does atomic_thread_fence(memory_order_seq_cst) have the semantics of a full memory barrier? output 10 与 memory_order_seq_cst - output 10 with memory_order_seq_cst memory_order_seq_cst 栅栏在 C++20 中如何不再有用? - How are memory_order_seq_cst fences useful anymore in C++20? 在 x86 上实现 std::atomic_thread_fence(std::memory_order_seq_cst) 没有额外的性能损失 - An implementation of std::atomic_thread_fence(std::memory_order_seq_cst) on x86 without extra performance penalties std::memory_order_seq_cst 如何工作 - How std::memory_order_seq_cst works 为什么memory_order_relaxed和memory_order_seq_cst没有区别? - Why does memory_order_relaxed and memory_order_seq_cst make no difference? memory_order_seq_cst 和 memory_order_acq_rel 有何不同? - How do memory_order_seq_cst and memory_order_acq_rel differ? memory_order_seq_cst 和 memory_order_release 的可能排序 - Possible orderings with memory_order_seq_cst and memory_order_release 为什么“memory_order_relaxed”在我的系统中被视为“memory_order_seq_cst”[C++] - Why “memory_order_relaxed” treat as “memory_order_seq_cst” in my system [C++] 为什么memory_order_relaxed的性能与memory_order_seq_cst相同 - why memory_order_relaxed performance is the same as memory_order_seq_cst
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM