[concurrency-interest] Unsafe.getAndAddLong

Andrew Haley aph at redhat.com
Thu May 22 13:40:06 EDT 2014

On 05/22/2014 05:44 PM, Nathan Reynolds wrote:
> A CAS retry can be very costly.  See this blog entry for the details. 
> https://blogs.oracle.com/dave/entry/atomic_fetch_and_add_vs
> Basically, as CAS retries happen more often the branch predictor will 
> predict that the loop will be re-executed.  So, when the CAS finally 
> succeeds, the processor will stall because the branch was predicted the 
> wrong way.  If CAS was used to acquire a lock, then the critical region 
> of the lock is now longer and throughput will suffer.
> Word tearing is probably not very common.  But, an already hot CAS loop 
> wouldn't appreciate any more problems.

Sure, but the problem is that already hot CAS loop has an unnecessary
barrier, and that's not a rare case, that's every time.


More information about the Concurrency-interest mailing list