Skip to content
Tutorials Center
My learning

Topics

Lesson 133 of 171 ยท Concurrency

Mutexes and Locks

Learn to protect shared data in C++ - data races, std::mutex, lock_guard, unique_lock, scoped_lock for deadlock-free locking, shared_mutex for readers, and call_once.

In the Threads lesson every thread wrote its own data, so nothing needed protecting. Real programs aren't always that tidy: a web server's threads update one cache, a game's workers append to one event list, a bank's threads move money between the same accounts. When several threads access the same data and at least one of them writes, you need synchronization, or the program has a data race.

The most common tool is the mutex (short for "mutual exclusion"): a lock that only one thread can hold at a time. A thread locks the mutex, works on the shared data, and unlocks it; any other thread that tries to lock it in the meantime waits. This lesson shows a real data race, fixes it with std::mutex, and then covers the RAII lock types (lock_guard, unique_lock, scoped_lock), deadlocks and how to avoid them, reader/writer locks with std::shared_mutex, and one-time initialization with std::call_once.

A data race you can see

Here four threads each record 200,000 hits on one shared counter. The right answer is 800,000.

C++ Lost updates from a data race

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

You'll almost certainly see totals below 800,000, different on each attempt. ++stats.hits is really three steps: load the value, add one, store it. If thread A loads 500, then the scheduler switches to thread B, which loads 500, adds and stores 501 a thousand times... then A resumes and stores its 501, erasing B's work. Our server has a single CPU core, so the threads never run at the same instant, yet updates are still lost: the operating system can preempt a thread between any two instructions. On a multi-core machine it gets worse.

A data race is undefined behavior, not just a wrong number. The compiler optimizes as if no other thread touches your variables between synchronization points: it may keep a value in a register, merge writes, or move them. A loop like for (...) ++counter; on a global is often compiled into a single counter += 1000000, so racing threads lose whole blocks of updates. "It only loses a few increments" is not a guarantee you can rely on.

std::mutex and std::lock_guard

