-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
core::atomic
docs: what guarantees hold when compare_and_swap
returns other values?
#21668
Comments
Some code in libstd seems to assume that Examples:
My understanding is that the same semantics holds for |
@pnkfelix who do you think i should ask about clarifying this behavior? |
The semantics (just forgotten to be documented) are what @ranma42 pointed out. The previous value is always returned. |
The documentation for the various
compare_and_swap
methods (there's one each forAtomicBool
,AtomicIsize
, andAtomicUsize
) says:But it says nothing about other return values.
In particular, it does not say if the return value actually corresponds to a value that had been stored at some point in the memory. For all that this says, maybe its going to return
old + 1
or!old
, rather than the contents of memory at the time that the failed swap was attempted.So right now, if I want to be portable and follow these docs, I need to issue a separate call to the
load
method to load a current value, e.g. if my plan is to loop around and recompute the next value I want to store from the concurrently updated current value.Now, maybe this is not an oversight in the documentation, but is in fact a deliberate underspecification of the return value for
compare_and_swap
-- i.e. maybe we do not want to guarantee too much about the underling behavior of the target system.But in my opinion, a return value that is so woefully underspecified is no better than returning a boolean flag indicating whether the swap was successful or not.
And in any case, if this is a deliberate underspecification on the part of the documentation, then it should explicitly say something like "when the value is not updated, then the return value will be unequal to
old
, but is otherwise unspecified." (Because right now it is very easy for a reader to infer that the "currently stored" value that is unequal toold
is what is returned.)The text was updated successfully, but these errors were encountered: