Concurrent programs are hard to write not so much because of the sheer number of APIs, but because you must answer three questions clearly: Who owns the data, who may modify it, and what mechanism lets another thread use it safely? This article organizes C++ concurrency and parallel programming around those three questions. The code centers on the C++20 standard library and uses no third-party concurrency library.
This is Part I of the series. It covers, in order, the execution model, the build environment, thread lifecycles and argument passing, shared state and mutexes, multiple locks and deadlock, condition variables, and task result channels such as promise, packaged_task, and async. Part II goes on to discuss atomic variables and memory ordering, C++20’s cancellation and coordination facilities, engineering practice, and the implementation of a complete task processor.
Every block marked “Example N” is a complete program that compiles on its own. In its comments, @brief describes the purpose, @param / @return describe the interface, and @pre / @note / @warning spell out preconditions and concurrency boundaries. Blocks marked “snippet” serve only for local illustration.
1. Understanding How a Program Makes Progress Concurrently
1.1 Concurrency and Parallelism
A thread is a path along which instructions execute. A process may contain several threads: each has its own call stack, but they normally share the objects and address space of the process. An object living on a stack can also be reached by other threads through references, so “local variable” does not mean “private to the thread.”
Concurrency means that multiple activities make progress within the same span of time; they may be interleaved or may execute simultaneously. Even a single-core processor achieves concurrency through scheduling. Parallelism means that several activities execute at the same instant, which typically requires more than one execution resource.
For example, a chef who alternates between boiling noodles and chopping vegetables is being concurrent; two chefs, each doing one of those tasks, can be parallel. Concurrency mainly helps organize independent activities, whereas parallelism mainly helps exploit multiple execution resources. They often appear together, yet they are not the same thing.

Figure 1-1: The upper diagram shows concurrent interleaving on a single core; the lower one shows parallelism on two cores. Concurrency is not confined to a single core—several concurrent activities may also run in parallel.
You cannot assume a thread starts running merely because it was created, nor infer which of two threads will finish first from their order in the source. Operating-system scheduling, compiler optimizations, and hardware execution all shape the behavior you observe.
1.2 Synchronous, Asynchronous, Blocking, and Non-Blocking Are Different Axes
These terms differ slightly across fields; this article uses them in the following sense:
| Axis | Question it answers | Meaning and example |
|---|---|---|
| Concurrency / parallelism | How do activities progress? | Advancing together over a span of time / executing at the same instant |
| Synchronous / asynchronous | How is a completed operation’s result obtained? | The call yields the result directly / launching the operation and obtaining its result are separate |
| Blocking / non-blocking | When completion is not yet possible, does the current call wait? | Wait for the condition to hold / report the current state or temporary inability at once |

Figure 1-2: The two sets of axes are independent and combine freely; asynchronous does not imply non-blocking, nor does synchronous necessarily imply waiting.
Launching an asynchronous task can return a result handle promptly, yet a later get() on that handle may still block. The synchronous try_lock() reports the outcome of the current attempt immediately, without waiting for the lock to become available. An asynchronous API need not create a new thread per task. Note also that std::async supports a deferred strategy: it does not run the task in the background but executes it on the waiting thread at the first non-timed wait (§7.4).
Also distinguish a “non-blocking API” from the progress guarantees studied in algorithm theory. Lock-free roughly means the system as a whole keeps making progress, while wait-free requires every operation to finish within a bounded number of steps. Calling a non-blocking API in a loop does not automatically earn you either guarantee.
1.3 Correctness Covers Both Safety and Liveness
- Safety: no out-of-bounds access, no touching destroyed objects, no broken data invariants, no data races.
- Liveness: tasks eventually get a chance to finish, waits have a way out, and threads do not wait on one another forever.
- Performance: given correctness, whether latency, throughput, and resource consumption meet the requirements.
“It ran correctly ten times” samples only a handful of executions. For a concurrent program you must use the synchronization rules to explain why every allowed interleaving is safe.
2. Build Environment and Conventions for the Examples
2.1 The Compile Command, Item by Item
Save a complete example as xxx.cpp and use:
| |
| Part | What it does |
|---|---|
g++ | GCC’s C++ compiler driver; it orchestrates compilation and linking and normally links the C++ standard library automatically |
-std=c++20 | Selects the C++20 language mode; it does not backfill features an older standard library has not implemented |
-O2 | Enables a set of optimizations; optimization cannot repair a broken program and may expose problems that were previously hidden |
-Wall | Enables a common set of warnings; the “all” in the name does not mean all warnings |
-Wextra | Turns on some additional warnings |
-Wpedantic | Diagnoses certain issues and extension usages that the chosen language standard requires to be diagnosed; not a full correctness proof |
-pthread | Configures thread-related compile and link options on targets that support them |
xxx.cpp | The input source file |
-o xxx | Specifies the output file name |
Compiling translates source code into object code, linking combines object code with the required libraries into an executable, and running actually executes the program. The command above normally performs the first two steps in one go and does not run anything. Use -c to compile without linking; when compiling and linking separately, keep the threading options consistent across the relevant stages.
In a Linux/macOS terminal, run it as:
| |
On Windows it is advisable to spell out the .exe and run it in PowerShell:
| |
Whether -pthread is supported—and whether it is needed—depends on the target platform and toolchain, and you should not mechanically swap it for -lpthread: the former may also affect preprocessing and compile options. Even if a platform has folded threading into its base runtime, that does not mean every toolchain can drop the option. On macOS, a program named g++ may actually be Apple Clang, so check g++ --version first; MSVC uses a different set of command-line options. See GCC’s notes on threading in link options.
2.2 Toolchain Support and Conventions for the Examples
Older toolchains support the C++20 concurrency library only partially. Take g++.exe (tdm64-1) 10.3.0 on Windows as an example (target x86_64-w64-mingw32, standard-library date macro __GLIBCXX__=20210408): it provides __cpp_lib_jthread=201911, but no feature-test macros for semaphores, latches, barriers, or atomic wait. Whenever this article mentions “an older toolchain lacking a feature,” it refers to implementations of this kind; do not conclude from them that C++20 omits those interfaces. Probe with the feature-test macros before using a feature.
GCC’s official status table lists jthread, stop_token, and interruptible condition-variable waits as available since libstdc++ 10.1, and semaphores, latches, barriers, and atomic waits since 11.1. These are implementation-version facts; actual availability still depends on the build configuration and platform. You cannot infer “full C++20 standard-library support” from “supports C++20.” See libstdc++ C++20 implementation status.
Every block marked “Example N” is a complete program; save and compile them separately, and never put multiple main() functions in one file. Blocks explicitly labeled “snippet” are for local illustration only, and the deliberately wrong snippets are not meant to compile into working programs.
The numeric examples assume a platform where int can represent at least −2147483647 through 2147483647; C++ does not guarantee a 32-bit int everywhere. If you port to a platform with narrower integers, adjust the types and input ranges first.
The examples use fixed inputs and assert to check results. The default command does not define NDEBUG, so assertions are active. Here assertions merely express an example’s expected outcome; they are no substitute for input validation and error handling in real applications.
3. The First Rule of Threads: Manage the Lifecycle
3.1 std::thread Starts Execution; join() Reaps It
Once a std::thread is constructed successfully, a new thread of execution invokes the designated function. The thread object is a management handle for that thread of execution: it cannot be copied, but it can be moved. That the thread function has returned does not mean the handle no longer needs to be reaped.
Let us first look at the class’s interface as a whole; Example 1 then shows its minimal usage.
Construction and launch. The constructor most often used to pass in a task takes a template form (snippet; f and the arguments must satisfy the callable requirements):
| |
f may be an ordinary function, a lambda, a function object, or a pointer to member function—in the last case the first argument is the calling object (an object copy, a pointer, or a std::ref wrapper). If the thread cannot be started (because resources are exhausted, say), std::system_error is thrown; copying or moving the callable and arguments into storage may throw other exceptions too. These exceptions occur in the caller that constructs the thread, and a failed construction leaves behind no new thread that the caller would need to join. The arguments are first copied or moved into internal storage per §3.3, and only then is f invoked via INVOKE. The thread function’s return value is thrown away: to bring a result back, use a reference argument (Example 1), guarded shared state (§4), or a future result channel (introduced in §3.4 and developed fully in §7).
The remaining construction forms: default construction associates no thread; copy construction and copy assignment are deleted (the handle owns uniquely); move construction and move assignment transfer management, after which the source has no associated thread (move assignment also requires care about whether the destination is already joinable).
Principal member functions and related utilities:
| Interface | What it does | Caveats |
|---|---|---|
join() | Blocks until the associated thread ends and reaps it | Requires a joinable thread; its return synchronizes with the thread’s completion (Example 1 relies on this) |
detach() | Severs the handle’s association with the thread, which then runs independently | Requires a joinable thread; the handle offers no further join, so completion and error channels must be designed separately; generally best avoided (§3.3) |
joinable() | Whether the handle still has an associated thread | Does not mean “currently running”; full state rules below |
get_id() | Returns the associated thread’s std::thread::id | Yields a default-constructed id when there is no association; ids are comparable and streamable, handy for logging and diagnostics |
native_handle() | Returns the platform’s native thread handle | For platform-specific APIs; code using it is no longer portable |
hardware_concurrency() (static) | Hints at the amount of hardware concurrency the implementation can offer | A hint only, and returning 0 is allowed; not an exact core count, nor the number of threads you “should” create (Part II, §10.1) |
std::this_thread::get_id() | Returns the current thread’s id | A free function, callable from any thread |
std::this_thread::sleep_for() / sleep_until() | Suspends the current thread for a span of time | Establishes no synchronization and cannot “fix” a data race (§4.1) |
std::this_thread::yield() | Hints that the current thread give up its execution slice | A scheduling hint only: no switch is guaranteed, and it is no synchronization tool |
The full interface specification is in the C++20 draft’s thread class.
Example 1: Passing by value and passing by explicit reference.
| |
The output is 49. The main thread does not read result before join(), and result stays alive throughout. A successful join() return synchronizes with the thread’s completion, so no additional mutex is needed here.
Why the argument needs std::ref, and how that differs from a lambda’s reference capture, is covered in §3.3.
joinable() means the handle still has an associated thread, not that it is “running.” A default-constructed object, one moved from, and one successfully join()ed or detach()ed are no longer joinable. Both join() and detach() require the object to remain joinable; calling either when it is not reports std::system_error, and you cannot reap or detach twice. join() also cannot wait on the calling thread itself. After a move the source is no longer joinable and the destination takes over the association; move-assigning to a std::thread that is still joinable likewise calls std::terminate(). Operations on one and the same thread handle are not automatically synchronized with each other, so a single owner should be responsible for it.
Lifecycle red line If a
std::threadis destroyed while still joinable,std::terminate()is called and the program ends. This is the error-handling policy prescribed by the standard: it keeps an implicit wait from masking a block, and keeps an implicit detach from letting referenced objects’ lifetimes run wild. You must settle the association explicitly before destruction.

