skip to content

In C++, how do you lock the std::mutex of two inventory records for a stock transfer without risking deadlock?

level: middleimportance: must knowfreq 66%

answer

  1. two threads, opposite directions
  2. one call that locks both
  3. C++17 variadic lock type
  4. std::lock plus adopt_lock
  5. same record twice

basics

~10 s

Lock both with one call that avoids deadlock: std::scoped_lock lock(from.mu, to.mu) in C++17, or std::lock(from.mu, to.mu) followed by two lock_guards with std::adopt_lock in C++11. Guard against transferring a record to itself first.

solid answer

~40 s

Locking `from.mu` and then `to.mu` with two `lock_guard`s deadlocks when one thread moves stock from A to B while another moves it from B to A: each holds one mutex and waits for the other. `std::lock(a, b, ...)` locks all its arguments through a sequence of `lock()`, `try_lock()` and `unlock()` calls that the standard says does not result in deadlock, typically try-and-back-off, and unlocks what it locked if a call throws. C++17's `std::scoped_lock lock(from.mu, to.mu);` wraps that in RAII. In C++11 write `std::lock(from.mu, to.mu);` then `std::lock_guard<std::mutex> a(from.mu, std::adopt_lock);` and the same for `to.mu`, or pass two `std::unique_lock`s built with `std::defer_lock` to `std::lock`. Check `&from == &to` first: locking the same `std::mutex` twice is undefined behaviour.

code

cpp · 29 lines
cpp
#include <iostream>
#include <mutex>
#include <thread>

struct InventoryRecord {
    std::mutex mu;
    int units = 100000;
};
void move_stock(InventoryRecord& from, InventoryRecord& to, int qty) {
    if (&from == &to) return;             // locking one mutex twice would be UB
    std::scoped_lock lock(from.mu, to.mu);  // C++17: deadlock-free, like std::lock
    from.units -= qty;
    to.units += qty;
}
void move_stock_cxx11(InventoryRecord& from, InventoryRecord& to, int qty) {
    if (&from == &to) return;
    std::lock(from.mu, to.mu);
    std::lock_guard<std::mutex> a(from.mu, std::adopt_lock);
    std::lock_guard<std::mutex> b(to.mu, std::adopt_lock);
    from.units -= qty;
    to.units += qty;
}
int main() {
    InventoryRecord dock, shelf;     // opposite directions, opposite lock order
    std::thread t1([&] { for (int i = 0; i < 100000; ++i) move_stock(dock, shelf, 1); });
    std::thread t2([&] { for (int i = 0; i < 100000; ++i) move_stock_cxx11(shelf, dock, 1); });
    t1.join(); t2.join();
    std::cout << "dock " << dock.units << ", shelf " << shelf.units << '\n';
}

go deeper

for a junior

Know that taking two locks one after another can deadlock and that std::scoped_lock takes both at once.

for a middle

Explain what std::lock guarantees, the C++11 adopt_lock and defer_lock spellings, and the adopt tag's position in each type.

for a senior

Catch the self-transfer UB, the zero-argument scoped_lock and run-time sets of mutexes that need a global order instead.

for a principal

Weigh per-record mutexes against coarser locking or a single-writer design when transfers dominate the workload.

