问题
In JLS, §17.4.5. Happens-before Order, it says that
A program is correctly synchronized if and only if all sequentially consistent executions are free of data races.
It only give us definition about "sequentially consistent", it does not give us definition about "sequentially consistent executions". Only after knowing about what is "sequentially consistent executions", we may make further discussion about the topic.
So what's "sequentially consistent executions" and what's "sequentially consistent executions are free of data races"?
回答1:
An execution has a very simple formal definition: it is simply a total ordering on the set of all memory actions that is under consideration.
A sequentially consistent execution is represented by any total ordering on actions which is sequentially consistent.
The term "free of data races" is also precisely defined by the JLS.
Note that the JLS does not require sequential consistency. In fact, the whole formalism of happens-before exists in order to define precisely the terms under which sequentially inconsistent executions can maintain an illusion of sequential consistency.
回答2:
sequentially consistent executions
means basically that each read operation on a variable sees the last write operation on that variable, no matter on which thread or processor the read/write operations are performed.
However, the JLS does not guarantee sequential consistency out of the box. Programmers have to achieve this consistency by proper synchronisation. Without synchronisation, threads may see inappropriate data, e.g. data that is being modified by another thread at the same time. This is called "data race".
回答3:
To ensure that two actions are free of a data race, you must establish a happens-before relationship between the two actions using any of the five conditions specified in §17.4.5 and recapitulated in Memory Consistency Properties. Once you do that, your program is correctly synchronized with respect to those two actions. All executions of that program will appear to be sequentially consistent, and you can safely ignore any re-ordering permitted in §17.4.3. Programs and Program Order.
回答4:
1) Sequential Consistent executions (by def.): the result of any execution is the same as if the operations of all the processors were executed in some sequential order, and the operations of each individual processor appear in this sequence in the order specified by its program
2) Data Race is a situation when concurrent operations accesses a shared memory location, of which at least one is a write.
3) So if the executions do not appear sequentially consistent their result will differ from the program result or would demonstrate the unexpected behavior. And such executions we don't want.
4) If to suppose that there is a seq. con. execution which is not free of a data race, then it means that there exists another fork of this seq. con. execution which reorders accesses in data race point and leads to the different program result. And then, these executions are not sequentially consistent with the program, or
5) the program allows seq. con. executions to have counterintuitive behaviors and unexpected results, and thus, is not correctly synchronized.
来源:https://stackoverflow.com/questions/12018369/whats-sequentially-consistent-executions-are-free-of-data-races