Figure 3-1: State transitions of a thread handle; the red line marks the consequence of destroying a joinable object outright. That is not the only way a program can trigger std::terminate().
In Example 1 nothing between successful construction and join() can throw; in real, more intricate code, prefer RAII.
3.2 RAII and std::jthread
RAII binds a resource’s release to an object’s destructor: cleanup happens automatically when the scope is left, exception unwinding included. A lock guard takes care of unlocking; a thread-management object takes care of reaping the thread.
When a C++20 std::jthread is destroyed while still joinable, it first requests a stop and then calls join(). A stop request is only a notification; whether the thread reacts depends on the thread function, a topic taken up in Part II, §9.1.
How it relates to std::thread, and how it differs. Strictly speaking, these are two classes in <thread>, not two member interfaces of one class. Both manage a thread of execution and both are move-only and non-copyable; jthread is not another scheduling model, nor a subclass of thread, and the two cannot be converted into one another directly. You may think of jthread as a thread-management class offering “automatic reaping plus cooperative stop,” but do not infer any particular internal implementation from that.
| Aspect | std::thread | std::jthread |
|---|---|---|
| Standard version | Since C++11 | Since C++20, with standard-library support required |
| Destroyed while joinable | Calls std::terminate(); the association must be settled beforehand | Calls request_stop() then join(); may block |
| Stopping mechanism | No built-in stop-request interface; an exit protocol must be designed separately | Built-in stop state and request interface, though the thread must still cooperate |
| Explicit wait | join() waits for the thread to end | Also supports join(), which by itself requests no stop |
| Move-assigned to while destination is joinable | Calls std::terminate() | Requests the old thread to stop and waits for it before taking on the new one |
These behaviors can be checked against the C++20 draft’s thread class and the jthread class.
When to reach for each?
- In new C++20 code, prefer
jthreadwhen a scope or object owns the thread and must reap it before exit—especially where an exception or early return may intervene. Finite tasks needing no cancellation can use it as well; Example 2 below accepts no stop token. - Long-lived workers that need cooperative stopping suit
jthread: a processing loop, for example, can clean up and return on a stop request, but both the compute path and any wait path must provide a way out; see Part II, §9.1. - Use
threadfor compatibility with C++11/14/17, when a legacy library interface demandsstd::thread, or when a complete thread-management wrapper already exists. In that case the wrapper or an RAII guard should cover normal and exceptional paths alike, rather than a lonejoin()at the function’s end. In C++20 you can also pair a standalonestop_source/stop_tokenwiththread; cooperative stopping is not exclusive tojthread.
Automatic reaping does not automatically solve concurrency problems
jthreadcannot forcibly terminate a thread; if the work waits forever or never exits, destruction may wait forever as well. Do not hold a lock the worker needs while waiting for it. When you want to control when the wait happens, an explicit earlyjoin()remains perfectly fine—no reason to switch tothreadfor that. Both classes supportdetach(), but once detached the object no longer reaps automatically, so detachment is no universal cure for blocking.jthreadalso does not protect shared data, extend the lifetime of referenced objects, or forward thread exceptions.
jthread at a glance. Its launch timing, argument passing, and joinable-state rules match std::thread (§3.1, §3.3); the differences center on automatic reaping and cooperative stopping:
| Interface | What it does | Caveats |
|---|---|---|
| Constructor | Starts the thread like std::thread; if f(token, args...) is callable it injects a stop token automatically, otherwise it calls f(args...) | Construction works even if the function takes no token; reacting to stopping depends on whether the function observes the stop state through parameters, captures, and so on |
request_stop() | Issues one stop request to the associated stop state | A notification only; its return does not mean the thread has exited |
get_stop_token() | Obtains a token for observing the stop state | Normally injected by jthread automatically, so rarely fetched by hand |
get_stop_source() | Obtains a stop_source able to issue requests | Lets you pass the ability to request a stop to another owner |
join() / detach() / joinable() / get_id() | Same as std::thread | join() requests no stop; reaping and stopping are independent matters |
The destructor’s “request stop, then join” is exactly the combination of request_stop() and join(). The stopping mechanism itself is covered in Part II, §9.1; the interface specification is in the C++20 draft’s jthread class.
Example 2: Automatic reaping and carrying an exception across threads.
| |
The output is worker failed. Only one thread writes error, and the main thread reads it after reaping, so no lock protects it. The catch (...) has a clear purpose here: handing any worker exception to the caller rather than silently swallowing it.
An uncaught exception in a thread’s entry function calls std::terminate(); a try/catch around the code that creates the thread catches only failures at creation time, never exceptions thrown while another thread runs. jthread does not forward exceptions automatically. Futures, introduced later, serve as a more natural channel for both results and exceptions.
3.3 Arguments, Captures, and Ownership
When constructing a thread, ordinary arguments are copied or moved into internal storage using their std::decayed types (references and top-level cv-qualifiers stripped, arrays and function names decayed to pointers), then used to call the function. std::ref(x) neither copies x nor extends its lifetime; std::cref(x) is the corresponding const-reference wrapper, appropriate when the thread only reads shared data.
3.3.1 Ordinary Arguments and Reference Wrappers
calculate in Example 1 is an ordinary function: its parameter list fixes what it receives, while std::thread decides how those arguments are stored first and then handed over. std::ref / std::cref alter only the passing semantics; they provide neither locking nor automatic lifetime extension.
Why must you write
std::ref(result)here? During constructionstd::threadfirst copies or moves the arguments into its own internal storage, using the types with references stripped away (§3.3). Passresultdirectly and the stored value is anintcopy; callingcalculatethen passes it as an rvalue, which cannot bind to the function’sint¶meter, so thread construction fails at compile time.
std::ref(result)does not copyresult; it wraps it as astd::reference_wrapper<int>. The wrapper itself copies safely into internal storage and converts implicitly back toint&when the thread function is invoked, so the function still gets a reference to the main thread’s originalresult. In short,std::refresolves the tension between “arguments must first be copied into storage” and “the function expects a reference.” It transfers no ownership and extends no lifetime; keeping the object alive remains the caller’s job. The construction rules are in the C++20 draft’s thread constructors.
std::reference_wrapper<T> offers two common ways to reach the reference: the member get() returns T& explicitly, while the implicit conversion operator operator T& lets the wrapper be used wherever a T& is wanted. std::ref(t) preserves the original object’s constness: an ordinary T gives reference_wrapper<T>, a const object gives reference_wrapper<const T>. std::cref(t) supplies const-reference access; choose between the two just as you would between T& and const T& parameters.
Correct snippet (inside a function body; take(int&) is declared and defined, and <functional> is included):
| |
Beyond thread argument passing, wrappers have two other typical uses: preserving reference semantics with std::bind or by-value captures (std::bind(f, std::ref(x))), and storing “references” in containers (std::vector<std::reference_wrapper<int>>), sidestepping the rule that references cannot be container elements. Two caveats apply: the wrapper does not own the object, so calling get() after the original is destroyed yields a dangling reference; and the overload for wrapping temporaries is =delete, so std::ref(42) will not compile—this prevents wrapping an object about to vanish.
3.3.2 Lambdas: Captures Save an Environment, Parameters Are Call-Time Inputs
A lambda expression creates a closure object—think of it as a callable carrying the data it needs. Its form is [capture list](parameter list) mutable -> return type { body }, with mutable and the return type omitted when appropriate. Captures happen when the closure is created, parameters are supplied when the closure is called; the closure is then copied or moved into the thread like any other callable.
| Form | What it stores | What you must prove in concurrent code |
|---|---|---|
[] | No outer local variables captured; parameters, global objects, etc. remain usable | Capturing nothing is not thread safety; shared global state still needs synchronization |
[x] | A copy of x at closure creation | Later changes to the original x do not affect the copy; the copy itself must not race with writes to x |
[&x] | A reference to the original x | The object must outlive the last access; conflicting concurrent accesses need their own synchronization |
[=] / [&] | Implicitly needed locals captured by value / by reference by default | Does not capture every variable in scope unconditionally; for thread entry points, list dependencies explicitly |
[x, &result] | An input snapshot and an output reference | The input is independent; read the result after join, and never treat a reference as a lock |
[p = std::move(p)] | Init-capture, moving a resource into the closure (since C++14) | The original owner no longer touches the moved-from resource; move the closure into the thread with std::move |
[this] | A pointer to the current object | Does not own the object; it must not be destroyed while the thread uses it |
[*this] | A copy of the current object (since C++17) | The type must be copyable; members such as pointers follow their own copy semantics |
By default a closure’s call operator is const, so you cannot modify an ordinary by-value capture inside it; adding mutable permits modifying that copy, without changing the original variable. Reference captures can still modify the original object and need no mutable for that. If several threads share and invoke the same mutable closure, a data race remains possible; mutable is no synchronization mechanism.
Example 3: Rewriting Example 1’s square computation with lambdas to show an input snapshot, a mutable copy, reference write-back, and exclusive resource transfer.
| |
The output is 49 64 81 81. [input, &result] expresses the same data arrangement as Example 1’s calculate(input, std::ref(result)): the input is stored independently, the output references the caller’s object, and the result is read after join. Rewriting it as [](int value) { return value * value; } would receive the input at call time; handing that to std::thread still discards the return value, so §3.4 uses async to pick it up.
flowchart TB
Source["Local object x at closure creation"]
Source -->|"[x]: copy"| Copy["The closure owns its own copy of x"]
Source -->|"[&x]: reference"| Borrow["The closure accesses the original; no lifetime extension"]
Owner["Exclusive resource p"] -->|"[p = std::move(p)]"| Move["The closure takes ownership of the resource"]
Copy --> Thread["Closure copied or moved into the thread"]
Borrow --> Thread
Move --> Thread
Thread --> Join["Reap the thread before destroying referenced objects"]
classDef owned fill:#e8f2fc,stroke:#4682b4,color:#182c42
classDef borrowed fill:#fff4dc,stroke:#b8860b,color:#463611
class Copy,Move owned
class Borrow borrowed
Figure 3-2: The capture scheme determines what the closure stores; thread reaping and access synchronization still require separate design.
3.3.3 Member Captures and Object Lifetime
In a member function, if [=] implicitly captures this, what is stored is still a pointer rather than the whole object; that implicit form is deprecated in C++20. Neither [this] nor [&] extends the object’s lifetime. Prefer capturing copies of the specific members the task needs, as in [value = value_]; use [*this] only when an object snapshot is genuinely required and the type is copyable. Raw pointers, string_view, span, and reference wrappers do not own the data they refer to even when captured by value.
Correct snippet (<thread> is included; it demonstrates a snapshot of a scalar member and involves no shared modification):
| |
Capture rules are specified in the C++20 lambda capture rules. Example 11 in §7.2 applies the same kind of move capture to a promise: the capture scheme determines both callability and who is responsible for delivering the result.
3.3.4 Detached Threads Cannot Replace Lifetime Management
Wrong example—do not copy it (snippet; the necessary headers are assumed included):
| |
detach() severs the management handle’s association with the thread of execution; it neither waits for completion nor extends the lifetime of referenced objects. If the thread accesses local after the function returns, it is accessing an object whose lifetime has ended—undefined behavior. If the thread happens to finish early, the code may look correct.
The fix is either an inner jthread scope as in Example 2, which guarantees the wait precedes destruction of the data, or a genuine transfer of the data ownership the task needs. Even if by-value capture removes the dangling reference, the questions of how a detached task exits, reports errors, and behaves at program shutdown remain, so avoid naked detach().
Declaration order matters as well: local objects are destroyed in reverse order of construction. Construct shared data, mutexes, and condition variables first, and the threads that reference them afterward. Automatic reaping cannot cure deadlock; holding a lock during destruction while waiting for a thread that needs that very lock stalls just as readily. The C++20 draft’s jthread specification gives the order of the stop request and reaping.
3.4 Retrieving a Thread’s Return Value with async/future
§3.1 established that a thread function’s return value is discarded outright. Example 1 worked around that by having the main thread prepare a result variable and pass a reference to it with std::ref, so the worker could “write back.” A more direct route exists: instead of writing back into a variable, let the task deposit its return value into a result channel visible to both sides. Under the C++20 draft’s async specification, std::async starts the task and wires up this channel automatically, while std::future acts as the caller’s “claim check” for it. The full mechanism—shared state, promise, packaged_task, shared_future, and destruction rules—unfolds in §7; this section presents only enough to cover the common case of “compute a value and bring it back.”
Three steps are central: explicitly pass std::launch::async to launch the task, store the returned future, and finally call get() to wait and retrieve the value or exception. Launching tasks guarantees neither that the CPU executes them simultaneously nor that they are scheduled immediately.
sequenceDiagram
participant Main as Main thread
participant Worker as Asynchronous task
participant State as Shared state
Main->>Worker: async(launch::async, square, 7)
Note over Main: Save the future and continue with independent work
Worker->>State: Store 49 or an exception, mark ready
Main->>State: future.get(), waiting if necessary
State-->>Main: Retrieve the value or rethrow the exception
Note over Main: After get, this future no longer has an associated state
Figure 3-3: Launching and retrieving are separate operations; the task may finish before get or while get waits. The diagram draws just one possible ordering.
Example 4: Retrieving a return value and an exception with async and future.
| |
The output consists of three lines—task launched; main thread free, 49, and no square for negative input—all written by the main thread, so their order does not depend on thread scheduling. Compared with Example 1, the computation is the same but needs no prearranged shared output variable, no std::ref, and no explicit join(): “compute a value and bring it back to the main thread” collapses into one std::async call plus one get().
Save the handle first, then retrieve the result An ordinary future’s
get()consumes the result only once;valid()merely says an associated state exists, not that the task has finished. Do not discard the temporary handle returned by async—its destructor may wait implicitly. The complete interface is in §7.1, launch policies and destruction rules in §7.4–§7.5.
If a task merely computes one result, start with async as shown here. If you need a long-lived worker thread, explicit delivery timing, or queue-based scheduling, go on to promise / packaged_task in §7 and the task processor in Part II, §11. async is not C++20’s general-purpose thread-pool interface.
4. Why Shared Mutable State Is Dangerous
4.1 Data Races and Race Conditions
A data race is an error in the C++ memory model: conflicting accesses to one memory location by different threads, at least one of them non-atomic, with no required happens-before ordering between them. The common case is one thread writing an ordinary object while another reads or writes it. A data race causes undefined behavior.
A race condition is a broader logical problem: whether the result is correct depends on how events interleave. Even when every access uses atomics or locks and no data race exists, a logical race can still arise.
Wrong example—do not copy it (snippet; count is an ordinary integer initialized to 0):
| |
++count involves a read, a computation, and a write; one line of source does not make it indivisible. Both threads may read 0 and then write back 1, but “a lost update” is only a way to picture it—undefined behavior is not confined to ending up with 1 or 2. Optimizers may transform the program on the assumption that no data race exists.

