[concurrency-interest] Volatile array and Wait

Dawid Kurzyniec dawidk at mathcs.emory.edu
Fri Oct 21 14:51:04 EDT 2005

Pete Soper wrote:

>See sections and 17.4.5, especially the discussion item in the
>latter section:
>"A write to a volatile field happens happens before every subsequent
>read of that field."
>This doesn't say "if the read or write doesn't get optimized away." This
>text isn't making a suggestion. 
Optimizations, by definition, are never mentioned in the spec (unless as 
hints). An optimization can do whatever it wants as long as it does not 
violate the spec. So, if the spec says something precise about side 
effects of reads and writes, that side effects must be preserved even in 
the face of optimizations.

>In general compilers can't eliminate
>volatile accesses. Cases like reflective inspection have to be covered,
>as David Holmes mentioned, and this simply can't be anticipated (in the
>sense of being ruled out) except in very special cases. 
Luckily, the situation does not look that terribly dire, since much more 
can be asserted about local variables, especially of primitive types 
(e.g. they cannot be seen by other threads, and they cannot be inspected 
reflectively; they cannot even be declared volatile); so aggressive 
optimizations apply to them quite well.


More information about the Concurrency-interest mailing list