compareandswap
Compare-and-swap, often abbreviated CAS, is an atomic synchronization primitive used in concurrent programming. It allows a thread to update a shared memory location only if its current value matches an expected value, providing a simple way to implement lock-free data structures and coordination without heavy locking.
Operation: CAS(ptr, expected, new) checks the value at ptr. If it equals expected, CAS sets *ptr to
Use cases and patterns: CAS is widely used to implement lock-free stacks, queues, and reference counters, as
Hardware and design considerations: CAS is provided by many CPUs (for example, cmpxchg on x86 and similar