Figure 4-1: Two threads interleaving ++count, so only one of the two increments takes effect. Bear in mind that a data race is undefined behavior; “a lost update” is merely one possible consequence.
volatile supplies neither mutual exclusion nor synchronization between threads and cannot repair this. Adding sleep_for() establishes no synchronization either. The fix calls for an atomic read-modify-write, or for placing the whole operation inside a critical section guarded by one mutex.
Another logical race: an account balance is checked under one lock and debited under another, so two threads each observe “sufficient funds” and both debit. Every individual method may be free of data races, yet “check then debit” is not a single operation. Put the entire business rule inside one critical section. The definition of a data race can be checked in the C++20 draft on conflicting accesses and happens-before.
4.2 A Mutex Provides Both Mutual Exclusion and Synchronization
A std::mutex does not automatically tag any variable as protected; the programmer must agree on which data each lock guards and ensure every relevant access honors that agreement.
It plays two roles:
- Mutual exclusion: only one thread holds the lock at a time, so one whole critical section cannot interleave with another protected critical section.
- Synchronization: an unlock synchronizes with a later lock acquisition on that mutex, letting writes from the earlier critical section be observed safely by the later one.
Three member functions deal directly with locking: lock() blocks until it acquires the mutex, try_lock() makes one attempt and reports the outcome immediately, and unlock() releases the mutex and must be called by the owning thread. There is also native_handle(), which returns the platform’s native handle for platform-specific APIs, at the same cost of portability as the similarly named interface in §3.1. Application code should not call these locking members directly: manual pairing tends to miss unlock() on exceptions or early returns. They are the low-level interface beneath lock-management objects (§4.3), which everyday code uses indirectly through RAII guards. A default-constructed std::mutex is unlocked and is neither copyable nor movable. The specification is in the C++20 draft’s mutex class.
Example 5: Protecting a shared counter.
| |
The output is 20000. If the main thread wants to inspect count while the threads are running, it must take the same lock too. Locking writes but not reads, or letting each thread use its own lock, establishes none of the required protection.
This example deliberately locks on every iteration to expose the semantics; real counting is better done by accumulating into local variables first and merging later, for reasons discussed further on. The standard synchronization specification is in mutex requirements.
4.3 Choosing a Lock-Management Object
| Type | Where it fits | Caveats |
|---|---|---|
lock_guard | Lock on scope entry, unlock on exit | Simple and cheap; no manual unlock |
unique_lock | Condition-variable waits, deferred locking, early unlock needed | Movable, tracks ownership; use its richer facilities as needed |
scoped_lock | Acquiring several mutexes together | Since C++17; uses a deadlock-avoidance algorithm for multiple locks |
shared_lock | Taking a shared (read) lock with shared_mutex | Since C++14; flexibility similar to unique_lock |
Do not create an unnamed temporary lock guard—it is destroyed at the end of the statement. Keep critical sections short, but never split a business process that must complete atomically. While holding a lock, steer clear of unpredictable operations such as invoking unknown callbacks, doing slow I/O, or waiting on other tasks.
std::mutex is a non-recursive exclusive lock: the same thread cannot lock it twice, and only its owner may unlock it. std::lock_guard and std::scoped_lock are neither copyable nor movable; std::unique_lock and std::shared_lock are movable but not copyable, and a move transfers responsibility for managing the lock without extending the underlying lock’s lifetime. Moving a guard does not change which thread holds the underlying mutex: with a standard mutex you cannot hand a guard that already holds the lock to another thread for unlocking or destruction; shared locks must likewise be released by the thread that acquired the shared ownership.
The next sections explain all four guards in turn—their purpose, common construction forms, member interfaces, and typical uses. The underlying mutex determines the locking rules, while the guard decides when to call the lock and unlock operations; keep these two layers distinct before examining construction tags.
4.3.1 Locking Policy Tags
Locking policy tags. A guard’s constructor accepts the following tags to vary its locking behavior at construction (all defined in <mutex>, with types such as defer_lock_t; each tag is the corresponding constant object):
| Tag | Behavior at construction | Usable with | Precondition / how to check |
|---|---|---|---|
std::defer_lock | Associate only, do not lock | unique_lock, shared_lock | None; acquire later with member lock() or std::lock |
std::try_to_lock | Try to lock once, no blocking on failure | unique_lock, shared_lock | The underlying try-lock preconditions must hold; use owns_lock() to check |
std::adopt_lock | Do not lock; take over directly | All four | The current thread must already hold the lock in the guard’s corresponding mode; shared_lock requires shared mode, otherwise the behavior is undefined |
lock_guard accepts only adopt_lock (it has no intermediate “associated but unlocked” state); scoped_lock likewise accepts only adopt_lock (its internal algorithm handles normal acquisition of several locks).
4.3.2 lock_guard: Exclusive Ownership Within a Scope
std::lock_guard<Mutex>: the simplest exclusive guard. It does “lock on scope entry, unlock on scope exit” and nothing beyond that: there are no member functions to operate on, not even mutex() for reaching the underlying lock. It provides a plain lock()/unlock() pairing; the standard promises neither a particular object size nor zero overhead.
| Interface | What it does | Caveats |
|---|---|---|
explicit lock_guard(Mutex& m) | Constructs and calls m.lock() | Blocks until acquired |
lock_guard(Mutex& m, std::adopt_lock_t) | Takes over an already-held lock | The current thread must already hold m |
~lock_guard() | Calls m.unlock() | The only unlocking route; applies on exception paths too |
| Copy/move constructors and corresponding assignments | Copying is deleted; no usable move operation | Lock ownership cannot be transferred |
Correct snippet (function definition, <mutex> included; m and shared_data outlive the call, and the caller does not yet hold m):
| |
The typical case is one exclusive lock whose critical section is exactly one scope—the shape of most locking code. The moment you need deferred, try, or timed locking, or an early unlock, switch to unique_lock.
4.3.3 unique_lock: Flexible Control of an Exclusive Lock
std::unique_lock<Mutex>: a full-featured exclusive-lock manager. Compared with lock_guard it keeps one extra piece of state—whether the lock is owned—and thereby supports deferred, try, and timed locking, early unlock, relocking, release of management, and transfer by move. std::condition_variable::wait() accepts only this guard (§6.1), because waiting must temporarily release and reacquire the lock. Full interface:
| Interface | What it does | Caveats |
|---|---|---|
unique_lock() | Empty guard associated with no lock | Can take over a lock later via move assignment |
explicit unique_lock(Mutex& m) | Constructs and calls m.lock() | Blocks until acquired |
unique_lock(m, defer_lock_t) | Associates without locking | Pairs with std::lock (Example 7) or a later lock() |
unique_lock(m, try_to_lock_t) | Makes one attempt during construction | Check the result with owns_lock() |
unique_lock(m, adopt_lock_t) | Takes over an already-held lock | The current thread must hold m before the call |
unique_lock(m, duration) / unique_lock(m, time_point) | Timed try-lock | Requires a TimedLockable, such as std::timed_mutex |
lock() / try_lock() | Lock / try once | The guard must be associated with a mutex and not holding it; otherwise std::system_error is thrown, and the underlying lock’s preconditions must also hold |
try_lock_for() / try_lock_until() | Timed try-lock | Same as above; returns whether the lock was obtained |
unlock() | Unlocks early | Requires current ownership; lock() may be called again, but business preconditions must be rechecked |
release() | Dissociates and returns Mutex*, without unlocking | Passes the unlocking duty to the caller; misuse means holding the lock forever |
owns_lock() / operator bool() | Whether the lock is held | Queries the internal state bit |
mutex() | Returns the associated Mutex* | Returns nullptr when there is no association |
swap() | Swaps state with another guard of the same type | — |
| Move constructor / move assignment | Transfers lock-management responsibility | Copying is deleted; does not extend the underlying lock’s lifetime |
Correct snippet (function definition, <chrono> and <mutex> included; timed_mutex is used to demonstrate timed try-locking):
| |
Move semantics allow unique_lock to return an “already-locked” guard from a function, handing lock-management responsibility to the caller within the same thread; release() merely abandons management and returns a pointer to the underlying lock without unlocking—a design lock_guard cannot express. The price is maintaining ownership state and more involved usage rules; the real cost depends on the implementation and optimization, and misuse (such as calling unlock() on a guard that holds no lock) throws std::system_error at runtime rather than being caught at compile time.
4.3.4 The Mutex Family and Timed Try-Locking
The mutex family and std::timed_mutex. unique_lock_demo above chose std::timed_mutex; let us make that clear. The standard library offers a family of mutex types differing along just three axes—exclusive versus shared, recursive locking or not, and timed try-locking or not:
| Type | Header | Capability | Difference from std::mutex |
|---|---|---|---|
std::mutex | <mutex> | BasicLockable + Lockable | Baseline: exclusive, non-recursive, no timeouts |
std::timed_mutex | <mutex> | Also TimedLockable | Adds timed try members (table below) |
std::recursive_mutex | <mutex> | BasicLockable + Lockable | Lets the same thread lock repeatedly, with matching unlocks |
std::recursive_timed_mutex | <mutex> | Also TimedLockable | Recursive plus timed |
std::shared_mutex | <shared_mutex>, C++17 | Exclusive and shared locking | Adds a shared (read) mode, see the example in §4.3.6 |
std::shared_timed_mutex | <shared_mutex>, C++14 | Shared + Timed | Timed try-locking in shared mode too |
The interface of std::timed_mutex equals std::mutex’s three lock/unlock members plus two timed ones:
| Interface | What it does | Caveats |
|---|---|---|
lock() / try_lock() / unlock() | Same as std::mutex | Non-recursive: repeated lock() by one thread is undefined behavior |
try_lock_for(duration) | Tries within a relative timeout budget, returning true only when acquired | Allows spurious failure (false even if the lock is free); returns false on timeout; the actual return may be delayed |
try_lock_until(time_point) | Tries until an absolute time point | The clock type comes from the template argument; adjusting the system clock affects the upper bound on waiting |
native_handle() | Platform-native handle | Loses portability (§4.2) |
Semantic points: the standard recommends that implementations measure try_lock_for’s relative timeout with a steady clock, though this is not a binding requirement, while try_lock_until uses the caller’s chosen clock. A timeout budget is not a hard bound on the real-time return—scheduler delays may push the return later, and acquiring the lock carries no fairness promise. Interaction with guards: lock_guard has no timed interface and locks normally even with a timed_mutex; timed capability is forwarded through unique_lock’s timed constructors and its try_lock_for()/try_lock_until() members (unique_lock_demo takes exactly this route). Do not treat a timeout as a cure for deadlock: a timeout merely gives the caller a way to stop waiting, and after a false result the business logic still needs a policy to abandon or retry; badly ordered multi-lock acquisitions can keep timing out and form a livelock even if each attempt is timed (§5.5).
Correct snippet (function definition, <chrono> and <mutex> included; m and the data outlive the call, and the caller does not yet hold m):
| |
A typical case is allowing abandonment once waiting exceeds a business budget, or logging diagnostics after a timeout. The approach cannot promise that an interface never stutters, nor that a periodic task returns by a hard deadline. Ordinary background code can stick with std::mutex and upgrade to timed_mutex only when timeouts are genuinely needed—the real cost depends on implementation and load, so do not assume timed locks must poll or must be slower. The specification is in the C++20 draft’s timed-mutex requirements.
4.3.5 scoped_lock: Managing Several Locks Together
std::scoped_lock<Mutex...>: acquire zero, one, or several locks at once (C++17). Several locks are obtained together through a deadlock-avoidance algorithm (the same origin as std::lock, see Example 7 / Example 9); with exactly one lock it is equivalent to lock_guard; with none it does nothing. Its interface has only constructor and destructor—no defer/try/unlock. It deliberately offers no flexibility, suiting multi-lock scopes that need no mid-scope unlock.
| Interface | What it does | Caveats |
|---|---|---|
explicit scoped_lock(Mutex&... m) | Acquires all locks; multiple locks go through the deadlock-avoidance algorithm | Blocks until all are acquired |
scoped_lock(std::adopt_lock_t, Mutex&... m) | Takes over all already-held locks | Each must already be held by the current thread |
~scoped_lock() | Releases all locks | Calls unlock on every associated lock; the standard does not prescribe an order |
| Copy/move constructors and corresponding assignments | Copying is deleted; no usable move operation | Multi-lock ownership cannot be transferred |
Correct snippet (function definition, <mutex> included; a and b are two distinct mutexes, neither held by the caller):
| |
The typical case is having to hold several exclusive locks at once (transfers, snapshots, double-buffer swaps). New code should prefer this over a manual unique_lock + std::lock combination; fall back to the unique_lock form only when deferred locking, early unlock, or a condition variable is also needed.
4.3.6 shared_lock: Shared Reads and Exclusive Updates
std::shared_lock<SharedMutex>: a shared (read) lock manager (C++14). It pairs with std::shared_mutex / std::shared_timed_mutex: several readers’ shared_locks may be held at once, mutually excluding a writer’s exclusive lock. Its interface broadly mirrors unique_lock; the key difference is that all lock and unlock operations go through the underlying lock’s shared-mode members such as lock_shared()/unlock_shared(). The exclusive (write) lock is still taken with unique_lock, and the read_settings/write_settings snippet below shows precisely that pairing.
| Interface | What it does | Caveats |
|---|---|---|
| Construction forms | Similar to unique_lock: default, direct, defer, try_to, adopt, timed, and move constructors | Timed overloads require the underlying type to support timed shared locking, e.g. shared_timed_mutex |
lock() / try_lock() | Lock / try once in shared mode | Goes through lock_shared() / try_lock_shared() |
try_lock_for() / try_lock_until() | Timed try in shared mode | Goes through try_lock_shared_for() etc. |
unlock() / release() | Release early / abandon management without unlocking | Same semantics as unique_lock |
owns_lock() / operator bool() / mutex() / swap() | State queries and accessors | Same semantics as unique_lock |
| Move constructor / move assignment | Transfers shared-lock management responsibility | Copying is deleted |
std::shared_mutex supports both shared and exclusive modes: several readers may hold shared locks simultaneously; a writer holding the exclusive lock excludes every other reader and writer. Shared mode admits multiple holders at once and is not mutually exclusive in itself; this example stipulates that shared locks only read the common configuration while updates take the exclusive lock. Read locks still carry a cost, promise no speedup, and guarantee no fairness; start with a plain mutex and reassess once you have measured a bottleneck.
Correct snippet (<mutex> and <shared_mutex> included; callable from several threads):
| |
std::shared_mutex offers no standard interface for atomically upgrading a read lock to a write lock; after releasing the read lock and acquiring the write lock, you must recheck the state you read earlier.
How to choose (decision path): one exclusive lock with the critical section equal to a scope → lock_guard; deferred/try/timed locking, early unlock, pairing with a condition variable, or returning a lock from a function → unique_lock; several exclusive locks held together → scoped_lock; readers of a shared_mutex → shared_lock, writers → unique_lock. The capabilities demanded of the underlying lock vary with usage: exclusive capability runs from BasicLockable (lock/unlock) to Lockable (adding try_lock) to TimedLockable (adding timed attempts); shared mode has its own lock_shared / try_lock_shared and timed requirements, which are not simply the last rung of that chain. Guards merely forward these requirements to the template parameter of the underlying lock, and compilation fails at instantiation when they are unmet. The specification is in the C++20 draft’s lock-management requirements.
The complete procedure for acquiring multiple locks, with Example 7, is in §5.2; the business invariant for account payments, with Example 8, is in §5.3.
4.4 One-Time Initialization: std::call_once
Some data needs initializing only on first use—a global configuration, for example. The instinct is to “check, then initialize,” but several threads may pass the check at once and initialize repeatedly; concurrent reads and writes of an ordinary variable also create a data race. Hand-rolled double-checked locking is hard to get right as well: even if a thread sees “initialized,” without proper atomics and memory ordering it cannot be sure of seeing the fully initialized result.
With std::call_once you supply a std::once_flag and an initialization function. For one and the same flag, the standard library guarantees:
- When several threads call concurrently, one executes the initialization function while the others wait.
- Once that function returns normally, the flag is marked complete and later calls do not initialize again.
- Other normally returning calls synchronize with that initialization, so the published result can be read safely.
Here “once” means one initialization that completes successfully, not “the initialization function is entered only once.” If initialization throws:
- The exception propagates to the caller that actually ran the initialization function.
- The flag is not marked complete, so other waiting calls or later calls may retry.
- Side effects produced before the failure are not rolled back automatically, and a retry must account for that.
Correct snippet (<mutex> and other headers assumed included, Config and load_defaults() defined):
| |
call_once only publishes the result of initialization; if config may be modified later, subsequent reads and writes still need their own synchronization.
Exception retry depends on the target toolchain’s implementation Under the C++ standard, later calls may retry after initialization throws. On TDM-GCC 10.3.0, however, testing showed a timeout at the second call; this resembles the behavior described in GCC’s note on exception support for non-Linux targets, but is not enough to establish the root cause. Distinguish the standard’s rule from one implementation: an implementation’s retry timeout does not turn the rule into “throwing also counts as completed initialization.”
In practice you can choose as follows:
- One initialization state shared across call sites: use
call_onceand pass the same flag every time. Flags are neither copyable nor resettable; when you need a completely fresh round of initialization, use a new flag. - One lazily initialized object inside a single function: prefer a local
static. Since C++11 its initialization is thread-safe too, and the code is simpler.
The interface semantics are in the C++20 draft’s call_once specification.
4.5 Write the Invariant First, Then Fix the Critical Section
An invariant is a rule that must hold before and after an operation—a non-negative balance, conservation of the total across accounts, a queue length within its capacity. A lock should protect the entire rule: checking preconditions, computing, and committing the change constitute one business operation, and locking each field separately will not do.
At design time, write down three things: who owns the object; which lock protects which state; and the instant at which the operation takes effect as a whole. That instant is often called the linearization point—a successful CAS that decrements stock, say, or an enqueue completed while holding the queue lock. Observers must honor the protocol too: a consistent snapshot of several fields requires protecting all of them together, and individually safe reads do not make a combined result consistent.
5. Multiple Locks, Deadlock, and Progress Problems
5.1 Deadlock Is a Cycle in the Wait Relations
Wrong example—do not copy it (snippet; <mutex> is included, a and b are two distinct mutexes, and two threads call the following functions respectively):
| |
If A holds a while waiting for b and B holds b while waiting for a, neither can ever proceed. The trouble here is a liveness failure, not a data race or inevitable undefined behavior. Some runs happen to let A take both locks first, which hides it.

Figure 5-1: A resource-allocation graph. Solid arrows run from a lock to its holder, dashed arrows from a thread to the lock it awaits; A → b → B → a → A forms a directed cycle. Drop the lock nodes and keep only A waiting for B and B waiting for A, and you have a wait-for graph.
The opposing order above is known as ABBA. Requiring every path to lock a before b breaks this example’s circular wait. The classic four Coffman conditions are mutual exclusion, holding some resources while waiting for more, no preemption of resources, and circular wait. You can break the cycle by standardizing lock order, acquiring several locks safely in one operation, or reducing nested lock ownership.
Example 6 (a deliberate deadlock demonstration—do not copy it): nested locking in opposite orders reproduces ABBA. bad_a/bad_b above were only snippets; this example turns them into a runnable program so you can observe the deadlock directly.
| |
How to observe it: set a timeout in a separate process. Under ABBA the program produces no output and cannot finish on its own; if it never hits the dangerous interleaving, it may complete and print 2000 2000. sleep_for() only widens the window between the two locks and cannot guarantee that the other side has taken its first lock. Nor can a timeout prove deadlock from a single observation; the fault is established by the cycle in lock dependencies, with the test offering only supplementary evidence.
Why a version with two separate sequential loops does not deadlock. If each thread is written as “finish a loop guarded by lock_guard on a, then start a loop guarded by b,” each thread holds at most one lock at any moment: by the time it contends for the second, the first has long been destroyed and released, so hold-and-wait fails and, absent other wait dependencies, these two locks cannot form an ABBA cycle. The cost is that the two counters are no longer updated as one atomic whole—an observer requiring them to remain in a consistent relation would see a “half-updated” intermediate state.
Three variants compared:
| Variant | Locks held at once | Deadlock possible? | Appropriate when |
|---|---|---|---|
| Two sequential single-lock loops | At most 1 | These two locks cannot form ABBA (given no other wait dependencies) | The two pieces of data are independent and need no joint consistency |
Nested lock_guard in opposite orders (Example 6) | 2 | Yes (ABBA wait cycle) | A bad example—do not use |
std::lock + defer_lock (Example 7) or scoped_lock (Example 9) | 2 | Avoids deadlock in this acquisition; does not rule out external wait cycles | Several pieces of data must be protected together and no global order can be agreed upon |
The rule can be distilled thus: independent single-lock loops like this one cannot form ABBA, but “holding one lock at a time” does not make an entire program deadlock-free: waiting on a task while holding a lock, recursively locking a non-recursive mutex, or exhausting a thread pool’s resources can all stall progress. When one order can be standardized globally, simply agreeing on it suffices; there is no need to reach for std::lock.
5.2 Associate First, Then Acquire Together: std::lock
Correct snippet (inside a function, <mutex> included; a and b are two distinct mutexes, neither held by the caller):
| |
Example 7: Two threads associating two locks in opposite orders.
| |
The output is 20000 20000. The two threads associate the locks in opposite orders: if each called lock() directly, the ABBA deadlock of §5.1 could result; given that only these two locks are involved and no other wait dependencies exist, letting std::lock acquire them together avoids it. Example 9’s std::scoped_lock expresses the same semantics more concisely and is preferred in new code; return to this example’s unique_lock form only when deferred locking, early unlock, or a condition variable is needed.

