Concurrency for C++s
C++ concurrency is the set of standard tools for running code on multiple threads safely. It includes std::thread and jthread, mutexes and lock guards to protect shared data, condition variables to coordinate threads, and atomics with memory ordering for lock-free operations. The main hazards are data races, deadlocks and subtle ordering bugs.
Stage: Generic Programming · Part of the C++ roadmap · Last reviewed 19 November 2026
What to learn in Concurrency
- Start threads and join them safely using std::jthread
- Protect shared state with std::mutex and std::lock_guard
- Coordinate producer and consumer threads with a condition variable
- Use std::atomic for counters and flags
- Detect data races with ThreadSanitizer
Concurrency interview questions
What is a data race in C++?
A data race occurs when two threads access the same memory location concurrently, at least one is a write, and there is no synchronisation. It is undefined behaviour, so the program may misbehave unpredictably. Use mutexes or atomics to prevent it.
How do you avoid deadlock when locking two mutexes?
Always acquire locks in the same global order, or lock both at once with std::scoped_lock, which uses a deadlock-avoidance algorithm. Keep critical sections short and avoid calling unknown code while holding a lock.
Where Concurrency fits in the C++ roadmap
Concurrency belongs to the Generic Programming stage (advanced level), which takes about 28 hours. Templates.