std::mutex from <mutex> has three operations: lock() (wait until you own it), unlock(), and try_lock() (take it if it's free, otherwise return false immediately). Code between locking and unlocking is called the critical section: only one thread at a time can be inside it.

You should almost never call lock() and unlock() yourself. If the code in between returns early or throws, unlock() never runs, and every other thread waits forever. Instead, use a lock guard: an RAII object that locks in its constructor and unlocks in its destructor. std::lock_guard is the simplest one.

C++ Fixing the race with a mutex

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Now every attempt prints exactly 800,000. Three design points from this class apply to all thread-safe code:

  • Keep the mutex next to the data it protects, private in the same class, so no one can touch the data without going through a locking member function.
  • Mark the mutex mutable so const member functions can lock it. Reading needs the lock too: an unsynchronized read racing with a write is still a data race.
  • Never return a pointer or reference to protected data. Once the function returns, the lock is gone, and the caller would be using the data unprotected. Return copies.

A mutex lock also creates a happens-before relationship: everything a thread wrote before unlocking is visible to the next thread that locks the same mutex. That's why the mutex fixes not only the "lost update" problem but also visibility.

Protecting invariants, not just variables

A mutex doesn't protect variables; it protects invariants, the rules that must hold whenever another thread can look. If an operation checks something and then acts on it, both steps must happen under the same lock. Otherwise the check is stale by the time you act, a bug known as a race condition (check-then-act).

C++ Check-then-act under one lock

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Eight customers try 400 withdrawals of 30 cents from $100.00, which can cover only 333 of them. Exactly 333 are approved and 67 refused every time, with 10 cents left. If withdraw instead had a bool has_funds() member that callers checked before calling an unchecked take(), two threads could both see "enough money" and both withdraw. Design thread-safe interfaces around complete operations ("withdraw if possible"), not around separate check and act steps.

Keep critical sections short: do expensive work (formatting, I/O, computation) outside the lock, and lock only to read or update shared state. And never call unknown code (callbacks, virtual functions supplied by users) while holding a lock: it might try to take the same lock or another one, which leads to deadlock.

Deadlock and std::scoped_lock

A deadlock happens when threads wait for each other forever. The classic case: thread 1 locks mutex A and then wants B, while thread 2 locks B and then wants A. Each holds what the other needs. A money transfer between two accounts, each with its own mutex, is the textbook example:

C++
void transfer(Account& from, Account& to, long long cents) {
    std::lock_guard lock_from{from.mutex};   // thread 1: locks alice, thread 2: locks bob
    std::lock_guard lock_to{to.mutex};       // thread 1 waits for bob, thread 2 for alice: stuck
    from.balance -= cents;
    to.balance += cents;
}
// thread 1: transfer(alice, bob, 10);   thread 2: transfer(bob, alice, 5);

There are two classic cures. One is to always lock mutexes in the same global order (for example, by account ID). The other, which C++ makes easy, is to lock them all at once with a deadlock-avoiding algorithm. std::scoped_lock (C++17) takes any number of mutexes, locks them all without risk of deadlock, and unlocks them all when it's destroyed:

C++ Deadlock-free transfers with scoped_lock

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

The program always finishes, and no money appears or disappears. Alice always ends at 30,000 and Bob at 70,000: she sends 20,000 ร— 2 cents and gets back 20,000 ร— 1, and neither balance can run short along the way, so every transfer succeeds whatever the interleaving. With two lock_guards in argument order instead, the same program can hang forever. A deadlock depends on timing, so a buggy version may pass a thousand test runs and then freeze in production.

Use std::scoped_lock as your default lock type in new code: with one mutex it behaves exactly like lock_guard, and with several it adds deadlock avoidance. (Older code does the same with std::lock(a, b) followed by two lock_guards with std::adopt_lock.)

std::unique_lock: a flexible lock

std::unique_lock is a lock guard with extra abilities, at a tiny cost (it stores an "owns the lock" flag):

  • Unlock early and relock: lock.unlock() and lock.lock() in the middle of a scope.
  • Deferred locking: std::unique_lock lock{m, std::defer_lock}; creates it unlocked.
  • Try locking: std::unique_lock lock{m, std::try_to_lock}; then check lock.owns_lock().
  • Timeouts with std::timed_mutex: try_lock_for(50ms) or try_lock_until(time_point).
  • Movable: you can return a unique_lock from a function to hand over a held lock.
  • Required by condition variables, which unlock and relock it while waiting (next lesson).
C++ unique_lock, try_lock and timeouts

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

The order of the three "worker added a line" messages, and which worker saw which count, may change between runs; the final total of 3 doesn't. The impatient thread always fails because main holds busy until after joining it, and the waiter always times out for the same reason.

Readers and writers: std::shared_mutex

Many shared structures are read far more often than they're written: configuration, caches, routing tables. A plain mutex makes readers wait for each other, although concurrent reads are perfectly safe. std::shared_mutex (C++17, header <shared_mutex>) has two modes:

  • Shared mode, taken with std::shared_lock: any number of readers at once.
  • Exclusive mode, taken with std::unique_lock or std::lock_guard: one writer, and no readers.
C++ A read-mostly cache with shared_mutex

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

How many reads saw the new value depends on when the writer got its turn, so those numbers change between runs; the final state is always dark, de, version 3. Measure before switching to shared_mutex: it costs more per lock than std::mutex, so it only wins when reads are frequent, take a while, and really happen in parallel.

One-time initialization and other mutex types

Sometimes the shared work is "initialize this exactly once", like loading a configuration file the first time any thread needs it. Two tools handle this without explicit locking:

  • A function-local static is initialized exactly once, even if many threads call the function at the same time; the others wait until initialization finishes ("magic statics", guaranteed since C++11).
  • std::call_once(flag, callable) with a std::once_flag runs the callable once, for cases where a static doesn't fit (for example, a member that's initialized lazily).
C++ call_once and thread-safe statics

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Both counters say 1, however the threads interleave. std::call_once also guarantees that the write to connects_ is visible to every thread that later passes through the same flag.

