From the Java AtomicReferenceFieldUpdater
docs:
Note that the guarantees of the
compareAndSet
method in this class are weaker than in other atomic classes. Because this class cannot ensure that all uses of the field are appropriate for purposes of atomic access, it can guarantee atomicity and volatile semantics only with respect to other invocations ofcompareAndSet
andset
.
This means I can't do normal volatile writes along with compareAndSet
, but have to use set
instead. It doesn't mention anything about get
.
Does that mean that I can still read volatile fields with the same atomicity guarantees - all writes before the set
or compareAndSet
are visible to everybody who has read the volatile field being?
Or do I have to use get
on the AtomicReferenceFieldUpdater
instead of volatile reads on the field?
Please post references if you have them.
Thank you.
EDIT:
From Java Concurrency in Practice, the only thing they say:
The atomicity guarantees for the updater classes are weaker than for the regular atomic classes because you cannot guarantee that the underlying fields will not be modified directly — the compareAndSet and arithmetic methods guarantee atomicity only with respect to other threads using the atomic field updater methods.
Again, no mention of how the other threads are supposed to read these volatile fields.
Also, am I right to assume that "modified directly" is a regular volatile write?
set
method instead. The question is - do you also have to use aget
method instead of volatile reads to ensure atomicity? – Euxenite