## Why two lock_guards deadlock A warehouse system moves stock between two `InventoryRecord`s, each with its own `std::mutex`. The obvious code is ```cpp std::lock_guard<std::mutex> a(from.mu); std::lock_guard<std::mutex> b(to.mu); ``` Thread 1 runs `move_stock(dock, shelf)` and locks `dock.mu`. Thread 2 runs `move_stock(shelf, dock)` and locks `shelf.mu`. Each now waits forever for the mutex the other holds. The bug needs only two threads moving stock in opposite directions at the same moment, so it passes tests and appears under load. ## std::lock: one call for all the mutexes `std::lock(l1, l2, ...)` takes two or more lockable objects and locks all of them. The standard specifies only the outcome: the sequence of `lock()`, `try_lock()` and `unlock()` calls **does not result in deadlock**, and if any call throws, every argument it had locked is unlocked again. A note suggests try-and-back-off; implementations choose their own algorithm. The order you pass the arguments in does not matter, which is exactly what the opposite-direction transfer needs. `std::lock` leaves the mutexes locked and returns, so ownership must be handed to RAII objects immediately. ## How a deadlock-avoiding lock can work The standard does not fix the algorithm, but a try-and-back-off implementation for two mutexes behaves like this: 1. Block in `lock()` on the first mutex. 2. Call `try_lock()` on the second. If it succeeds, both are held: done. 3. If it fails, `unlock()` the first, so the thread holding the second can finish. 4. Retry, this time blocking on the mutex that was busy and trying the other. No thread ever **waits** while holding a mutex someone else needs, which removes the circular wait. The cost is possible retries under heavy contention. The related `std::try_lock(a, b, ...)` never blocks: it calls `try_lock()` on each argument in order, unlocks the ones it took if any fails, and returns `-1` on success or the zero-based index of the argument that failed. ## The C++17 way and the C++11 ways | Spelling | Standard | Notes | |---|---|---| | `std::scoped_lock lock(from.mu, to.mu);` | C++17 | calls `std::lock` for two or more mutexes, unlocks all in the destructor | | `std::lock(from.mu, to.mu);` then `lock_guard(m, std::adopt_lock)` twice | C++11 | the guards take over locks already held | | `unique_lock(m, std::defer_lock)` twice, then `std::lock(la, lb);` | C++11 | movable, can unlock early | Two details trip people up: - `std::scoped_lock`'s adopting constructor takes the tag **first**: `std::scoped_lock lock(std::adopt_lock, a, b);`. `lock_guard` takes it last: `lock_guard(m, std::adopt_lock)`. Putting the tag last in a `scoped_lock` makes the compiler deduce `adopt_lock_t` as a mutex type and fail. - `std::scoped_lock` with one mutex behaves like `lock_guard`; with **zero** it does nothing, so `std::scoped_lock lock;` written by mistake compiles and locks nothing. ## Transferring a record to itself If a request moves stock from a record to the same record, `std::scoped_lock lock(r.mu, r.mu)` hands one mutex to `std::lock` twice. For `std::mutex` that means locking or try-locking a mutex the thread already owns, which breaks the precondition and is undefined behaviour. Check `if (&from == &to) return;` before locking, or treat it as a no-op at the API boundary. ## Beyond two mutexes `std::lock` and `std::scoped_lock` accept any number of mutexes, so a transfer that also updates a ledger can lock three at once. When the set of mutexes is only known at run time, for example a batch of SKUs, these variadic tools do not apply directly. Then the usual answer is a consistent global order, such as always locking records by ascending SKU or address, which is the general deadlock-prevention technique and works with plain guards. ## Checklist for review 1. Any function that holds two mutexes at once uses `std::scoped_lock` or `std::lock`, never two sequential guards. 2. The adopt tag follows the type's own parameter order. 3. Self-transfer is rejected before locking. 4. Nothing slow or external runs while both locks are held. 5. A stress test moves stock in both directions at once; under the two-guard bug it eventually hangs, which a passing single-direction test never shows.

  • Does the order of arguments to std::scoped_lock or std::lock matter?
    Not for deadlock. The standard requires the sequence of calls `std::lock` makes not to result in deadlock whatever order the arguments come in, which is why the opposite-direction transfer is safe. The order can still affect performance under contention, because the algorithm may lock, fail a `try_lock`, back off and retry.
  • When would you use std::unique_lock with std::defer_lock instead of std::scoped_lock?
    When you need what only `std::unique_lock` offers: releasing one mutex before the end of the scope, moving the lock out of a function, or a C++11 or C++14 build without `std::scoped_lock`. Construct both with `std::defer_lock`, call `std::lock(la, lb);`, and each lock then owns its mutex.
  • How do you lock a set of records chosen at run time?
    The variadic tools need the mutexes as separate arguments, so for a run-time set use a consistent global order: sort the records by a stable key such as SKU, then lock them in that order and unlock in reverse. Every thread that follows the order is free of lock-order deadlock.

  • Two lock_guards are fine as long as every function locks from before to.
  • std::lock locks its arguments in the order they are passed.
  • std::scoped_lock lock(r.mu, r.mu) is safe because scoped_lock detects duplicates.
  • std::scoped_lock takes std::adopt_lock as its last argument, like lock_guard.
  • std::lock needs a try_lock loop written around it to avoid deadlock.