Two more mutex types exist for special cases. std::recursive_mutex can be locked again by the thread that already holds it; needing one usually means the locking design is muddled, so prefer splitting functions into a locking public part and a non-locking private part. std::timed_mutex and std::recursive_timed_mutex add try_lock_for/try_lock_until, as you saw above.

Choosing between lock_guard, scoped_lock and unique_lock

All three are RAII wrappers around a mutex. The difference is how much freedom you need:

Wrapper Mutexes Can unlock early Deferred / try / timed Movable Use it when
std::lock_guard one no no no one mutex, whole scope: the simplest case
std::scoped_lock zero or more no no no several mutexes at once (deadlock-free); also fine for one
std::unique_lock one yes yes yes condition variables, early unlock, try or timed locking, handing a lock to a caller
std::shared_lock one (shared mode) yes yes yes many readers on a shared_mutex

The rule of thumb: start with scoped_lock (or lock_guard), and switch to unique_lock only when you need something from its column. Its flag costs a little space and a branch. Two features need an example: deferred locking, where the lock object exists first and locks later, and moving ownership out of a function.

C++ Deferred locking and moving a lock

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

std::defer_lock followed by std::lock(la, lb) is the pre-C++17 way to lock two mutexes safely, and it still shows up in older code; std::scoped_lock is the modern shortcut. try_lock() on a plain mutex returns false when somebody else holds it, and calling it from a thread that already owns the mutex is undefined behavior, so the example uses try_to_lock on a free mutex.

Rules of thumb

  • Don't share if you can avoid it: give threads their own data and combine results after join().
  • When you share, put the mutex in the same class as the data and lock it in every member function that touches that data, including const ones.
  • Always lock through RAII: std::scoped_lock by default, std::unique_lock when you need its flexibility, std::shared_lock for readers.
  • Lock several mutexes with one std::scoped_lock, or always in the same order.
  • Keep critical sections short, and don't call unknown code or do I/O while holding a lock.
  • For a single counter or flag, an atomic is simpler and faster than a mutex.
  • Use ThreadSanitizer (-fsanitize=thread with GCC or Clang) in testing: it reports data races precisely, even ones that didn't change the output.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why is calling m.lock() and m.unlock() by hand discouraged?

    Lock guards unlock in their destructor, which runs on every exit path, including exceptions.
  2. 2. A class has a private std::mutex and a member function const std::vector<int>& items() const that locks the mutex and returns a reference to the internal vector. What is the problem?

    Returning a reference lets callers access the protected data after the lock is gone. Return a copy, or offer operations that do the work under the lock.
  3. 3. Thread 1 locks mutex a then b; thread 2 locks b then a. What can happen, and what is the simplest fix?

    Each thread can hold one mutex while waiting for the other forever. scoped_lock locks several mutexes with a deadlock-avoidance algorithm.
  4. 4. bool has_funds(int c) and void take(int c) each lock the account mutex. Two threads run if (acct.has_funds(80)) acct.take(80); on a balance of 100. What can go wrong?

    Each call is safe on its own, but the lock is released between the check and the action. Make the whole check-and-update one locked operation, such as withdraw().
  5. 5. When does std::shared_mutex help compared to std::mutex?

    shared_lock allows many concurrent readers while unique_lock gives writers exclusive access. It has more overhead per lock, so it only pays off for read-heavy workloads.

Summary

  • A data race (unsynchronized access to the same data, at least one write) is undefined behavior; even on one CPU core, preemption makes ++x lose updates.
  • std::mutex gives mutual exclusion; lock it with RAII types: std::lock_guard, std::scoped_lock (the default, and the one for several mutexes), std::unique_lock (unlock early, defer, try, timeouts, condition variables).
  • Protect invariants: do check-then-act under one lock, keep the mutex mutable and private next to its data, and never hand out references to protected data.
  • Avoid deadlock by locking several mutexes with one std::scoped_lock or in a fixed order, and by not calling unknown code under a lock.
  • std::shared_mutex with std::shared_lock lets readers share; function-local statics and std::call_once handle one-time initialization.

Next: Condition Variables, where threads wait efficiently for each other instead of polling.