On Tuesday, 22 October 2013 at 12:46:16 UTC, Moritz Maxeiner
wrote:
On Tuesday, 22 October 2013 at 10:45:29 UTC, qznc wrote:
On Tuesday, 22 October 2013 at 10:44:41 UTC, qznc wrote:
You are a little bit off. Sequential consistency (SC) does
require the compiler to insert locking.
Arg. Does NOT require the compiler to insert locking. ;)
Alright, I more or less paraphrased the definition of SC
(allegedly) made by Lamport, which is given on the Wikipedia
page (http://en.wikipedia.org/wiki/Sequential_consistency), but
in this case you answered your own question, I think. If SC
does only guarantee this, then you'd need the atomic operations
for correct synchronisation, as SC doesn't guarantee it?
The usual phrase is "sequential consistency for data-race-free
programs". The advice for unaware programmers is to avoid race
conditions and properly synchronize everything with locks. Only
then you get SC.
My example contains a data-race, hence no SC (according to Java
and C++ memory models).
If you really want to write such code, then you must understand
the whole complexity of the memory model. Then you can figure
out, if the behavior of your program is defined by language
semantics. In my example, the memory models says "undefined
behavior". If you make the necessary changes (atomic operations,
memory fences, etc), then the memory model says "defined
behavior: you might get pass the assert successfully or not at
all".