In computer science, compare-and-swap (CAS) is an atomic instruction used in multithreading to achieve synchronization. It compares the contents of a memory location to a given value and, only if they are the same, modifies the contents of that memory location to a given new value. The atomicity guarantees that the new value is calculated based on up-to-date information; if the value had been updated by another thread in the meantime, the write would fail. The result of the operation must indicate whether it performed the substitution; this can be done either with a simple Boolean response (this variant is often called compare-and-set), or by returning the value read from the memory location (not the value written to it).

Read more about Compare-and-swap:  History of Use, Implementation in C, Usage, Extensions

Other related articles:

Compare-and-swap - Extensions
... Double compare-and-swap compares two unrelated memory locations with two expected values, and if they're equal, sets both locations to new values ... Double-wide compare-and-swap operates on two adjacent pointer-sized locations (or, equivalently, one location twice as big as a pointer) ... Multi-word compare-and-swap is a generalisation of normal compare-and-swap ...
Linearizability - Example Atomic Operation - Compare-and-swap
... Most systems provide an atomic compare-and-swap instruction that reads from a memory location, compares the value with an "expected" one provided by the user, and writes out a "new" value if the two match, returning ... as follows read the value in the memory location add one to the value use compare-and-swap to write the incremented value back retry if the value read in by the ...