1

The book Java Concurrency in Practice by Brian Goetz et al. describes the happens-before relationship. "To guarantee that the thread executing action B can see the result of action A (whether or not A and B occur in different threads) there must be a happens-before relationship between A and B."

The program order rule is defined as "Each action in a thread happens-before every action in that thread that comes later in the program order."

However I don’t understand how the definition of happens-before can be applied here. It seems to me that 'A happens-before B' only makes sense when A and B are related (e.g write/read of the same volatile variable) and when any number of threads can be involved.

If we have two instructions

a=0 (1)

b=1 (2)

The program order rule says that action (1) happens before action (2) within the thread executing them. But what does that mean since the two operations can be reordered and are unrelated? It seems to me that using the happens-before concept to describe the program order rule is confusing, and that rule should be described using something else.

It should just be said that execution of a program in a thread may occur with some actions reordered but will have the same result as if it were executed in program order?

alfer
  • 355
  • 2
  • 8
  • 1
    @alfer, see https://stackoverflow.com/a/16629365/402428. Specifically the paragraph starting with "happens-before has a very specific meaning in the JMM..." – michid May 21 '22 at 08:54

0 Answers0