Compare-and-Swap
An atomic read-modify-write primitive that updates a memory location only when its current value matches an expected value.
Concurrency Context
Compare-and-swap (CAS) is a core read-modify-write primitive in lock-free stacks, queues, counters, and state machines. A failed CAS normally means the caller must observe the new value and retry using an updated assumption.
Correctness and Progress Boundary
A retry loop around CAS can become expensive under contention, and CAS-based designs can be exposed to the ABA Problem. Lock-free progress also does not imply that every individual thread is protected from starvation.
Related Concurrency Concepts
- Lock-Free
- ABA Problem
- Memory Barrier
- Atomicity