Deadlock
A deadlock is a set of threads that will never proceed because each is waiting for a resource another holds. The usual C++ form is two mutexes locked in opposite orders. The process does not crash; it hangs.
Detailed Explanation
Thread A locks account1 then waits for account2. Thread B locks account2 then waits for account1. Both critical sections are correct in isolation. Together they form a cycle in the wait-for graph. No timeout, no exception, no progress.
Coffman's four conditions (all required): mutual exclusion, hold-and-wait, no preemption of locks, circular wait. You break the cycle. In C++ that means:
- Lock ordering. A global total order on mutexes (by address, or by account id). Every thread locks in that order.
scoped_lock/std::lockimplement an internal order so you do not write it. - Lock less. Take a snapshot under one lock, release, then work. Do not call unknown code while holding a mutex; that code may lock.
- Try-lock and back off.
try_lockplus release-and-retry avoids a cycle at the cost of complexity and livelock. Prefer ordering. - One mutex. If two structures are always updated together, one lock is simpler than two.
Self-deadlock is locking a non-recursive mutex you already own on the same thread. foo locks mu and calls bar which locks mu. That is a cycle of length one. Do not use recursive_mutex as the first fix; split the locked and unlocked parts of the function.
Condition-variable deadlocks: waiting without notifying, or waiting while holding a second mutex the notifier needs. Join deadlocks: thread A joins B, B joins A. jthread destructor joins; do not join a thread that is waiting to join you.
Detection: hang a debugger and look at pthread_mutex_lock / WaitForSingleObject in the stacks. Helgrind and TSan can flag lock-order inversions. A watchdog that logs "no progress for 30s" is an operational backstop, not a fix.
Livelock is the cousin: threads keep releasing and retrying and never finish. Starvation is a thread that never wins the lock. Different bugs, same family of "not the answer I wanted from concurrency".
Code Examples
#include <mutex>
#include <thread>
std::mutex a;
std::mutex b;
void safe() {
std::scoped_lock lock(a, b); // one handshake, no cycle
}
int main() {
std::thread t1(safe);
std::thread t2(safe);
t1.join();
t2.join();
}#include <iostream>
#include <mutex>
std::mutex mu;
void inner() {
std::lock_guard<std::mutex> lock(mu); // would deadlock if called from outer
std::cout << "inner\n";
}
void outer() {
std::lock_guard<std::mutex> lock(mu);
// inner(); // do not: self-deadlock
std::cout << "outer\n";
}
int main() {
outer(); // prints: outer
}Common Gotchas
Opposite lock order in two functions
`transfer(a, b)` and `transfer(b, a)` must not lock `first` then `second` by parameter order. Lock by a stable key (`if (a.id < b.id)`).
Joining a thread that needs you to unlock
if the worker needs `mu` and you `join` while holding `mu`, you deadlock. Unlock first.
`scoped_lock` is not a hierarchy
nested `scoped_lock`s that overlap on one mutex still self-deadlock.
Recursive mutex hides design bugs
it lets you re-enter and then you forget which invariant holds. Prefer splitting functions.