[concurrency-interest] Synchronization question

Ryan LeCompte Ryan.LeCompte at pangonetworks.com
Tue Jun 27 18:05:31 EDT 2006


Sounds great. I will make the field volatile. The fact that I ensure that the variable is initialized before other threads get a chance to read it satisfies the happens-before relationship, correct?

________________________________

From: Jeremy Manson [mailto:jmanson at cs.purdue.edu]
Sent: Tue 6/27/2006 5:59 PM
To: Ryan LeCompte
Cc: Tim Peierls; concurrency-interest at cs.oswego.edu
Subject: Re: [concurrency-interest] Synchronization question



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


-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/attachments/20060627/f4100b0a/attachment-0001.html 


More information about the Concurrency-interest mailing list