Compare-and-swap (CAS) is an atomic operation that compares the current value at a memory location with an expected value and, only if they match, replaces it with a new value; otherwise it leaves the location unchanged. Because the comparison and the replacement happen as a single indivisible step, when several threads or processes try to update the same value at once, exactly one succeeds and the others learn that they failed. Most CPUs provide it as an instruction, and programming languages expose it through atomic operation libraries.
CAS allows shared data to be updated safely without acquiring a lock, so it underpins lock-free data structures and conditional writes in distributed storage systems. A failed attempt must re-read the latest value and retry, so throughput drops as more writers contend for the same value.