2
$\begingroup$

In section 17.4.5 "Happens-before Order" of Java Language Specification, a trace is examined for the notion of happens-before consistency.

The trace is shown in the figure in which $A$ and $B$ are shared variables with initially $A = B = 0$.

trace

An execution order of the trace is as follows.

executionorder

In this execution, the reads see writes that occur later in the execution order. The document says that it is happens-before consistent. I am quite confused about this. In my opinion, this execution order does not even satisfy program order. Why is it happens-before consistent?

$\endgroup$
  • $\begingroup$ The trace is presented in inverse order, i.e., A = 2 is executed first, and r2 = A is executed last. $\endgroup$ – Yuval Filmus Mar 3 '14 at 18:50
  • $\begingroup$ I don't understand it. The document remarks that In this execution, the reads see writes that occur later in the execution order. How could this happen? $\endgroup$ – hengxin Mar 4 '14 at 1:39
  • $\begingroup$ I suggest you read the entire subsection. It seems that the idea is that although the writes occur out of order, semantically they occur in order. For this trickery to work, the reads should behave as if the previous (wrt program order) writes have happened, even though in reality they "happen" later. $\endgroup$ – Yuval Filmus Mar 4 '14 at 2:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.