-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
compare_and_swap "migration" documentation should explain how to get return value #80486
Comments
FWIW, the underlying primitive in LLVM that backs |
Looks like |
related: #79315 |
I'm probably dense, but I don't understand how I am supposed to change my code. This is what I had, but reading the if !DRAIN_MODE.compare_and_swap(false, true, Ordering::Relaxed) { I understand the motivation for the deprecation, but I feel sad it's done at the cost of a more complex API. For my use case, I simply want atomic access to a bool, and now I'm forced to get my head around the double Wouldn't it be possible to not deprecate |
@lolgesten without commenting on the rest of what you said, this is how you could change your code: if !DRAIN_MODE.compare_exchange(false, true, Ordering::Relaxed, Ordering::Relaxed).unwrap_or_else(|x| x) { This translation can be applied systematically. However, I assume what you really want to express here is "if the CAS happened successfully", which would be more clearly expressed as if let Ok(_) = DRAIN_MODE.compare_exchange(false, true, Ordering::Relaxed, Ordering::Relaxed) { or if DRAIN_MODE.compare_exchange(false, true, Ordering::Relaxed, Ordering::Relaxed).is_ok() { Arguably, this code is more clear than your original code, since it is easier to see that it checks for "successful CAS". |
@RalfJung thanks! This is what I was suspecting I should do. In terms of improving the documentation. The table added in the doc for And also
It's not clear to me whether the doc about |
I agree that the "Original" column could be better named. But the column names "Success" and "Failure" are named after the arguments to |
Yes. :-) |
The
compare_and_swap
docs explain how to switch theorder
tocompare_exchange
, but that is not the only difference: the return type also changed. The docs don't say anything about how to best get the old behavior back with the new method. Many concurrent algorithms are written based on getting the old value back, so there should be a convenient way to get that behavior.Cc @faern
The text was updated successfully, but these errors were encountered: