C++ concurrency exercises
Writing multithreaded C++ requires strict synchronization. A data race on a non-atomic variable is undefined behavior in C++, while acquiring multiple mutexes in differing order produces intermittent deadlocks under production load.
Debug multithreaded C++ algorithms and ensure thread safety.
The concurrency bugs you will meet
- Accessing a shared variable across threads without a mutex or std::atomic is undefined behavior.
- Acquiring mutexes in different orders across threads leads to deadlocks; use std::lock or std::scoped_lock.
- Spawning std::thread without calling .join() or .detach() invokes std::terminate in its destructor.
- Condition variable wait() without a predicate lambda suffers from spurious wakeups.
The exercises
Medium
Errors you might be searching for
More JavaScript practice by topic
Questions
What is std::scoped_lock?
Introduced in C++17, std::scoped_lock acquires multiple mutexes atomically using a deadlock-avoidance algorithm.