Re: Synchronization Question
Tom Anderson wrote:
See JLS 17.6, 'Word Tearing':
http://java.sun.com/docs/books/jls/third_edition/html/memory.html#57481
markspace wrote:
I didn't read this, and while it may say what you indicate, I'm still
dubious. It's possible for other system components outside the CPU to
write to memory as well. The cache and various DMA devices connected to
main memory are my biggest concern. If a cache miss occurs, a cache
line has to be written out to main memory (if dirty) before a new one
can be read in, and it is completely possible in my mind that a "word
tear" could occur at this point.
Read that whole chapter, not just 17.6.
I haven't read up on any CPU data sheets in many years, but I still
wouldn't trust any kind of word writes in the absence of
synchronization. There's too many weird things that are going on and
not under control of the CPU.
The Java semantics for synchronization are independent of CPU data sheets.
Aligning int on mod four word boundaries might help (cache lines are
typically aligned the same) but I'd still be dubious that there wasn't
something unexpected lurking for the unwary. Defensive programming and
all that.
Not a Java programmer's responsibility, and not within the Java programmer's
control.
This is probably FUD, but emotionally I can't bring myself to trust any
write without synchronization.
Writes without reads when you don't care who wins clearly don't need
synchronization. As you read the JLS about 'happens-before', you see that the
relevant issues have to do with ordering writes and reads with respect to each
other. It's a kind of Java Heisenberg Indeterminacy Principle - the state of
the shared data is only important once observed.
The Java language makes very specific promises about the visibility or lack
thereof of data transformations in the presence or absence of synchronization.
You are wise to have doubts (in the sense used by the majority of English
speakers), but those doubts can be resolved by understanding the rules.
The recommendations in this thread to determine 'happens-before', e.g., to use
'join()', will resolve the OP's question with certainty.
--
Lew