Figure 5-2: Locking and unlocking in one iteration of Example 7; this is only one implementation and interleaving permitted by the standard.
Read the figure in four moments: ① Association—defer_lock merely makes the guard remember the mutex; ② Acquisition—std::lock tries uniformly, releasing and retrying where necessary; ③ Update—only after a successful return are the two counters modified inside the double-lock critical section; ④ Release—as each round’s scope ends, the two guards are destroyed and unlock separately.
Association order and locking order are two different things
unique_lock(m, defer_lock)locks nothing by itself; even withoutstd::lockyou could decide the order later with memberlock(). Only the directly locking constructor form locks at construction.std::lockprescribes no internal acquisition order and is not a single atomic acquisition; on exception it releases the locks already taken. The “try—release—retry” drawn in the figure must not be read as the only implementation. See the multi-lock algorithm specification.
std::lock avoids deadlock within this acquisition but promises neither fairness nor completion within a bounded time for every thread; waiting on tasks that depend on those locks while holding them still creates an external wait cycle. If deferred or early unlocking is unnecessary, scoped_lock in §5.4 is the more concise choice.
5.3 A Double-Lock Critical Section: Checking and Paying Must Take Effect Together
Example 7 showed “how to take two locks safely”; the next example answers “what to protect once you have them.” Consider a classic funds scenario: several payment threads transfer money from account A to account B at once, and concurrency must not cause overdrafts, a drifting total, or deadlock.
Wrong example—do not copy it (snippet; from and to are two accounts, and lock/unlock denote manual locking and unlocking of each account’s mutex):
| |
Example 8: Concurrent payment simulation, account A paying account B.
| |
The output is paid=33 failed=7 A=10 B=990. Of 40 requests at 30 yuan each, 1000 yuan covers only 33 payments, so the final state is A=10, B=990 regardless of interleaving. That determinism rests on three constraints:
- No overdraft: the check and the debit share one double-lock critical section, so no other payment can overturn the balance decision.
- Conserved total: the debit and credit commit together; observers following the double-lock protocol never see an intermediate state.
- Isolated statistics: each thread writes only its own
paid/failedslot, and the main thread aggregates them after join.
std::lock is responsible for avoiding deadlock in this double-lock acquisition; the critical section is responsible for maintaining the funds rule. The two duties are distinct. Real payments would also need amount-overflow checks, persistence, and rollback on failure; this example studies the in-memory concurrency protocol only.

