[concurrency-interest] AtomicReference weakCompareAndSet"Mayfailspuriously"?

Bill Pugh pugh at cs.umd.edu
Sun May 28 20:50:30 EDT 2006

> But isn't that all just a long-winded way of saything that it creates
> happens-before edges?


> Otherwise, what on earth does "creates happens-before edges" mean?

Say we have:

initially, x = 0 and v = AtomicInteger(0);

Thread 1:
x = 1
while (!v.weakCompareAndSwap(0,1));

Thread 2:
while (v.get() == 0);
r1 = x

If thread 1 terminates, then because the actions on the atomic  
integer are synchronization
actions,  thread 2 is guaranteed to see the update to v and  
terminate. However, no happens-before
order is established between the weakCompareAndSwap in thread 1 and  
the get in thread 2.
Thus, we don't have a happens-before order between the write to x and  
the read of x. Thus,
this program is incorrectly synchronized and the read of x can see  
either 0 or 1.


More information about the Concurrency-interest mailing list