0Pricing
C++ Academy · Lesson

Compare-and-Swap CAS Patterns

Implement lock-free updates with compare_exchange_weak and strong.

The CAS Primitive

Compare-and-swap atomically reads, compares, and conditionally writes. The foundation of most lock-free algorithms.

C++ Compare Exchange

Two variants in std::atomic:

  • compare_exchange_strong(expected, desired) — succeeds when current equals expected
  • compare_exchange_weak(expected, desired) — may fail spuriously

All lessons in this course

  1. std::atomic and Memory Orders
  2. Compare-and-Swap CAS Patterns
  3. Lock-Free Queue Implementation
  4. Hazard Pointers and the ABA Problem
← Back to C++ Academy