Data race
A data race is two threads accessing one memory location at the same time, at least one of them writing, without a happens-before edge between the accesses. In C++ a data race is undefined behaviour: the program may do anything, including appear to work.
Detailed Explanation
The standard does not say "you might read a stale value". It says the behaviour of the entire program is undefined. Compilers assume data races do not exist and will delete checks, hoist loads, and invent writes. A torn 64-bit store on a 32-bit machine is only one of the symptoms. The serious ones are "the compiler proved this branch is unreachable because a race would have been required otherwise".
Two threads incrementing int n without a mutex or an atomic is a data race. Two threads writing different elements of a vector is not a race on those elements, but a push_back that reallocates races with any access to any element. Two threads reading const data is fine. A writer and a reader of a string without a lock is a race.
You prevent races with:
- A mutex that both threads lock around the access (lock is acquire, unlock is release).
std::atomicoperations on that location.- Message passing: only one thread ever touches the object after a hand-off that synchronises (a
future, a queue with a mutex, joining the writer before the reader starts). - Not sharing. Thread-local or unique ownership.
A race detector (ThreadSanitizer, -fsanitize=thread) is the practical test. It is not complete, but it finds the bugs humans miss. Run your tests under TSan on every commit if you ship threads.
"Benign races" (writing a bool flag as a bool, not an atomic<bool>) are not benign in C++. They are UB. Use atomic<bool>.
A race is not the same as a race condition in the product sense. A race condition is "the outcome depends on scheduling" and can exist in a well-defined program (lost updates on a locked-but-wrong protocol). A data race is the UB kind. You can have a logic bug without a data race. You cannot have a data race without a language-level bug.
const methods on a shared object are a frequent source: const means "I will not write through this", not "no other thread writes". If another thread mutates, readers still need a lock or a snapshot.
Code Examples
#include <atomic>
#include <iostream>
#include <thread>
std::atomic<int> n{0};
void bump() { n.fetch_add(1); }
int main() {
std::thread a(bump);
std::thread b(bump);
a.join();
b.join();
std::cout << n.load() << '\n'; // prints: 2
}#include <mutex>
#include <string>
#include <thread>
std::mutex mu;
std::string name = "ada";
void rename() {
std::lock_guard<std::mutex> lock(mu);
name = "al";
}
void print() {
std::lock_guard<std::mutex> lock(mu);
(void)name.size();
}
int main() {
std::thread a(rename);
std::thread b(print);
a.join();
b.join();
}Common Gotchas
`bool flag` as a stop bit
not atomic, not ordered. Use `atomic<bool>` or a mutex.
`vector` reallocation versus readers
even reading `v[0]` while another thread `push_back`s is a race if the push reallocates. Lock the whole container, or do not share it.
"It passed on x86"
UB is not a hardware guarantee. TSan and ARM will disagree with your laptop.
Two readers, no writer
not a data race. A writer plus a reader without sync is.