A compare-and-exchange operation atomically compares a memory value with an expected value and replaces it with a new value only if they match. This conditional update is a building block for synchronization algorithms. section 8.3.1
In a concurrent linked list, an insertion may use compare-and-swap to update a pointer. If the operation fails because the pointer changed, the insertion must restart or return an error, limiting the wait-free guarantee. section 8.5.5
Table 8.1 Consensus numbers of wait-free primitives that you’ll typically use. The consensus number represents the maximum number of threads or processes a primitive can guarantee consensus for.
| Wait-free primitive |
Consensus number |
|---|---|
| Atomic registers |
1 |
| Test-and-set |
2 |
| Fetch-and-add |
2 |
| Queues |
2 |
| Stacks |
2 |
| Queues with peek |
∞ |
| Compare-and-swap (CAS) |
∞ |
Compare-and-swap has an infinite consensus number in this comparison of wait-free primitives. section 8.5.2