Figure 5-3: How a stale decision arises in concurrent payments, and how to prevent it (initial balances A=50, B=0, two threads each paying 30).
The left diagram separates the check from the debit: both threads read A=50 and each subtracts 30, leaving A=−10; each access is locked, yet the earlier decision still goes stale. The right diagram puts the check, debit, and credit into one double-lock critical section: once the first payment completes A=20, and the second rechecks and is rejected. This small 50-yuan counterexample obeys the same rule as the 1000-yuan batch verification in Example 8.
Reading the total of two accounts must likewise hold both locks at once with std::scoped_lock. Locking A to read its balance, unlocking, then locking B to read its balance may involve no data race, yet it can splice together values from before and after a transfer and produce a “snapshot” that never existed. The claim that “any observer holding both account locks sees a conserved total” refers to observers following the same double-lock protocol.
5.4 Simplifying Bidirectional Transfers with scoped_lock
Example 9: Protecting both accounts of a transfer in one step.
| |
The output is 110 90. The fixed inputs ensure the balance requirement holds for either transfer order, with no integer overflow. If you extend this into a real account system, define amount limits, failure results, and persistence rules as well.
scoped_lock prevents deadlock in this multi-lock acquisition but cannot repair every wait cycle in the program. For instance, a thread holding an account lock may wait on another task that itself wants an account lock, and that still deadlocks.
5.5 Livelock and Starvation
- Livelock: threads keep retrying or yielding to one another, looking busy while no business completes. Two threads might release their locks and retry in lockstep, only to collide again.
- Starvation: the system makes progress overall, yet one thread goes without a lock or an execution opportunity for a long time. Standard mutexes promise no fair queueing.
- Deadlock: the participants wait on one another and cannot proceed.
Wrapping try_lock() in a loop is no universal fix; busy-waiting can intensify contention. Simplify resource dependencies first. Where fairness, timeouts, or queue ordering matter, build them into the design instead of assuming the scheduler will look after every thread.
6. Condition Variables: Waiting for State, Not for Notifications
6.1 Why a Lock Alone Is Not Enough: The Need to Wait
A lock answers “who may access the data right now”; a condition variable answers “how to give up execution resources temporarily when the condition is not met.” A consumer finding the queue empty should not keep the lock while checking repeatedly: the producer needs that same lock to put data in.
A correct condition-variable protocol usually has three parts:
- Shared state: the value of
ready, say, or the data held in a queue. - Shared predicate: an expression deciding whether progress is possible, such as
readyorclosed || !queue.empty(). - One mutex: guarding both state modification and predicate checks, and paired with the wait operation.
First, what a predicate is: it answers “can we go on now?” A predicate is nothing mysterious—just a test returning bool: true means “the condition holds, carry on,” false means “not yet, wait.” With a condition variable the predicate is normally a lambda that reads shared state, not a new kind of data type.
Think of waiting for a meal at a restaurant: ready or the queue contents are the status on the screen, “is my order ready?” is the predicate, and notify_one() merely reminds the customer to look at the screen again. The reminder does not make the food ready; the predicate still decides whether it can be collected.
| |
This line first checks the predicate while holding lock. If it is false, the thread releases the lock and waits; after waking and reacquiring the lock, it checks again. wait returns only once the predicate is true. Since the predicate may run several times, keep it simple—normally just reading state, never modifying business data.
Another predicate common with queues:
| |
It means “stop waiting either because the queue is closed or because it contains data.” A true result here does not necessarily mean data is available: if the queue is closed and empty, the right move is to exit rather than take data.
Example 10: The main thread hands one piece of data to a worker.
| |
The diagram below shows one possible ordering: the consumer enters its wait first, and the main thread publishes the data afterward.
sequenceDiagram
autonumber
participant C as Consumer thread
participant M as Main thread (producer)
Note over C,M: The consumer waiting first is one possible ordering, not a fixed schedule
C->>C: unique_lock constructed, locking mutex
C->>C: First predicate check, ready == false
C->>C: wait atomically unlocks mutex and starts waiting
Note right of C: No mutex held while waiting
so the main thread can enter the critical section
M->>M: lock_guard constructed, acquiring mutex
M->>M: payload = 21
M->>M: ready = true
M->>M: Scope exit, unlocking mutex
M->>C: notify_one() wakes one waiter
M->>M: Calls join(), waiting for the consumer to end
C->>C: Wakes on the notification and reacquires mutex
C->>C: Rechecks the predicate, ready == true
Note right of C: wait returns still holding the lock
waking does not mean the condition holds; recheck the predicate
C->>C: result = payload * 2, yielding 42
C->>C: Leaves the lambda, unique_lock unlocks automatically
C-->>M: Consumer thread ends, join() returns
M->>M: Reads result, asserts, and prints 42
The key point is this: notify_one() only reminds a waiter to wake; what really decides whether it can proceed is the predicate check after reacquiring the lock. If the main thread publishes first, the consumer’s very first check of ready yields true and it never waits; even a notification with no waiter at the time does no harm.
The output is 42. Do not destroy cv, the lock, or the data before the thread ends; the main thread reads result after join. In this example the worker waits while the main thread produces, so there is no need for a separate producer thread.
Condition-variable interface at a glance (protocol details unfold in §6.2–§6.4):
| Interface | What it does | Caveats |
|---|---|---|
wait(lock) | Atomically “unlocks and waits”; returns after waking and reacquiring the lock | Must be paired with a loop or a predicate recheck; unsafe on its own (§6.2, §6.3) |
wait(lock, pred) | Equivalent to while (!pred()) wait(lock); | The recommended form; the predicate holds on return |
wait_for / wait_until | Waiting with a timeout; the predicate overload returns whether the predicate ultimately holds | A timeout does not mean data is available (§6.4) |
notify_one() | Wakes one waiter | With no waiter, this notification is simply lost; it does not accumulate |
notify_all() | Wakes all waiters | Suited to shutdown and global state changes |
native_handle() | Platform-native handle | For platform-specific APIs, at the cost of portability |
std::condition_variable can only wait with std::unique_lock<std::mutex>, and threads waiting simultaneously must use the same mutex; if you need another lock type or interruptible waits, use the more general std::condition_variable_any instead (Part II, §9.2). Condition variables are non-copyable; destroying one requires that all waiting threads have been notified (a thread already notified and waiting to reacquire the lock does not count as still blocked in the wait) and that no new waits will follow.
6.2 What wait Does
A predicate wait is equivalent to the following correct snippet, where lock is a unique_lock already holding its mutex:
| |
wait(lock) performs “release the lock and start waiting” as a single atomic step; after waking it reacquires the lock before returning. It needs a unique_lock because waiting must temporarily release and reacquire the lock.
The predicate version checks the condition up front. If the producer finished long ago, the consumer simply carries on and never needs to receive that notification.
Core principle State holds the fact; a notification only prompts a recheck.
| |
The same lock prevents the producer from slipping into the dangerous gap between “the consumer has decided false” and “it has actually started waiting.” Threads waiting on one condition variable must use one and the same mutex. The visibility established by unlocking also lets the consumer read payload safely.
6.3 Lost Wakeups and Spurious Wakeups
Wrong example—do not copy it (snippet; ready is atomic here, deliberately ruling out a data race on an ordinary variable):
| |
The producer may notify just after the consumer’s check but before it starts waiting; notifications are not saved as “tickets for the future,” so the consumer may wait forever. Even with no data race in play, the waiting protocol is wrong here. If ready were an unprotected ordinary bool, a data race would be added on top. The fix is Example 10’s same-lock predicate protocol.

