Re:The best engineering interview question I've ever gotten
by jhgb on Monday April 18, 2022 @12:32AM
(#31067457)
Is ABA an issue here? "Replace A by X*A" should be perfectly fine if an atomic swap gives you A back, since it means it was actually A at the time when you perform the swap. That it was B a moment go doesn't seem to matter.
Re:The best engineering interview question I've ever gotten
by stormbrew on Sunday April 17, 2022 @10:18PM
(#31066599)
Updating an entry in a database table (which is what a memcache entry is, in practice) is not usually as simple as just "change the value from X to Y". Locking is likely necessary no matter what.
For example, for adding it should be enough to use an atomic Fetch-And-Add instruction:
https://doc.rust-lang.org/std/sync/atomic/struct.AtomicU32.h...
Multiply doesn't have an equivalent. You can work around that by reading, multiplying and then using CMPXCHG to atomically update the variable if nothing has changed while you were processing. But you need to think about the ABA problem[1]. (I think in this case its safe, but I haven't thought about it enough. Lock free algorithms have subtle bugs.)
[1] https://en.wikipedia.org/wiki/ABA_problem