1. Problem It Solves
An atomic object performs indivisible operations, and compare-and-swap changes a value only when it still equals an expected snapshot. It makes an important constraint visible instead of leaving readers to guess. This lesson keeps only the C++11 core that fits one focused day.
2. Prerequisites
The ideas from Day 47, plus basic variables, functions, and output already introduced.
3. Core Idea
Mental model: An atomic object performs indivisible operations, and compare-and-swap changes a value only when it still equals an expected snapshot. Identify the relevant value or state, who owns it, and whether the rule acts during compilation or execution.
4. Minimal Syntax
value.compare_exchange_strong(expected, desired);5. How It Works
The example creates a tiny fixed state with no keyboard input.
C++11 or the standard-library contract applies today's rule.
The program prints the important result so it can be checked against the source.
6. Common Mistakes
On CAS failure,
expectedis overwritten with the current value; retry loops that ignore this rule compare against stale assumptions.
7. When to Use It
Use it when simple shared state needs lock-free indivisible updates.
Avoid it when it hides ownership, lifetime, type, ordering, or cost.
8. Simple Example
A strong CAS changes five to nine, then fetch_add returns the old value while atomically increasing the stored value. The .cpp keeps the data fixed and avoids unrelated abstraction.
Complete sample code
Source file
cpp11/48_atomic_compare_and_swap/main.cpp
#include <atomic>
#include <iostream>
int main() {
std::atomic<int> value(5);
int expected = 5;
const bool changed = value.compare_exchange_strong(expected, 9);
const int before_add = value.fetch_add(3);
std::cout << std::boolalpha;
std::cout << "changed=" << changed << '\n';
std::cout << "before_add=" << before_add << '\n';
std::cout << "after_add=" << value.load() << '\n';
}
9. Key Takeaways
The feature is part of the C++11 scope used in this course.
Understand its lifetime, ownership, type, and ordering consequences.
Compile with warnings and prefer the smallest form that makes the rule obvious.
10. Self-Check Questions
Easy — When does
value.compare_exchange_strong(expected, desired)onstd::atomic<int>updatevalue? What happens toexpectedwhen the comparison fails?Medium — Read the small example described above. What value or state should it print, and which rule produces that result?
Hard — Find and explain the subtle bug in this situation: On CAS failure,
expectedis overwritten with the current value; retry loops that ignore this rule compare against stale assumptions. What is the smallest C++11-safe correction?