Skip to content
Free public beta until December 4, 2026: every course is open, no card needed. After that, the first chapter of each course stays free and full access is a one-time $39 ($19 for up to 100 founding members chosen for helpful feedback). Your progress is kept. How the beta works

compare-and-swap

An atomic step that writes a new value only if memory still holds the value you expected, and tells you whether it did.

Compare-and-swap atomically compares *p with an expected value and, if equal, stores a desired value; it reports success and the value observed (x86-64 lock cmpxchg with the expected value in rax; ARM64 LSE cas; C++ compare_exchange_strong/weak).

Examples

  • CAS(&x, 5, 6) sets x to 6 only if it still holds 5, and reports whether it did.

Read the full section →