如何理解发生 - 在一致之前 [英] How to understand happens-before consistent

查看:105
本文介绍了如何理解发生 - 在一致之前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第17章JLS ,它引入了一个概念:发生在一致之前。

In chapter 17 of JLS, it introduce a concept: happens-before consistent.


一组动作A发生 - 如果是所有读取在A中,其中W(r)是r看到的写入动作,不是hb(r,W(r))或者在A中存在写w的情况,wv = rv和hb(W(r),w)和hb(w,r)

A set of actions A is happens-before consistent if for all reads r in A, where W(r) is the write action seen by r, it is not the case that either hb(r, W(r)) or that there exists a write w in A such that w.v = r.v and hb(W(r), w) and hb(w, r)"

在我的理解中,它等于以下词:
...,既不是......也不是......

In my understanding, it equals to following words: ..., it is the case that neither ... nor ...

所以我的前两个问题是:

So my first two questions are:


  • 我的理解是对吗?

  • wv = rv是什么意思?

它还给出了一个示例:17.4.5-1

It also gives an Example: 17.4.5-1

Thread 1 Thread 2

B = 1; A = 2; 

r2 = A; r1 = B; 

首次执行订单:

1: B = 1;

3: A = 2;

2: r2 = A;  // sees initial write of 0

4: r1 = B;  // sees initial write of 0

订单本身已告诉我们两个线程交替执行,所以我的第三个问题是:左数是什么意思?

The order itself has already told us that two threads are executed alternately, so my third question is: what does left number mean?

根据我的理解,r2和r1的原因可以看出初始写0是A和B都是不易变形的领域。所以我的第四个问题是:我的理解是否合适?

In my understanding, the reason of both r2 and r1 can see initial write of 0 is both A and B are not volatile field. So my fourth quesiton is: whether my understanding is right?

第二个执行顺序:

1: r2 = A;  // sees write of A = 2

3: r1 = B;  // sees write of B = 1

2: B = 1;

4: A = 2;

根据事先一致性的定义,不难理解这种执行顺序是否发生 - 在一致之前(如果我的第一个理解是正确的)。
所以我的第五和第六个问题是:在现实世界中是否存在这种情况(读取后面会发生的写入)?如果确实如此,你能给我一个真实的例子吗?

According to definition of happens-before consistency, it is not difficult to understand this execution order is happens-before consistent(if my first understanding is correct). So my fifth and sixth questions are: does it exist this situation (reads see writes that occur later) in real world? If it does, could you give me a real example?

推荐答案

每个线程可以在具有自己缓存的不同核心上。这意味着一个线程可以写入存储在寄存器或其本地缓存中的值,并且该值在一段时间内对另一个线程不可见。 (毫秒并不罕见)

Each thread can be on a different core with its own cache. This means that one thread can write to a value storing in a register, or its local cache, and this value is not visible to another thread for some time. (milli-seconds is not uncommon)

一个更极端的例子是读取线程的代码被优化,假设因为它永远不会改变值,所以它不会需要从内存中读取它。在这种情况下,优化代码永远不会看到由另一个线程执行的更改。

A more extreme example is that the reading thread's code is optimised with the assumption that since it never changes the value, it doesn't need to read it from memory. In this case the optimised code never sees the change performed by another thread.

在这两种情况下,使用 volatile 确保读取和写入以一致的顺序发生,并且两个线程都看到相同的值。这有时被描述为始终从主存储器读取,但并非必须如此,因为高速缓存可以直接相互通信。 (因此,性能影响远小于您的预期)

In both cases, the use of volatile ensures that reads and write occur in a consistent order and both threads see the same value. This is sometimes described as always reading from main memory, though it doesn't have to be the case because the caches can talk to each other directly. (So the performance hit is much smaller than you might expect)

这篇关于如何理解发生 - 在一致之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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