IT lexicon › Programming › Compare-exchange

Compare-exchange

Programming På svenska → Updated: 2026-11-15

An atomic operation that reads a value, compares it to an expected value, and writes a new value only if they match.

Compare-exchange (CAS) is the building block for lock-free programming. It's optimistic locking: you read the value, do calculations, then try to write back — but only if no other thread changed it between read and write. If it fails, you loop and retry. It's an atomic instruction on modern processors and enables algorithms that avoid mutexes entirely. Performance depends on contention — high contention can make it slower than a mutex.

← Back to the lexicon