Figure 6-1: On the left, the wrong timing of a lost wakeup—the notification falls into “the gap between check and wait”; on the right, the correct timing under the same-lock predicate protocol—one lock eliminates that gap.
A spurious wakeup ends a wait even though no matching business notification took place. Even in the absence of spurious wakeups, another consumer might take the data first. A single if, or a wait() with no check, cannot prove the condition holds; you must loop and recheck.
notify_one() wakes one waiter and suits the arrival of one more consumable task under the same wait condition; notify_all() wakes all waiters and suits shutdown or a global state change. When several distinct predicates share one condition variable, notify_one() may pick a thread that cannot proceed—split the condition variables or use an appropriate all-wake notification.
Notifications may be sent inside or outside the lock; the core requirements are a correct state protocol and correct object lifetimes. If objects could be destroyed before an out-of-lock notification, additional lifetime constraints apply. A notification is not automatic protection for arbitrary shared data. Interface semantics are in the condition-variable wait specification.
6.4 Timeouts, Exit, and Shutdown
The predicate overload of wait_for reports whether the predicate ultimately holds, so do not read “woke up” as “data available.” In a hand-written loop, waiting the full duration again each time can stretch the total wait without bound; compute one absolute deadline from steady_clock and call wait_until repeatedly, or simply use the predicate overload. A timeout is no real-time return guarantee: scheduling delays and reacquiring the mutex can both push the actual return later.
Queues usually use closed || !queue.empty(): on waking, take data if there is any; exit only when “closed and empty.” Set closed under the lock first, then notify the waiters, and finally reap the threads and destroy the queue. That is clearer than sending a “special number” indistinguishable from business data. The task processor in Part II, §11 implements this protocol.
A bounded queue additionally needs producers to wait for capacity: the producer predicate is closed || size < capacity, the consumer predicate closed || !empty. Protect the queue and the closed flag with one mutex, and typically use two condition variables—“not full” and “not empty.” Notify producers after a successful removal, consumers after a successful insertion, and both groups on shutdown. After closing, producers already blocked must wake and reject their enqueues; consumers exit once the queue is drained. A zero capacity should be rejected at construction. Before destruction the caller must close and reap every thread that accesses the queue.
7. From Threads to Tasks: How Results Come Back
7.1 Threads, Tasks, and Shared State
§3.4 used async for one square computation; this chapter explains the result channel behind it and extends it to explicit delivery and queue-based scheduling. First separate “who schedules execution” from “who stores the result,” and only then choose an interface.
A thread is an execution resource, a task is the work to be done, and a future is the handle for obtaining that work’s result. One thread can process many tasks; holding a future does not imply that a thread is already executing the task.
A future<T> connects to the producing end through an asynchronous shared state. Do not confuse this with “sharing business variables at will”: the state is managed by the standard library and holds the result, any exception, and readiness information.

