“强烈发生在"之前发生了什么?意思是? [英] What does "strongly happens before" mean?

查看:126
本文介绍了“强烈发生在"之前发生了什么?意思是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++草案标准中,短语强烈发生在……之前"已多次使用.

The phrase "strongly happens before" is used several times in the C++ draft standard.

例如:终止 [basic.start .term]/5

如果完成了带有静态对象的初始化 存储持续时间强烈地发生在对std :: atexit的调用之前(请参阅 ,[support.start.term]),则将对函数的调用传递给 std :: atexit在调用析构函数之前进行排序 目的.如果对std :: atexit的调用强烈发生在 用静态存储完成对象的初始化 持续时间,对对象的析构函数的调用是有序的 在对该函数的调用传递给std :: atexit之前.如果致电 std :: atexit强烈发生在对std :: atexit的另一个调用之前 传递给第二个std :: atexit调用的函数的调用是 在对函数的调用传递给第一个函数之前进行排序 std ::: atexit呼叫.

If the completion of the initialization of an object with static storage duration strongly happens before a call to std​::​atexit (see , [support.start.term]), the call to the function passed to std​::​atexit is sequenced before the call to the destructor for the object. If a call to std​::​atexit strongly happens before the completion of the initialization of an object with static storage duration, the call to the destructor for the object is sequenced before the call to the function passed to std​::​atexit. If a call to std​::​atexit strongly happens before another call to std​::​atexit, the call to the function passed to the second std​::​atexit call is sequenced before the call to the function passed to the first std​::​atexit call.

并在 数据竞赛 [intro.races]/12

如果出现以下情况之一,则评估A会在评估D之前发生:

An evaluation A strongly happens before an evaluation D if, either

(12.1)A在D之前排序,或者

(12.1) A is sequenced before D, or

(12.2)A与D同步,并且 A和D都是顺序​​一致的原子操作 ([atomics.order])或

(12.2) A synchronizes with D, and both A and D are sequentially consistent atomic operations ([atomics.order]), or

(12.3)存在评估B和C使得A 在B之前被排序,B仅仅在C之前发生,并且C被排序 在D之前,或

(12.3) there are evaluations B and C such that A is sequenced before B, B simply happens before C, and C is sequenced before D, or

(12.4)存在一个评估B,使得A强烈 发生在B之前,而B强烈发生在D之前.

(12.4) there is an evaluation B such that A strongly happens before B, and B strongly happens before D.

[注意:非正式地,如果A强烈发生在B之前,则A似乎 在所有情况下都要先于B得到评估.强烈发生在 排除消耗操作. —尾注]

[ Note: Informally, if A strongly happens before B, then A appears to be evaluated before B in all contexts. Strongly happens before excludes consume operations. — end note ]

为什么要引入在此之前确实发生"?凭直觉,它与之前发生的事情"有什么区别和关系?

Why was "strongly happens before" introduced? Intuitively, what's its difference and relation with "happens before"?

注释中的在所有情况下A似乎都在B之前被评估"是什么意思?

(注意:此问题的动机是彼得·科德斯在此答案下的评论.)

(Note: the motivation for this question are Peter Cordes's comments under this answer.)

附加的标准报价草稿(感谢Peter Cordes)

Additional draft standard quote (thanks to Peter Cordes)

顺序和一致性 [atomics.order]/4

所有memory_order :: seq_cst上只有一个总订单S 满足以下条件的操作(包括围栏) 约束.首先,如果A和B是memory_order ::: seq_cst 操作,并且A强烈地发生在B之前,然后A在S中先于B. 其次,对于对象M上的每对原子操作A和B, 其中A在B之前是相干顺序的,以下四个条件 必须由S满足:

There is a single total order S on all memory_­order​::​seq_­cst operations, including fences, that satisfies the following constraints. First, if A and B are memory_­order​::​seq_­cst operations and A strongly happens before B, then A precedes B in S. Second, for every pair of atomic operations A and B on an object M, where A is coherence-ordered before B, the following four conditions are required to be satisfied by S:

(4.1)如果A和B都是memory_order ::: seq_cst操作,则A 在S中先于B;和

(4.1) if A and B are both memory_­order​::​seq_­cst operations, then A precedes B in S; and

(4.2)如果A是一个memory_order ::: seq_cst操作而B发生了 在memory_order ::: seq_cst栅栏Y之前,然后A在S中在Y之前; 和

(4.2) if A is a memory_­order​::​seq_­cst operation and B happens before a memory_­order​::​seq_­cst fence Y, then A precedes Y in S; and

(4.3)如果memory_order :: seq_cst栅栏X发生在A和B之前 一个memory_order ::: seq_cst操作,则X在S中位于B之前;和

(4.3) if a memory_­order​::​seq_­cst fence X happens before A and B is a memory_­order​::​seq_­cst operation, then X precedes B in S; and

(4.4)如果memory_order :: seq_cst栅栏X发生在A和B之前 发生在memory_order :: seq_cst栅栏Y之前,然后X在Y之前 在S.

(4.4) if a memory_­order​::​seq_­cst fence X happens before A and B happens before a memory_­order​::​seq_­cst fence Y, then X precedes Y in S.

推荐答案

为什么要引入在此之前确实发生"?凭直觉,它是什么 与之前发生过"的区别和联系?

Why was "strongly happens before" introduced? Intuitively, what's its difference and relation with "happens before"?

也要做好事前发生"的准备! 看一下当前的cppref快照 https://en.cppreference.com/w/cpp/atomic/memory_order

Brace yourself for "simply happens-before" as well! Take a look at this current snapshot of cppref https://en.cppreference.com/w/cpp/atomic/memory_order

在C ++ 20中似乎添加了仅在发生之前".

It seems "simply happens-before" is added in C++20.

仅在发生之前

与线程无关,只要满足以下任一条件,评估A就会在评估B之前发生:

Regardless of threads, evaluation A simply happens-before evaluation B if any of the following is true:

1)A在B之前排序

