Compare-and-swap
An atomic machine instruction that updates a memory location only if it holds an expected value, with no opportunity for interference between the check and the update.
CAS (compare-and-swap) is the foundation of lock-free data structures. It performs two operations indivisibly: read the value at a memory address, compare it to an expected value, and if they match, write a new value. If they don't match, nothing is written and the operation signals failure — the caller typically retries. Because the three steps are atomic (uninterruptible by other threads), it's safe to use without explicit locks.
CAS first appeared on IBM System/360 in the 1960s and is now a standard instruction on every modern processor (x86 CMPXCHG, ARM cas, PowerPC lwarx/stwcx). It enables lock-free stacks, queues, and other structures that would otherwise require a central lock. The downside: retry loops can spin indefinitely under high contention.