Figure 7-1: An asynchronous result channel. The producing end writes a value or exception into the shared state and makes it ready; the consuming end waits and reads through the future.
Having a state and having a ready state are two different things. A default-constructed future has no associated state; a valid future may still be awaiting its result.
| Operation | Waits? | Consumes the result? |
|---|---|---|
valid() | Does not wait for the computation | Only tells whether a shared state is associated; says nothing about success or completion |
wait() | Waits if not ready; a deferred task may run here | Does not take the value and does not rethrow a stored task exception |
wait_for() / wait_until() | Checks the state within the given wait range | Returns ready, timeout, or deferred |
future<T>::get() | Waits if necessary | Takes the value or rethrows the exception, releasing this handle’s association with the state |
shared_future<T>::get() | Waits if necessary | May be read repeatedly; for an ordinary T it returns const T& |
An ordinary future is move-only; after a successful share() the original loses its state. Calling get() or a wait function on a stateless future violates the interface contract, and you cannot count on the implementation throwing a friendly future_error. Make ownership explicit up front, avoid consuming twice, and never let one thread move or consume a handle while another is still accessing it.
Synchronization exists between making a state ready and successfully detecting readiness, so once the result is retrieved the published data can be used safely; returning a reference or pointer still requires the referred-to object to be alive—a future does not extend external objects’ lifetimes. See asynchronous shared state and future consumption semantics.
7.2 promise: Explicitly Delivering a Value or Exception
A promise<T> is the writing end: normally you first call get_future() to obtain the sole reading end, then move the promise to the thread that will perform the work. A state can be satisfied only once; taking the future twice or setting the result twice yields future_error.
promise at a glance (promise<T&> and promise<void> are specializations with corresponding semantics):
| Interface | What it does | Caveats |
|---|---|---|
get_future() | Obtains the sole reading end associated with this state | Succeeds once per state; usually called before the promise is moved (the moved-from source no longer owns the state, while the new owner can still obtain it) |
set_value(v) | Writes the result value into the shared state and makes it ready | The state becomes ready at the call, without waiting for the thread function to return; promise<void> uses the no-argument set_value() |
set_exception(ep) | Writes an exception pointer into the shared state and makes it ready | The waiter rethrows it at get(); normally paired with std::current_exception() |
set_value_at_thread_exit(v) / set_exception_at_thread_exit(ep) | Writes the result but delays readiness until thread exit | Readiness comes later: the waiter also waits for the thread function to return and for thread-local objects to be destroyed |
| Destructor | If the state was never satisfied, stores broken_promise and makes it ready | An abandoned, unsatisfied state becomes ready with an exception (third path in Example 11); a promise that stays alive but never delivers may keep a waiter waiting forever |
Example 11: A normal result, a business exception, and a broken promise.
| |
set_value() normally makes the state ready at the call and does not wait for the thread function to return. After the consumer reads the result, the producing thread may still be doing cleanup; if you need proof that the whole thread has ended, join it regardless. Interfaces such as set_value_at_thread_exit() delay readiness; this article does not go into them.
Destroying an unsatisfied promise gives the consumer broken_promise rather than guaranteeing it waits forever. If the promise stays alive, however, with no path either setting a result or destroying it, the consumer may indeed wait forever. A promise protocol error and the task’s business exception are different sources of error. See the promise specification.
7.3 packaged_task: Bundling a Callable with Its Result Channel
A packaged_task stores a callable and, when the task is invoked, automatically deposits its return value or any thrown exception into the shared state. It creates no thread of its own. It suits a design that wraps a task first and hands it to a worker later.
Key interface points:
- The template argument is a function signature—
packaged_task<int(int)>means “takes an int, yields an int”—and the constructor accepts any callable matching that signature. get_future()succeeds only once per shared state and only on an object that still owns that state. Usually take the reading end before the task is moved away (the moved-from source has no state and throwsfuture_errorif called; the new owner that took over the task can still obtain it).operator()(args...)runs the task and delivers the result; each shared state can be satisfied only once. An exception from the target also consumes that state’s delivery opportunity, and invoking an already-run task again throwsfuture_error. To reuse the object, callreset()first: it abandons the old shared state and establishes a new one—a previously obtained future does not become invalid: if the old task ran, that future still holds the original result or exception; if the old task never ran, abandoning the state makes it receivebroken_promise. You must callget_future()again against the new state to obtain a new reading end.- Move-only and non-copyable; this is why Example 12 hands the task over to a thread with
std::move.
The specification is in the C++20 draft’s packaged_task.
Example 12: Moving a task into a thread.
| |
The output is 81. After the move, the original task no longer owns that state. Destroying a valid packaged_task without running it likewise abandons the state, and the consumer may receive broken_promise. As a rule, treat a task as a one-shot object; repeated invocation and state resetting complicate the protocol, so do not use it casually like an ordinary function that can be called repeatedly.
7.4 async: Simple, but Choose the Launch Policy
Example 4 already demonstrated return values and exceptions; Example 13 adds only the merging of two tasks’ results and the trigger point for deferred execution, so as not to repeat earlier material.
| Policy | Behavior |
|---|---|
std::launch::async | Executes with new-thread-of-execution semantics and returns a future; creating the resource may fail |
std::launch::deferred | Stores the task; the first non-timed wait (such as get() or wait()) runs it on the calling thread |
| No policy passed explicitly | Equivalent to allowing either async or deferred, with the implementation choosing; no parallel execution is guaranteed |
Example 13: Concurrent results and deferred execution.
| |
The output is 42 7. The work here is tiny and serves only to demonstrate behavior; it does not suggest that two threads beat direct addition. Futures have no standard cancellation interface; a timeout merely ends this wait, without canceling or failing the task.
Wrong example—do not copy it (snippet; work_a and work_b are existing functions):
| |
The temporary future on the first line is destroyed at the semicolon and may wait there for the task to finish, so tasks intended to overlap end up executing in sequence. Normally this is not undefined behavior but an implicit wait at destruction. The fix is to save both handles as in Example 13 and then retrieve the results explicitly.
7.5 Does Destruction Wait or Not?
You must decide based on the state’s origin, not merely on the type name future:
- A state created by promise / packaged_task: the future’s destructor releases the association and normally does not wait for the producing thread; that thread’s lifetime is managed separately.
- A
std::asyncstate that selected the async policy: if it is unfinished when the last reference is released, that release may wait for the associated thread to finish. After a move to another owner or a conversion to shared_future, the last releaser may carry the wait as well. - A deferred state: if no non-timed wait ever triggered it, destroying it does not run the task simply to “get the work done.”
Destruction and locks Do not destroy the last handle that may wait while holding a lock the task needs, and never treat a future’s destructor as active cancellation.
Neither wait() nor destruction throws a task’s saved exception to the caller; to observe it you need get(). An unretrieved exception may be discarded along with the state. Launch policies and wait rules are in the async specification.
7.6 shared_future: Reading One Result from Several Places
It is copyable, with each consuming thread holding its own copy of the handle. For an ordinary T, get() returns const T& into the shared state, so at least one related handle must keep the state alive; copy the result if you need to retain it long term. T& and void have their own return semantics.
Example 14: Supplying the same configuration to two threads.
| |
The output is 5 5. Both threads read the same string read-only and write different array elements; the main thread reads them after reaping. A shared_future guarantees the mechanism for reading the shared state, not the thread safety of mutable objects pointed to from inside the result; sharing a shared_ptr<Mutable> result does not remove the need to synchronize Mutable.
Part I ends here. Part II starts with atomic variables and memory ordering, continues with C++20’s cancellation and coordination facilities and engineering practices around performance and testing, and combines the locks, condition variables, and futures above into a shut-downable concurrent task processor.
