[concurrency-interest] AtomicReference.updateAndGet() mandatory updating

Justin Sampson jsampson at guidewire.com
Tue May 30 16:28:53 EDT 2017


Alex Otenko wrote:

> The write of the default value (zero, false, or null) to each
> variable synchronizes-with the first action in every thread.

That's not the write I'm talking about: There's no happens-before
relationship between the x=1 and either of the reads, so both
reads are independently permitted to see either 0 or 1 without
violating happens-before consistency.

But now I see that my concern would be addressed by simply
changing the code to eliminate the second read in Thread1:

if (CAS(z, 0, 1)) {
  if (x == 0) {
    b=true;
    CAS(z, 1, 2);
  } else {
    return 1; // instead of falling through to return x
  }
}
return x;

Cheers,
Justin




More information about the Concurrency-interest mailing list