Compare-And-Swap
Compare-and-swap (CAS) is an atomic instruction used in multithreading to implement synchronization. It compares the contents of a memory location with a given value and, only if they match, modifies the contents of that memory location to a new given value. CAS is the foundational primitive of lock-free and wait-free algorithms — without it, modern non-blocking concurrent data structures would be impossible.
The instruction is supported natively by virtually all modern CPU architectures, including x86 (CMPXCHG), ARM (LDREX/STREX), and RISC-V. Operating systems and language runtimes expose CAS through intrinsics or standard library functions, enabling programmers to build lock-free stacks, queues, and hash tables. Yet CAS is not a panacea: it suffers from the ABA problem, requires careful attention to memory ordering, and can degrade into livelock under high contention.
See also: Lock-Free Programming, Atomic Operation, Mutex, Memory Ordering, ABA Problem