2)A与B同步

3)A发生在X之前,而X发生在B之前

3) A simply happens-before X, and X simply happens-before B

注意:如果不执行消耗操作,则只需在发生之前和之后 发生在关系相同之前.

Note: without consume operations, simply happens-before and happens-before relations are the same.

因此Simply-HB和HB相同,除了它们处理消耗操作的方式不同.参见HB

So Simply-HB and HB are the same except for how they handle consume operations. See HB

发生之前

如果满足以下任一条件,则无论线程如何,评估A都会在评估B之前发生:

Regardless of threads, evaluation A happens-before evaluation B if any of the following is true:

1)A在B之前排序

2)线程间发生在B之前

2) A inter-thread happens before B

需要实现以确保之前发生的关系是非循环的,如果需要的话可以通过引入额外的同步来实现(仅在涉及消耗操作的情况下才有必要,请参见Batty等) al)

The implementation is required to ensure that the happens-before relation is acyclic, by introducing additional synchronization if necessary (it can only be necessary if a consume operation is involved, see Batty et al)

它们在消费方面有何不同?参见Inter-Thread-HB

How do they differ with regard to consume? See Inter-Thread-HB

线程间发生-之前

在线程之间进行评估A在评估B之前发生线程间 如果满足以下任何条件

Between threads, evaluation A inter-thread happens before evaluation B if any of the following is true

1)A与B同步

2)A在B之前是依序排列的

2) A is dependency-ordered before B

3)...

...

按依赖关系排序的操作(即使用发布/使用)是HB,但不一定是Simply-HB.

An operation that is dependency ordered (i.e. uses release/consume) is HB but not necessarily Simply-HB.

消费比获取更轻松,因此,如果我理解正确,HB比Simply-HB更轻松.

Consume is more relaxed than acquire, so if I understand correctly, HB is more relaxed than Simply-HB.

强烈发生-在之前

不管线程如何,强烈评价A 如果满足以下任一条件,则在评估B之前发生:

Regardless of threads, evaluation A strongly happens-before evaluation B if any of the following is true:

1)A在B之前排序

2)A与B同步,并且A和B都是顺序一致的原子操作

2) A synchronizes with B, and both A and B are sequentially consistent atomic operations

3)A在X之前被排序,X在Y之前简单地发生,而Y在B之前被排序

3) A is sequenced-before X, X simply happens-before Y, and Y is sequenced-before B

4)A强烈发生在X之前,而X强烈发生在B之前

4) A strongly happens-before X, and X strongly happens-before B

注意:非正式地,如果A强烈发生在B之前,那么A似乎是 在所有情况下都在B之前评估.

Note: informally, if A strongly happens-before B, then A appears to be evaluated before B in all contexts.

注意:强烈发生-在排除消耗操作之前.

Note: strongly happens-before excludes consume operations.

因此,发布/消费操作不能是Strongly-HB.

So a release/consume operation cannot be Strongly-HB.

发布/获取可以是HB,也可以是Simply-HB(因为发布/获取与之同步),但不一定是Strong-HB.因为Strongly-HB特别指出A必须与B同步,并且是顺序一致的操作.

Release/acquire can be HB and Simply-HB (because release/acquire synchronizes-with) but is not necessarily Strongly-HB. Because Strongly-HB specifically says that A must synchronize-with B AND be a Sequentially Consistent operation.

                            Is happens-before guaranteed?

                        HB             Simply-HB          Strongly-HB

relaxed                 no                 no                 no
release/consume        yes                 no                 no      
release/acquire        yes                yes                 no
S.C.                   yes                yes                yes

音符是什么意思?

What does the "A appears to be evaluated before B in all contexts" in the note mean?

所有上下文:所有线程/所有CPU看到(或最终会同意")相同的顺序.这是顺序一致性的保证-所有变量的全局总修改顺序.获取/释放链仅保证参与链的线程的感知修改顺序.从理论上讲,链外的线程可以看到不同的顺序.

All contexts: All threads / all CPUs see (or "will eventually agree on") the same order. This is the guarantee of sequential consistency--a global total modification order of all variables. Acquire/release chains only guarantee perceived modification order for threads participating in the chain. Threads outside the chain are theoretically allowed to see a different order.

我不知道为什么引入Strongly-HB和Simply-HB.也许可以帮助阐明如何围绕消耗进行操作? Strongly-HB具有很好的属性-如果一个线程在B之前观察到A强烈发生,则它知道所有线程都将观察到同一件事.

I do not know why Strongly-HB and Simply-HB were introduced. Maybe to help clarify how to operate around consume? Strongly-HB has a nice properties--if one thread observes A strongly-happens-before B, it knows all threads will observe the same thing.

消费记录:

Paul E. McKenney负责使用C和C ++标准.消费保证指针分配与其指向的内存之间的顺序.它是由于DEC Alpha发明的. DEC Alpha可以推测性地取消对指针的引用,因此它还具有一个内存栅栏来防止这种情况. DEC Alpha不再生产,并且今天没有处理器具有此行为.打算使消费非常轻松.

Paul E. McKenney is responsible for consume being in the C and C++ standards. Consume guarantees ordering between pointer assignment and the memory it points to. It was invented because of the DEC Alpha. The DEC Alpha could speculatively dereference a pointer, thus it also had a memory fence to prevent this. The DEC Alpha is no longer made and no processors today have this behavior. Consume is intended to be very relaxed.

这篇关于“强烈发生在"之前发生了什么?意思是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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