[concurrency-interest] Assignment of references atomic?

David Holmes dholmes at dltech.com.au
Wed Oct 12 19:09:16 EDT 2005

> Chris Purcell writes:
> Just to clarify here: you can synchronize by using the "volatile"
> keyword, right? You do not need to use the "synchronized" keyword?

You can use the volatile keyword to ensure that things are visible and that
the right ordering is maintained.

For example, this is perfectly safe:

  volatile boolean dataReady = false;
  Object theObject;

  Thread-1                             Thread-2..n
  theObject = makeObject();            if (dataReady)
  dataReady = true;                        use(theObject);

Here there is a single writer and many potential readers. As long as the
readers see dataReady is true then they are guaranteed to see everything
that happened to theObject during makeObject().

But volatile in itself is not sufficient *if* there is a need for atomicity
between deciding to update the object and performing the update.

David Holmes

More information about the Concurrency-interest mailing list