[concurrency-interest] Synchronization question

Jeremy Manson jmanson at cs.purdue.edu
Tue Jun 27 17:59:29 EDT 2006


Ryan LeCompte wrote:
> Thanks! I'll make the field volatile then. There is absolutely no
> harm in making a field volatile and using it inside of another
> synchronized method/block, correct? No potential deadlocks simply due
> to the variable being made volatile?
> 
> Thanks, Ryan


I was going to mention volatile, but I realized that annotating the
variable volatile doesn't actually guarantee that the initialization
will happens-before the reads.  It only guarantees that the
intialization happens-before the reads /if the reads actually see the
initialization/.

That's circular in this case, and seems to miss the point.

So you still need the happens-before relationship.

HOWEVER, as Tim pointed out, volatile is cheap, and is a good annotation
to have on shared variables that are not GuardedBy some lock.  So you
might as well still use it.

					Jeremy


More information about the Concurrency-interest mailing list