Description
Rationale: Writing an optimal CAS loop is time-consuming and risks errors (e.g.
mis-setting Orderings). On the other hand, there are two general operations:
fetch_and_update
which is useful for things like random number generators,
and update_and_fetch
which is the usual operation when you want to e.g. keep
a maximum stat over a series of concurrently produced values.
Adding those two operations as methods has little cost. Conversely, requiring
an extra crate means that the methods would have to either be implemented as
functions instead of methods, or that an extension trait would have to be
imported to use them. Both of those options are suboptimal.
Prior art: Java has an getAndUpdate
/ updateAndGet
method on its
AtomicInteger
class since version 1.8. Due to the fact that Java lambdas are
more restricted w.r.t. mutability of their arguments than Rust's, there's also
accumulateAndGet
/ getAndAccumulate
.