Semantics of VarHandle CAS methods

Martin Buchholz martinrb at google.com
Thu Jun 30 01:20:21 UTC 2016


VarHandle.compareAndSet is strong in two ways - non-spurious and
sequentially consistent.

VarHandle.weakCompareAndSet is weak in both these ways.
(That seems like a mistake to me.
The fact that j.u.c. Atomic classes are a precedent for this seems
unfortunate.)

http://download.java.net/java/jdk9/docs/api/java/lang/invoke/VarHandle.html#weakCompareAndSet-java.lang.Object...-

Sequential consistency should always be the default behavior of an
unqualified cas operation - users should have to go out of their way to
request relaxed reads or writes.  C++ reasonably makes you use
e.g. std::atomic_compare_exchange_weak_explicit.

I was surprised that weakCompareAndSetAcquire used relaxed writes!

I would expect that almost everyone would be happy with sequentially
consistent, and almost everyone else would be happy with acquire-release, a
variant we don't offer!  And a few others would be happy with fully relaxed
but single-variable sequentially consistent (but Java, unlike C++, doesn't
promise that!).


More information about the core-libs-dev mailing list