Compare-and-Swap
Pronunciationkum-PAIR and SWOP
Definition
Compare-and-swap (CAS) is an atomic operation that updates a value only if it still holds the value you expected, and reports whether it worked. It's the building block of lock-free code.
Where you hear it
In atomic counters, lock-free data structures, optimistic locking in databases and interview questions.
Examples
Retry the CAS in a loop until it succeeds.
A SQL
UPDATE ... WHERE version = 3is compare-and-swap at database level.The counter uses compare-and-swap so two threads never overwrite each other's update.
Common mistake
Ignoring the ABA problem. The value can change from A to B and back to A, and the CAS can't tell.
Don't confuse with
A mutex, which blocks other threads. CAS never blocks; it just fails and you retry.
Say it at work
- In a meeting or chat
Use an atomic counter instead of a lock.
- In an email or ticket
This is a CAS loop.
Keep exploring
More in Programming Fundamentals