IT lexicon › Programming › CAS loop

CAS loop

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

A programming pattern that repeatedly attempts to atomically update a shared value using Compare-and-Swap until the operation succeeds.

A CAS loop is the foundation of almost all lock-free data structures. A thread reads a value, computes a new one based on the old, and atomically tries to swap it via compare-and-swap. If another thread changed the value concurrently, CAS fails and the loop retries. The pattern is simple but critical for avoiding the ABA problem.

CAS loops underpin lock-free stacks, queues, and counters. They outperform locks under low contention but can suffer from cache-line contention under heavy concurrent access when many threads repeatedly fail their attempts.

← Back to the lexicon