Your country

Tools that support it use your country for local currency, number formats, units and paper size. Your choice is saved only in this browser.

Type a name or a two-letter code. Use the up and down arrow keys to move through the countries, Enter to choose one and Escape to close.

C++  Module 16 – Concurrency: threads, atomics and the memory model

Threads with std::thread and std::jthread

Start C++ threads with std::thread and std::jthread, pass arguments safely, wait with join(), and stop a worker cooperatively with std::stop_token.

  • Intermediate
  • 25 minutes
  • Examples run with Clang 21.0.0, GCC 16.2.0 and Python 3.14.8
  • By MySmartCoPilot
Free lesson, Pro answers: 25 minutes
Free: the whole lesson. With a pass: the model answers to its 2 interview questions.

What you will learn

  • Start threads with std::thread and wait for them with join()
  • Split a job across threads that each write only their own results
  • Explain why destroying a joinable std::thread ends the program
  • Pass arguments to a thread by value, with std::ref or by moving them
  • Cancel a std::jthread cooperatively through its std::stop_token

Before you start

What you’ll learn · Before you start

What you will learn

  • Start threads with std::thread and wait for them with join()
  • Split a job across threads that each write only their own results
  • Explain why destroying a joinable std::thread ends the program
  • Pass arguments to a thread by value, with std::ref or by moving them
  • Cancel a std::jthread cooperatively through its std::stop_token

Before you start

On this page

On the 18-core computer that ran this lesson’s examples, counting the 664,579 primes below 10,000,000 took more than a second with one thread and finished more than ten times sooner with 18. A thread is a separate line of execution inside your program: it runs a function at the same time as main and any other threads, and it shares the program’s memory with them. In C++, std::thread starts one. Writing std::thread t(f, x) begins running f(x) at once, and t.join() waits until that call has returned.

std::jthread, new in C++20, does the same, but it joins by itself when it goes out of scope, and it can ask its thread to stop through a std::stop_token: A handle through which a thread can see whether a stop has been requested: its stop_requested() turns true once request_stop() is called on the matching std::jthread or std::stop_source. It never interrupts the thread; the thread checks it.. Prefer std::jthread in new code; std::thread is what older code and C++17 compilers have. This lesson splits a sum across four threads, shows what happens when a thread is never joined, passes arguments without dangling references, stops a worker cleanly, and measures how much 18 cores speed a job up.

Four threads split one sum, and join() waits for each

The program below adds up the numbers 1 to 10,000,000. Instead of one loop, it starts four threads, and each one sums a quarter of the vector:

sum_threads.cpp C++ · sum_threads.cpp
// Adds up the numbers 1 to 10,000,000 with four threads. Each thread sums one quarter of the vector into its own
// element of `partial`, so no two threads ever write the same object; main adds the four results after join().
#include <cstddef>
#include <cstdint>
#include <numeric>
#include <print>
#include <thread>
#include <vector>

int main() {
    std::vector<std::int64_t> numbers(10'000'000);
    std::iota(numbers.begin(), numbers.end(), 1);  // 1, 2, 3, ..., 10,000,000

    constexpr std::size_t n_threads = 4;
    std::vector<std::int64_t> partial(n_threads);  // one result slot per thread
    std::vector<std::thread> threads;

    for (std::size_t t = 0; t < n_threads; ++t) {
        const std::size_t first = numbers.size() * t / n_threads;
        const std::size_t last = numbers.size() * (t + 1) / n_threads;
        // numbers and partial are captured by reference: both outlive the threads, because main joins them below.
        threads.emplace_back([&numbers, &partial, t, first, last] {
            std::int64_t sum = 0;
            for (std::size_t i = first; i < last; ++i) sum += numbers[i];
            partial[t] = sum;  // the only write, to this thread's own slot
        });
    }
    for (std::thread& th : threads) th.join();  // wait until all four have finished

    for (std::size_t t = 0; t < n_threads; ++t) std::println("thread {} summed {}", t, partial[t]);
    const std::int64_t total = std::accumulate(partial.begin(), partial.end(), std::int64_t{0});
    std::println("total    {}", total);
    std::println("formula  {}", std::int64_t{10'000'000} * 10'000'001 / 2);  // n(n + 1) / 2
}
main fills a vector, starts four threads that each sum a quarter into their own partial slot, then joins them and adds the four results.main: fill numberswith 1 … 10,000,000four threads run at the same timemain: join() each thread,then add partial[0 … 3]thread 0sums the 1st quarterwrites partial[0]thread 1sums the 2nd quarterwrites partial[1]thread 2sums the 3rd quarterwrites partial[2]thread 3sums the 4th quarterwrites partial[3]start 4 threadseach join() waitsfor one thread

One job split across four threads, then joined

Text description of the diagram

The diagram reads from top to bottom.

  • At the top, main fills the vector numbers with 1 to 10,000,000.
  • An arrow labelled "start 4 threads" leads to a box of four threads that run at the same time, two per row. Thread 0 sums the first quarter of the vector and writes partial[0]; thread 1 sums the second quarter and writes partial[1]; thread 2 the third quarter, into partial[2]; thread 3 the fourth quarter, into partial[3].
  • An arrow labelled "each join() waits for one thread" leads to the bottom box: main joins each thread and then adds partial[0] to partial[3].

Built with each compiler, it prints the same four partial sums and the same total, which matches the formula n(n + 1) / 2:

Four threads, two compilers Python · sum_session.py
from session import run

# sum_threads.cpp built and run with Apple clang, then with GCC 16.
run("""
clang++ -std=c++23 -Wall -Wextra -fsanitize=address,undefined sum_threads.cpp -o sum_threads && ./sum_threads
g++-16 -std=c++23 -Wall -Wextra -fsanitize=address,undefined sum_threads.cpp -o sum_threads-gcc && ./sum_threads-gcc
""")

Output

$ clang++ -std=c++23 -Wall -Wextra -fsanitize=address,undefined sum_threads.cpp -o sum_threads && ./sum_threads
thread 0 summed 3125001250000
thread 1 summed 9375001250000
thread 2 summed 15625001250000
thread 3 summed 21875001250000
total    50000005000000
formula  50000005000000
$ g++-16 -std=c++23 -Wall -Wextra -fsanitize=address,undefined sum_threads.cpp -o sum_threads-gcc && ./sum_threads-gcc
thread 0 summed 3125001250000
thread 1 summed 9375001250000
thread 2 summed 15625001250000
thread 3 summed 21875001250000
total    50000005000000
formula  50000005000000

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 sum_session.py

Step through the same run one event at a time: when each thread starts, what it writes, and what each join() waits for. The sums in the figure are the ones the program printed.

Interactive · message by message Four threads sum one vector, then main joins them Step through sum_threads.cpp. Each arrow is one event of the run, in time order; the sums are the ones the program printed.
Four threads sum one vector, then main joins them, step 9 of 9. Messages so far: main to thread 0: start: numbers 1 to 2,500,000; main to threads 1 to 3: start: the other quarters; main to thread 0: join(): wait for the end; thread 0 to main: ended: partial[0] visible; threads 1 to 3 to main: ended: partial[1..3] visible.maintotal 50000005000000thread 0partial[0] = 3125001250000threads 1 to 3partial[1], [2], [3] writtenstart: numbers 1 to 2,500,000start: the other quartersjoin(): wait for the endended: partial[0] visibleended: partial[1..3] visible

The drawing shows the whole scene; its 9 steps are listed below.

Every step as text
  1. main fills the vector. main fills numbers with 1 to 10,000,000 and makes partial, four slots of 0, one for each thread. No other thread exists yet.
  2. Thread 0 starts at once. emplace_back constructs the first std::thread, and thread 0 starts running at once: it adds numbers[0] to numbers[2,499,999]. main does not wait; it goes on to the next pass of its loop.
  3. Three more start. Threads 1, 2 and 3 start the same way, each with its own quarter. All four now run at the same time, and the operating system decides when each one gets a processor.
  4. Each writes its own slot. Thread 0 finishes its quarter and writes 3,125,001,250,000 into partial[0], its only write. It only reads numbers, which nobody writes now, so there is nothing to protect.
  5. The others write theirs. Threads 1 to 3 write 9,375,001,250,000, 15,625,001,250,000 and 21,875,001,250,000 into partial[1], partial[2] and partial[3]. No two threads ever write the same object.
  6. join() waits. main calls join() on thread 0. join() returns only when thread 0 has finished, however long that takes, so main waits here if thread 0 is still adding.
  7. Thread 0 has ended. Thread 0 has ended, and join() returns. The standard says a thread's completion synchronizes with that return: main now sees partial[0] exactly as thread 0 wrote it.
  8. Three more joins. main joins threads 1, 2 and 3 in turn, and each join() makes that thread's slot visible. A join() on a thread that has already finished returns at once.
  9. main adds the results. Only now does main read partial. The total is 50,000,005,000,000, the same as the formula n(n + 1) / 2. Read before the joins, partial could have held slots that were not written yet.

Three things make this program correct:

  • The threads never share a write. Each thread writes one element of partial, its own, and only reads numbers. Two threads that read the same object at the same time are fine; two that touch the same object while one of them writes need protection, which is the subject of the next lesson.
  • join() comes before the results are read. join() waits until the thread has finished, and everything the thread wrote is then visible to the thread that joined it: the standard says the thread’s completion synchronizes with: A relation of the C++ memory model between an operation in one thread and an operation in another: everything the first thread did before its operation is visible to the second thread after its own. A thread's end synchronizes with the return of the join() that waits for it. the return from join(). Reading partial before the joins would read values that may not be there yet.
  • The references stay valid. The lambda captures numbers and partial by reference, which is safe only because both outlive every thread: main joins all four before either vector is destroyed.

The threads start running as soon as each std::thread is constructed, and the operating system decides when each one gets a processor. The output is still in a fixed order, because main prints only after all four have finished.

Key idea

Give every thread its own data to write, and read the results only after join(): then four threads that share one vector need no lock at all.

Checkpoint

Check: Start, split and join

  1. Question 1 of 2 sum_threads.cpp prints partial[0] to partial[3] after its loop of join() calls. What could go wrong if it printed them before that loop?

    Choose one answer.

    A hint

    Which call tells main that a thread has finished writing?

    Show the answer: Question 1 of 2

    Answer: It could print slots that are still 0 or still being written, because nothing yet orders the threads' writes before main's reads

    join() is how main learns that a thread has finished, and the standard makes a thread's completion synchronize with the return from join(). A read of partial before that point races with the thread's write, which is a data race, the subject of the next lesson.

  2. Question 2 of 2 Why do the four threads of sum_threads.cpp need no lock?

    Choose one answer.

    Show the answer: Question 2 of 2

    Answer: Each thread writes only its own element of partial, and numbers is only read

    Threads that only read shared data, and write only data of their own, have nothing to protect. That is why the program splits the work into four slices and gives each thread one slot of partial.

A joinable thread must not be destroyed

A std::thread that represents a running (or finished but not yet joined) thread is joinable: Said of a std::thread or std::jthread object that still represents a thread of execution: from the moment it starts the thread until join() or detach() is called, even after the thread's function has returned. Destroying a joinable std::thread calls std::terminate().. Before the object is destroyed, you must either join() it, which waits, or detach() it, which lets the thread run on with nobody waiting for it. Forget both, and the destructor calls std::terminate: The function C++ calls when exception handling cannot go on, for example when no handler matches a throw or an exception tries to leave a noexcept function. By default it calls std::abort, which ends the program with SIGABRT. More in “C++ exceptions: throw, catch and stack unwinding”, which ends the whole program:

forgot_join.cpp C++ · forgot_join.cpp
// A std::thread that is still joinable when it is destroyed ends the whole program.
#include <chrono>
#include <print>
#include <thread>

int main() {
    std::thread worker([] {
        std::this_thread::sleep_for(std::chrono::seconds(1));  // still running when main ends
        std::println("worker: done");
    });
    std::println("main: leaving without join() or detach()");
}  // ~thread() finds worker joinable and calls std::terminate()
std::terminate() with libc++ and with libstdc++ Python · terminate_session.py
from session import run

# forgot_join.cpp ends in std::terminate() with each compiler's library: libc++, then libstdc++.
run("""
clang++ -std=c++23 -Wall -Wextra forgot_join.cpp -o forgot_join && ./forgot_join
g++-16 -std=c++23 -Wall -Wextra forgot_join.cpp -o forgot_join-gcc && ./forgot_join-gcc
""")

Output

$ clang++ -std=c++23 -Wall -Wextra forgot_join.cpp -o forgot_join && ./forgot_join
main: leaving without join() or detach()
libc++abi: terminating
Abort trap: 6
[exit status 134]
$ g++-16 -std=c++23 -Wall -Wextra forgot_join.cpp -o forgot_join-gcc && ./forgot_join-gcc
main: leaving without join() or detach()
terminate called without an active exception
Abort trap: 6
[exit status 134]

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 terminate_session.py

Each library says it in its own words, “libc++abi: terminating” and “terminate called without an active exception”, and both end with abort(), which the shell reports as exit status: The number a program hands back when it ends, the value main returns. 0 means success; a shell keeps the last status in $?, and && runs the next command only after a status of 0. More in “How C++ compiles: preprocess, compile and link” 134. The worker never printed its line: the program ended while it slept. The standard chose to terminate on purpose. Its note on the destructor explains that an automatic detach() would cause correctness bugs and an automatic join() performance bugs, and both would show up only when an exception skips the code that was meant to join.

That last point is the everyday danger. Any code between starting a thread and joining it can throw:

  • if std::thread t(work); parse(input); t.join(); and parse throws, t is destroyed while still joinable, and the exception ends the program instead of reaching its handler;
  • wrapping the code in try and calling join() in every path works, but it is easy to forget one path.

The C++ Core Guidelines (CP.25) recommend a thread type that joins in its destructor, and C++20 added one to the standard library: std::jthread.

Follow one std::thread object from its constructor to its destructor. Two of its paths end the program, and both start with an object that is still joinable:

Interactive · step by step The life of a std::thread object, and the step that ends the program Step from the constructor to the destructor. The left column is the safe path; the arrows to the right are what forgot_join.cpp did.
The life of a std::thread object, and the step that ends the program, step 6 of 6. Showing std::thread worker(f), f runs, f returns, join() or detach(), ~thread(): nothing to do, std::terminate(). In focus: ~thread(): nothing to do.destroyeddestroyedstd::thread worker(f)the thread starts at oncef runsjoinable() is truef returnsworker is still joinablejoin() or detach()joinable() becomes false~thread(): nothing to dothe program goes onstd::terminate()exit status 134

The drawing shows the whole scene; its 6 steps are listed below.

Every step as text
  1. The thread starts. forgot_join.cpp constructs worker with a lambda that sleeps for one second. The new thread starts running the lambda at once, before the constructor returns.
  2. worker is joinable. While the lambda runs, worker represents a thread of execution, so worker.joinable() is true. main prints "main: leaving without join() or detach()".
  3. Destroyed while joinable. main reaches its closing brace with worker still joinable. ~thread() calls std::terminate(): the program aborts with exit status 134, and the worker never prints its line.
  4. Returning changes nothing. Had the lambda returned first, nothing would change: a thread whose function has returned stays joinable until someone calls join() or detach(), and destroying it then terminates too.
  5. join() or detach(). join() waits for the thread to end, and detach() lets it run on with nobody waiting. Either way joinable() becomes false: the object no longer represents a thread.
  6. A safe destructor. Now ~thread() has nothing to do, and the program goes on. std::jthread does the joining for you: its destructor requests a stop, then joins.

The only safe way through is the column on the left: join() or detach() before the destructor runs. The rest of this section is about why detach() is the poorer of the two.

Why detach() is rarely the answer

detach() does avoid std::terminate(), but it trades one problem for others. Nobody can wait for a detached thread, so nobody knows when it is finished. When main returns, the process ends and every detached thread stops wherever it is, even in the middle of writing a file. And a detached thread that refers to a local variable can easily outlive that variable:

dangling_detach.cpp C++ · dangling_detach.cpp
// A detached thread that keeps a reference to a local variable outlives that variable.
#include <chrono>
#include <print>
#include <string>
#include <thread>

using namespace std::chrono_literals;

void start_logger() {
    std::string channel = "orders";
    std::thread logger([&channel] {     // captures a reference to start_logger()'s local string
        std::this_thread::sleep_for(100ms);
        std::println("logging to {}", channel);  // by now start_logger() has returned
    });
    logger.detach();                    // nobody waits for it
}                                       // channel is destroyed here

int main() {
    start_logger();
    std::this_thread::sleep_for(3s);  // keep the process alive long enough for the logger to run
}

The logger thread captures channel by reference, start_logger() returns, and 100 milliseconds later the thread reads a string that no longer exists. That is undefined behaviour: What a program does when it breaks a rule for which the C++ standard sets no requirements, such as reading past the end of an array. The compiler may assume it never happens, so the result can change with the compiler and the optimisation level. More in “Object lifetime and undefined behaviour in C++”: a normal build may print the right word, garbage or nothing. sanitizer: A compiler option, such as -fsanitize=address or -fsanitize=undefined, that builds extra checks into a program, so that memory errors and undefined behaviour are reported, with their source line, while it runs. More in “Finding memory bugs with AddressSanitizer and UBSan” catches it when it watches for stack memory used after its function has returned. Clang’s documentation says that check is on by default only on Linux; -fsanitize-address-use-after-return=always turns it on everywhere:

A detached thread reads a dead local variable Python · detach_session.py
from session import run

# dangling_detach.cpp under AddressSanitizer, with detection of stack memory used after its function returned.
run("""
clang++ -std=c++23 -g -fsanitize=address -fsanitize-address-use-after-return=always dangling_detach.cpp -o dangling_detach
./dangling_detach
""")

Output

$ clang++ -std=c++23 -g -fsanitize=address -fsanitize-address-use-after-return=always dangling_detach.cpp -o dangling_detach
$ ./dangling_detach
==…==ERROR: AddressSanitizer: stack-use-after-return on address 0x…
READ of size 1 at 0x… thread T1
    #11 start_logger()::$_0::operator()() const dangling_detach.cpp:13
Address 0x… is located in stack of thread T0 at offset 55 in frame
    #0 start_logger() dangling_detach.cpp:9
  This frame has 3 object(s):
    [32, 56) 'channel' (line 10) <== Memory access at offset 55 is inside this variable
    [96, 104) 'logger' (line 11)
    [128, 136) 'ref.tmp' (line 11)
SUMMARY: AddressSanitizer: stack-use-after-return dangling_detach.cpp:13:9 in start_logger()::$_0::operator()() const
Thread T1 created by T0 here:
    #4 start_logger() dangling_detach.cpp:11
    #5 main dangling_detach.cpp:19
==…==ABORTING
[58 more lines of the report left out]
[exit status 134]

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 detach_session.py

The report names the variable, 'channel' (line 10), the frame it lived in, start_logger(), and the thread that read it, T1, created at line 11. The sanitizer then stops the program with exit status 134. The Core Guidelines put the lesson shortly (CP.26): don’t detach() a thread.

Step through the run that the report describes, with main, the frame of start_logger() and the logger thread side by side:

Interactive · message by message A detached thread outlives the variable it reads Step through dangling_detach.cpp. The middle column is start_logger() and its local variables; the right one is the logger thread, T1.
A detached thread outlives the variable it reads, step 6 of 6. Messages so far: main to start_logger(): start_logger(); start_logger() to logger thread: start, capture &channel; start_logger() to main: returns; logger thread to start_logger(): read channel: gone.mainthread T0start_logger()returned: channel destroyedlogger threaddetached: nobody waitsstart_logger()start, capture &channelreturnsread channel: gonestack-use-after-return, line 13

The drawing shows the whole scene; its 6 steps are listed below.

Every step as text
  1. A local string. main calls start_logger(). Its local std::string channel holds "orders" and lives until the function's closing brace.
  2. The thread keeps an address. The lambda captures channel by reference, so thread T1 holds the address of a local variable of start_logger(). T1 starts by sleeping for 100 ms.
  3. Nobody waits for T1. logger.detach() lets T1 run on with nobody waiting for it. The std::thread object is no longer joinable, so its destructor will not call std::terminate().
  4. channel is destroyed. start_logger() returns, and channel is destroyed at its closing brace. T1 is still asleep, and it still holds the address of that string.
  5. The process stays alive. main sleeps for 3 seconds, so the process is still running when T1 wakes up. Without that sleep, main would return and the process would end with T1 still asleep.
  6. A read of dead memory. After 100 ms, T1 reads channel, a string that no longer exists: undefined behaviour. AddressSanitizer reports a stack-use-after-return in the frame of start_logger(), names 'channel' (line 10) and stops the program with exit status 134.

The read on the last step is the one the report points at, line 13, about 100 ms after channel was destroyed. Nothing in the code waited for T1, so nothing kept channel alive for it.

Checkpoint

Check: A joinable thread at its destruction · What detach() leaves behind

  1. Question 1 of 3 A thread's function has already returned, and nobody has called join() or detach() on its std::thread. Is the object joinable?

    Choose one answer.

    Show the answer: Question 1 of 3

    Answer: Yes. It stays joinable until join() or detach() is called

    A std::thread is joinable from the moment it starts a thread until join() or detach() is called, even after the thread's function has returned. Only then may it be destroyed without ending the program.

  2. Question 2 of 3 parse() throws std::invalid_argument, and the caller of run() has a matching catch. What happens?

    Read the code, then choose one answer.

    void run(const std::string& input) {
        std::thread t(work);
        parse(input);  // throws
        t.join();
    }
    Show the answer: Question 2 of 3

    Answer: The program ends with std::terminate(), and the handler never runs

    Stack unwinding destroys t on its way out of run(), and ~thread() calls std::terminate() because t is still joinable, so the exception never reaches its handler. Writing std::jthread t(work); fixes it: the destructor then joins, after a stop request that work() may simply ignore.

  3. Question 3 of 3 A program detaches a thread that writes a report to a file, and main returns 10 ms later, while the thread is still writing. What happens to the report?

    Choose one answer.

    Show the answer: Question 3 of 3

    Answer: The process ends, the thread stops wherever it is, and the file may be cut short

    A detached thread has no owner: nothing waits for it, and when main returns the process ends with the thread wherever it was. Work that must finish needs a thread that is joined, for example a std::jthread whose scope ends before main does.

std::jthread joins for you, and can be asked to stop

std::jthread is a std::thread with two additions. Its destructor first asks the thread to stop, then joins it. And if the function you give it takes a std::stop_token as its first parameter, the jthread passes its own token, which the function checks to see whether a stop has been requested:

jthread_scope.cpp C++ · jthread_scope.cpp
// std::jthread joins in its destructor, after asking its thread to stop through the thread's std::stop_token.
#include <chrono>
#include <print>
#include <stop_token>
#include <thread>

using namespace std::chrono_literals;

int main() {
    {
        // A callable whose first parameter is a std::stop_token receives the jthread's token.
        std::jthread worker([](std::stop_token token) {
            while (!token.stop_requested()) {
                std::this_thread::sleep_for(10ms);  // one round of work
            }
            std::println("worker: stop requested, finishing");
        });
        std::this_thread::sleep_for(100ms);
        std::println("main: leaving the block");
    }  // ~jthread(): request_stop(), then join()
    std::println("main: the worker has finished");
    std::println("sizeof(std::thread) = {}, sizeof(std::jthread) = {}", sizeof(std::thread), sizeof(std::jthread));
}

Step through the end of the block: the destructor’s two calls, and the moment the worker notices the first one. The lines it prints are the ones the run in the next section recorded.

Interactive · message by message What std::jthread's destructor does at the closing brace Step through jthread_scope.cpp. The left column is main; the right is the worker the jthread runs.
What std::jthread's destructor does at the closing brace, step 7 of 7. Messages so far: main to worker: start, pass the token; main to worker: request_stop(); worker to main: ended: join() returns.mainpast the closing braceworkerstop_requested(): truestart, pass the tokenrequest_stop()ended: join() returns

The drawing shows the whole scene; its 7 steps are listed below.

Every step as text
  1. The jthread starts. main constructs worker. Its lambda takes a std::stop_token as its first parameter, so the jthread passes its own token, and the thread starts at once.
  2. Rounds of work. The worker checks token.stop_requested(), finds false, sleeps 10 ms for one round of work and checks again, about ten times while main sleeps for 100 ms.
  3. main leaves the block. main prints "main: leaving the block" and reaches the closing brace. The destructor of worker runs, in main.
  4. First, request_stop(). ~jthread() first calls request_stop(), which marks the stop state that the token shares. The worker does not see it yet: it is in the middle of a 10 ms sleep.
  5. Then, join(). Then ~jthread() calls join(), so main waits until the worker's function has returned.
  6. The worker notices. At its next check the worker finds stop_requested() true, prints "worker: stop requested, finishing" and returns.
  7. join() returns. The thread has ended, so join() returns and main prints "main: the worker has finished". Nothing was left running, and nothing called std::terminate().

At the closing brace, the destructor of worker requests a stop and waits. The worker sees the request on its next check, prints its line and returns, and only then does main go on. The two types compare like this:

std::thread or std::jthread?
Criterion std::threadstd::jthread
Standard and header C++11, <thread>C++20, <thread> (stop tokens in <stop_token>)
Destroyed while joinable Calls std::terminate()Calls request_stop(), then join()
Move-assigned over a joinable thread Calls std::terminate()Requests a stop and joins the old thread first
Asking the thread to stop Your own flag, shared with the threadrequest_stop() and the std::stop_token it passes
Size on the computer that ran the examples 8 bytes16 bytes, plus a stop state allocated when it starts a thread
When to choose Code that must still build as C++17Everything else, including threads you would detach

The second and third rows are the reason to switch: a std::jthread that is destroyed or overwritten while it still runs a thread waits for it instead of ending the program. What it cannot do is make the thread stop; that is up to the thread, as the next section shows.

Checkpoint

Check: What std::jthread's destructor does

  1. Question 1 of 2 A std::jthread runs a lambda that takes no std::stop_token and loops until a flag is set that nobody ever sets. What happens when the jthread goes out of scope?

    Choose one answer.

    Show the answer: Question 1 of 2

    Answer: Its destructor requests a stop, which the lambda never sees, then waits in join() for ever

    std::jthread removes the std::terminate(), not the need to stop. Its destructor can only ask: a thread whose function never checks its token keeps the destructor waiting until the function returns by itself.

  2. Question 2 of 2 w = std::jthread(task); runs while w still runs an earlier thread. What happens to that earlier thread?

    Choose one answer.

    Show the answer: Question 2 of 2

    Answer: w requests a stop on it and joins it, then takes over the new thread

    Move-assigning a std::jthread over one that still runs a thread first requests a stop on the old thread and joins it. A std::thread in the same place calls std::terminate().

A stop is a request the thread checks

C++ has no way to kill a thread from the outside, and that is deliberate: a thread stopped at an arbitrary point could be holding a lock or be halfway through updating a data structure, and the rest of the program would inherit the mess. A stop in C++ is a request. The thread decides where it is safe to stop by checking its token:

stop_token.cpp C++ · stop_token.cpp
// Asking a std::jthread to stop: request_stop(), the token's stop_requested() and a std::stop_callback.
#include <chrono>
#include <print>
#include <stop_token>
#include <thread>

using namespace std::chrono_literals;

int main() {
    const std::thread::id main_id = std::this_thread::get_id();

    std::jthread worker([main_id](std::stop_token token) {
        // Runs once, when a stop is requested, in the thread that requests it.
        std::stop_callback on_stop(token, [main_id] {
            std::println("callback: ran on the main thread: {}", std::this_thread::get_id() == main_id);
        });
        int rounds = 0;
        while (!token.stop_requested()) {
            ++rounds;
            std::this_thread::sleep_for(5ms);
        }
        std::println("worker: saw the request after at least one round: {}", rounds >= 1);
    });

    std::this_thread::sleep_for(100ms);
    const bool first = worker.request_stop();   // true: this call made the request
    const bool second = worker.request_stop();  // false: a stop was already requested
    worker.join();
    std::println("main: request_stop() returned {}, then {}", first, second);
    std::println("main: joinable() after join(): {}", worker.joinable());
}
jthread_scope.cpp and stop_token.cpp with both compilers Python · stop_session.py
from session import run

# jthread_scope.cpp and stop_token.cpp, each with both compilers.
run("""
clang++ -std=c++23 -Wall -Wextra -fsanitize=address,undefined jthread_scope.cpp -o jthread_scope && ./jthread_scope
g++-16 -std=c++23 -Wall -Wextra -fsanitize=address,undefined jthread_scope.cpp -o jthread_scope-gcc && ./jthread_scope-gcc
clang++ -std=c++23 -Wall -Wextra -fsanitize=address,undefined stop_token.cpp -o stop_token && ./stop_token
g++-16 -std=c++23 -Wall -Wextra -fsanitize=address,undefined stop_token.cpp -o stop_token-gcc && ./stop_token-gcc
""")

Output

$ clang++ -std=c++23 -Wall -Wextra -fsanitize=address,undefined jthread_scope.cpp -o jthread_scope && ./jthread_scope
main: leaving the block
worker: stop requested, finishing
main: the worker has finished
sizeof(std::thread) = 8, sizeof(std::jthread) = 16
$ g++-16 -std=c++23 -Wall -Wextra -fsanitize=address,undefined jthread_scope.cpp -o jthread_scope-gcc && ./jthread_scope-gcc
main: leaving the block
worker: stop requested, finishing
main: the worker has finished
sizeof(std::thread) = 8, sizeof(std::jthread) = 16
$ clang++ -std=c++23 -Wall -Wextra -fsanitize=address,undefined stop_token.cpp -o stop_token && ./stop_token
callback: ran on the main thread: true
worker: saw the request after at least one round: true
main: request_stop() returned true, then false
main: joinable() after join(): false
$ g++-16 -std=c++23 -Wall -Wextra -fsanitize=address,undefined stop_token.cpp -o stop_token-gcc && ./stop_token-gcc
callback: ran on the main thread: true
worker: saw the request after at least one round: true
main: request_stop() returned true, then false
main: joinable() after join(): false

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 stop_session.py

The run shows the three pieces of the stop machinery:

  • request_stop() returns true when this call made the request and false when a stop had already been requested, so exactly one caller “wins”.
  • stop_requested() on the token becomes true and stays true. The worker checks it once per round, so it stops within one round, here at most 5 milliseconds later.
  • A std::stop_callback registered on the token runs when the stop is requested, in the thread that requests it, before request_stop() returns. That is why the callback reported that it ran on the main thread. If the stop had already been requested when the callback was registered, it would run at once, in the registering thread.

Step through stop_token.cpp to see who runs what, and when. The middle column is the stop state that the jthread and the worker’s token share:

Interactive · message by message A stop request, the stop state and a stop_callback Step through stop_token.cpp. The middle column is the stop state that the jthread and the worker's token share.
A stop request, the stop state and a stop_callback, step 7 of 7. Messages so far: worker to stop state: register on_stop; main to stop state: request_stop(); stop state to main: run on_stop, in main; worker to main: ended: join() returns.mainreturned true, then falsestop statestop requestedworkerstop_requested(): trueregister on_stoprequest_stop()run on_stop, in mainended: join() returns

The drawing shows the whole scene; its 7 steps are listed below.

Every step as text
  1. A callback is registered. The worker's first line registers a std::stop_callback on its token. Nothing runs yet: the callback waits in the stop state for a request.
  2. Rounds of work. The worker checks stop_requested(), finds false and sleeps 5 ms, round after round, while main sleeps for 100 ms.
  3. main requests a stop. main calls worker.request_stop(). The stop state records the request and, before the call returns, runs every callback registered on it.
  4. The callback runs in main. The callback runs inside request_stop(), in the thread that made the request. That is why it printed "callback: ran on the main thread: true".
  5. true, then false. The first request_stop() returned true, because this call made the request. The second returned false, because a stop had already been requested: exactly one caller wins.
  6. The worker notices. At its next check, within one round of about 5 ms, the worker finds stop_requested() true and leaves its loop. It had done at least one round, as it printed.
  7. join(). main's join() returns once the worker's function has returned, and joinable() is then false. The token interrupted nothing: the worker stopped because it checked.

A token cannot interrupt a thread that is blocked. sleep_for finishes its full time, and a thread waiting to read from a socket waits until data arrives. Keep each round of work short, use timeouts, or use a std::stop_callback to wake the thread up, for example by closing the socket. Condition variables have a wait that also returns on a stop request, which a later lesson of this module uses.

Key idea

A stop in C++ is a flag the thread reads: request_stop() sets it, and the thread stops only where it checks stop_requested(), so keep the gaps between checks short.

Checkpoint

Check: Stop requests and stop tokens

  1. Question 1 of 2 The worker of stop_token.cpp sleeps 5 ms per round and checks stop_requested() after each sleep. A stop is requested just after a check. About how many milliseconds pass before the worker notices it?

    Type a number.

    A hint

    The worker only looks at its token between rounds.

    Show the answer: Question 1 of 2

    Answer: 5 ms (anything from 4 to 6 counts)

    The request lands while the worker sleeps, so the worker sees it at its next check, one round later: about 5 ms. A stop is noticed only where the code looks, which is why rounds of work should be short.

  2. Question 2 of 2 A worker is in the middle of std::this_thread::sleep_for(30s) when a stop is requested on its jthread. What happens?

    Choose one answer.

    Show the answer: Question 2 of 2

    Answer: The sleep runs its full 30 s, and the worker sees the request at its next check

    Stop tokens do not wake blocked threads. Keep each round short, use timeouts, or register a std::stop_callback that wakes the thread; a later lesson of this module shows a condition-variable wait that returns on a stop request.

Passing arguments: copies unless you say otherwise

std::thread t(f, a, b) does not hand a and b to the new thread as they are. The constructor first copies or moves each argument into storage that the new thread owns, in the thread that calls the constructor, and the new thread then calls f with those copies. Three cases follow from that rule:

arguments.cpp C++ · arguments.cpp
// How std::thread passes arguments: it copies (or moves) each one into storage of its own before the thread starts.
#include <memory>
#include <print>
#include <string>
#include <thread>

void add_one(int& n) { ++n; }

void show(const std::string& s) { std::println("show() sees \"{}\"", s); }

void consume(std::unique_ptr<std::string> p) { std::println("consume() owns \"{}\"", *p); }

int main() {
    // 1. A copy: the thread gets its own std::string, made in main before the constructor returns.
    std::string text = "first";
    std::thread a(show, text);
    text = "changed in main";  // changes main's string, not the thread's copy
    a.join();

    // 2. A reference, on purpose: std::ref wraps n so that add_one() can bind its int& to it.
    int n = 41;
    std::thread b(add_one, std::ref(n));
    b.join();  // n must live at least until here
    std::println("after add_one: n = {}", n);

    // 3. A move-only argument is moved in: the thread's function becomes its owner.
    auto owned = std::make_unique<std::string>("a moved string");
    std::thread c(consume, std::move(owned));
    c.join();
    std::println("main's unique_ptr is now {}", owned ? "still set" : "empty");
}
needs_ref.cpp C++ · needs_ref.cpp
// add_one() takes int&, but std::thread passes its stored copy of n as an rvalue, which int& cannot bind to.
#include <thread>

void add_one(int& n) { ++n; }

int main() {
    int n = 41;
    std::thread t(add_one, n);  // does not compile: write std::ref(n)
    t.join();
}
A copy, std::ref, a move, and the error without std::ref Python · args_session.py
from session import run

# arguments.cpp with both compilers, then the error each one gives for needs_ref.cpp.
run("""
clang++ -std=c++23 -Wall -Wextra arguments.cpp -o arguments && ./arguments
g++-16 -std=c++23 -Wall -Wextra arguments.cpp -o arguments-gcc && ./arguments-gcc
clang++ -std=c++23 -fsyntax-only needs_ref.cpp 2>&1 | grep -E "error:" | head -2
g++-16 -std=c++23 -fsyntax-only needs_ref.cpp 2>&1 | grep -E "error:" | head -2
""")

Output

$ clang++ -std=c++23 -Wall -Wextra arguments.cpp -o arguments && ./arguments
show() sees "first"
after add_one: n = 42
consume() owns "a moved string"
main's unique_ptr is now empty
$ g++-16 -std=c++23 -Wall -Wextra arguments.cpp -o arguments-gcc && ./arguments-gcc
show() sees "first"
after add_one: n = 42
consume() owns "a moved string"
main's unique_ptr is now empty
$ clang++ -std=c++23 -fsyntax-only needs_ref.cpp 2>&1 | grep -E "error:" | head -2
…/c++/v1/__thread/thread.h:215:19: error: static assertion failed due to requirement '__is_invocable_v<void (*)(int &), int>':
…/c++/v1/__thread/thread.h:161:3: error: no matching function for call to '__invoke'
$ g++-16 -std=c++23 -fsyntax-only needs_ref.cpp 2>&1 | grep -E "error:" | head -2
…/c++/16/bits/std_thread.h:168:72: error: static assertion failed: std::thread arguments must be invocable after conversion to rvalues
…/c++/16/bits/std_thread.h:302:11: error: no type named 'type' in 'struct std::thread::_Invoker<std::tuple<void (*)(int&), int> >::__result<std::tuple<void (*)(int&), int> >'

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 args_session.py

Step through the three cases of arguments.cpp: what each constructor call hands to the new thread, and who holds what afterwards.

Interactive · message by message Three ways an argument reaches a new thread Step through arguments.cpp. Each message is what the std::thread constructor hands over; the sub-labels say who holds what afterwards.
Three ways an argument reaches a new thread, step 7 of 7. Messages so far: main to each new thread: a copy of text: "first"; main to each new thread: std::ref(n): n is 41; main to each new thread: std::move(owned).mainowned: emptyeach new threadconsume() owns the stringa copy of text: "first"std::ref(n): n is 41std::move(owned)

The drawing shows the whole scene; its 7 steps are listed below.

Every step as text
  1. A copy, made in main. std::thread a(show, text) copies text into storage that the new thread owns. The copy is made in main, before the constructor returns.
  2. main changes its string. main then sets text to "changed in main". That changes main's string only; the thread's copy still says "first".
  3. show() reads the copy. The new thread calls show() with its own copy, so the run printed show() sees "first", even if main's assignment ran before the thread read its copy.
  4. A reference, on request. std::thread b(add_one, std::ref(n)) stores a reference wrapper instead of a copy, so add_one's int& binds to main's own n, which holds 41.
  5. main's n changes. add_one increments main's n from 41 to 42. n must live until the thread has finished with it, which b.join() makes sure of.
  6. A move hands over the string. A std::unique_ptr cannot be copied, so std::thread c(consume, std::move(owned)) moves it into the thread's storage, and consume() becomes its owner.
  7. main's pointer is empty. After the move, main's owned holds nothing, as the last line printed: main's unique_ptr is now empty.

  1. A copy by default. show() saw “first”, because the thread holds its own copy of text, made before the constructor returned; changing text in main afterwards touches only main’s string.
  2. A reference only on request. add_one(int&) cannot take the thread’s stored copy, which is passed as an rvalue: An xvalue or a prvalue: an expression whose object may be moved from, because nobody will look at it again. An rvalue reference, T&&, binds only to rvalues. More in “C++ value categories, std::move and copy elision”, so needs_ref.cpp does not compile; both libraries reject it with a static assertion, and libstdc++’s message, “std::thread arguments must be invocable after conversion to rvalues”, says why. std::ref(n) passes a reference instead, and the referenced object must then outlive the thread.
  3. move-only type: A type whose objects can be moved but not copied, because its copy constructor is deleted, such as std::unique_ptr. Its objects cannot come out of a std::initializer_list, whose elements can only be copied. More in “Brace initialisation and initializer_list traps” are moved. A std::unique_ptr cannot be copied, so std::move hands its ownership to the thread’s function, and main’s pointer is empty afterwards.

One trap follows from “the function is called in the new thread”: a conversion between the argument’s type and the parameter’s type happens there too. Passing a char* to a local buffer to a function that takes std::string copies the pointer, and the new thread builds the string from the buffer later, possibly after the buffer is gone. Convert in the caller, std::thread t(f, std::string(buffer)), and the thread receives a complete string. The same thinking applies to lambdas: [=] copies what it captures, [&] refers to it. The Core Guidelines advise passing small amounts of data to threads by value (CP.31): a thread that owns its copies cannot be left holding a reference to something that has gone.

Checkpoint

Check: How arguments reach a thread

  1. Question 1 of 2 What does this print?

    Read the code, then choose one answer.

    void show(const std::string& s) { std::println("{}", s); }
    
    std::string text = "draft";
    std::thread a(show, text);
    text = "final";
    a.join();
    Show the answer: Question 1 of 2

    Answer: draft

    std::thread copies (or moves) every argument into storage the new thread owns, in the thread that calls the constructor. arguments.cpp printed show() sees "first" for the same reason.

  2. Question 2 of 2 void log_line(std::string s); A function fills a local char buffer[64], starts std::thread t(log_line, buffer);, detaches t and returns. What is wrong, and what fixes it?

    Choose one answer.

    Show the answer: Question 2 of 2

    Answer: The thread stores the pointer and builds the std::string later, possibly after buffer is gone; pass std::string(buffer) instead

    The new thread calls log_line, so the conversion of the stored pointer into a std::string happens there, possibly after the function that owns buffer has returned. std::thread t(log_line, std::string(buffer)) converts in the caller, and the thread then owns a complete copy.

How many threads?

std::thread::hardware_concurrency() returns the number of hardware thread: One of the threads a computer's processors can run at the same moment. std::thread::hardware_concurrency() returns their number as a hint, 18 on the computer that ran this track's examples, one per core, or 0 when it cannot tell. at the same time. The standard calls it a hint, and an implementation returns 0 when it cannot tell. The program below counts the primes below 10,000,000 with 1 to 36 threads. For each count it prints two times, keeping the fastest of three runs: the wall time: The time a clock on the wall would show between the start and the end of a run, waiting included. Threads that run at the same time can make it much shorter than the CPU time., what a clock on the wall would show, and the CPU time: The processor time a program's threads used, added up over all of them, without the time spent waiting. CPU time divided by wall time is the number of cores the program kept busy on average., the processor time all its threads used together. CPU time divided by wall time is the number of cores the program kept busy, on average:

speedup.cpp C++ · speedup.cpp
// Counts the primes below 10,000,000 by trial division with 1, 2, 4, 6, 12, 18 and 36 threads. For each run it
// prints the wall-clock time and the processor time all threads used together; their ratio is the number of cores
// the program kept busy on average. The numbers are dealt out in chunks of 10,000, round robin, so every thread gets
// a similar mix of small and large numbers, and each std::jthread writes its count to its own slot. Each run is
// repeated three times and the fastest is kept, because other programs on the computer slow some runs down.
#include <sys/resource.h>

#include <algorithm>
#include <chrono>
#include <cstdint>
#include <print>
#include <thread>
#include <vector>

bool is_prime(std::uint32_t n) {
    if (n < 2) return false;
    if (n % 2 == 0) return n == 2;
    for (std::uint32_t d = 3; d * d <= n; d += 2)
        if (n % d == 0) return false;
    return true;
}

constexpr std::uint32_t limit = 10'000'000;
constexpr std::uint32_t chunk = 10'000;

std::uint32_t count_primes(unsigned n_threads) {
    std::vector<std::uint32_t> counts(n_threads);
    {
        std::vector<std::jthread> workers;
        for (unsigned t = 0; t < n_threads; ++t) {
            workers.emplace_back([&counts, t, n_threads] {
                std::uint32_t c = 0;
                for (std::uint32_t first = t * chunk; first < limit; first += n_threads * chunk)
                    for (std::uint32_t n = first; n < std::min(first + chunk, limit); ++n) c += is_prime(n);
                counts[t] = c;
            });
        }
    }  // every jthread joins here
    std::uint32_t total = 0;
    for (std::uint32_t c : counts) total += c;
    return total;
}

// Processor time used so far by all threads of this process (user plus system), in milliseconds (POSIX getrusage).
double cpu_ms() {
    rusage u{};
    getrusage(RUSAGE_SELF, &u);
    auto ms = [](timeval t) { return t.tv_sec * 1000.0 + t.tv_usec / 1000.0; };
    return ms(u.ru_utime) + ms(u.ru_stime);
}

int main() {
    std::println("hardware_concurrency() = {}", std::thread::hardware_concurrency());
    std::println("threads  primes  wall time  CPU time  cores busy");
    for (unsigned n : {1u, 2u, 4u, 6u, 12u, 18u, 36u}) {
        double best_wall = 1e300, best_cpu = 0;
        std::uint32_t primes = 0;
        for (int run = 0; run < 3; ++run) {
            const double cpu0 = cpu_ms();
            const auto start = std::chrono::steady_clock::now();
            primes = count_primes(n);
            const double wall = std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - start).count();
            if (wall < best_wall) best_wall = wall, best_cpu = cpu_ms() - cpu0;
        }
        std::println("{:7}  {:6}  {:6.0f} ms  {:5.0f} ms  {:10.1f}", n, primes, best_wall, best_cpu, best_cpu / best_wall);
    }
}
Counting primes with 1 to 36 threads Python · speedup_session.py
from session import run

# The computer's processor and its two kinds of cores, then speedup.cpp built with optimisation and run once.
run("""
sysctl -n machdep.cpu.brand_string hw.perflevel0.logicalcpu hw.perflevel1.logicalcpu
clang++ -std=c++23 -O2 -Wall -Wextra speedup.cpp -o speedup && ./speedup
""")

Output

$ sysctl -n machdep.cpu.brand_string hw.perflevel0.logicalcpu hw.perflevel1.logicalcpu
Apple M5 Pro
6
12
$ clang++ -std=c++23 -O2 -Wall -Wextra speedup.cpp -o speedup && ./speedup
hardware_concurrency() = 18
threads  primes  wall time  CPU time  cores busy
      1  664579    1665 ms    936 ms         0.6
      2  664579     823 ms    932 ms         1.1
      4  664579     338 ms    927 ms         2.7
      6  664579     264 ms    836 ms         3.2
     12  664579      84 ms    852 ms        10.1
     18  664579      76 ms    858 ms        11.2
     36  664579      74 ms    878 ms        11.9

This output changes from run to run: the times change from run to run, more so while other programs keep the computer busy

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 speedup_session.py

The computer that ran the examples reported 18 hardware threads: an Apple M5 Pro with two kinds of cores, 6 in its first performance level and 12 in its second, as sysctl lists them. It was also running other programs at the time, as a shared build machine does, and the numbers show what threads can and cannot do:

  • The work stays the same; only the waiting shrinks. Every run found the same 664,579 primes and used about the same CPU time, a little under a second. Threads did not make the job smaller; they spread it over more cores, so the wall time fell more than tenfold from one thread to 18.
  • Cores busy can never pass the hardware. The last column grows with the number of threads, but it cannot exceed 18 here, however many threads the program starts, because only 18 can run at once, so 36 threads were not reliably faster than 18: in repeated runs they were sometimes a little faster and sometimes slower. With other programs competing for the same cores, even one thread got less than a whole core, and 18 threads got fewer than 18.
  • The work has to be shared fairly. The program deals numbers out in chunks of 10,000, round robin, because large numbers take longer to test than small ones; one contiguous block per thread would leave the threads with the small numbers idle while the last thread finished.

Starting a thread costs the operating system work too, so a thread is worth it only for work that takes much longer than starting it. For thousands of small tasks, a fixed set of threads that take tasks from a queue (a thread pool, built later in this module) is the usual design, as the Core Guidelines advise (CP.41).

Key idea

Threads spread a fixed amount of work over more cores: the wall time shrinks while the CPU time stays about the same, and only as far as the hardware can run threads at once.

Checkpoint

Check: How many threads pay off

  1. Question 1 of 2 A run takes 100 ms of wall time and uses 900 ms of CPU time across its threads. On average, how many cores did it keep busy?

    Type a number.

    A hint

    Divide the processor time by the time on the clock.

    Show the answer: Question 1 of 2

    Answer: 9 cores

    CPU time divided by wall time is the number of cores kept busy on average: 900 ÷ 100 = 9. speedup.cpp prints the same ratio in its last column, 11.2 for its run with 18 threads.

  2. Question 2 of 2 On the 18-core computer, why were 36 threads not reliably faster than 18 at counting the primes?

    Choose one answer.

    Show the answer: Question 2 of 2

    Answer: Only 18 threads can run at once there, so the other 18 only wait for a core

    Threads spread a fixed amount of work over the cores there are. Past the number the hardware can run at once, more threads add no cores, only threads that wait for one.

Practise: checksums on N threads

checksum_all() below computes the CRC-32 of twelve in-memory “files”, and it does all the work on the calling thread. Make it share the files out among n_threads std::jthread workers, each with a contiguous slice and each writing only its own elements of the result:

checksum_exercise.cpp C++ · checksum_exercise.cpp
// Exercise: make checksum_all() share the files out among n_threads std::jthread workers, then build and run this
// file. Every check must say "ok".
//   clang++ -std=c++23 -Wall -Wextra -fsanitize=address,undefined checksum_exercise.cpp -o checksum_exercise
//   ./checksum_exercise
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <format>
#include <print>
#include <set>
#include <string>
#include <string_view>
#include <thread>
#include <vector>

// CRC-32 (the checksum of zip and PNG files, CRC-32/ISO-HDLC), one bit at a time. Nothing to change here.
std::uint32_t crc32(std::string_view data) {
    std::uint32_t crc = 0xFFFF'FFFFu;
    for (unsigned char byte : data) {
        crc ^= byte;
        for (int bit = 0; bit < 8; ++bit) crc = (crc >> 1) ^ (0xEDB8'8320u & (0u - (crc & 1u)));
    }
    return ~crc;
}

struct Checksum {
    std::uint32_t crc = 0;
    std::thread::id worker;  // the thread that computed it
};

// Returns the CRC-32 of every file, in the same order as `files`, and which thread computed each one.
//  * Use min(n_threads, files.size()) std::jthread workers: never more threads than files, and none for no files.
//  * n_threads == 0 means "as many as std::thread::hardware_concurrency() suggests", or 1 if it returns 0.
//  * Give each worker its own contiguous slice of the files; a worker writes only its own elements of the result.
std::vector<Checksum> checksum_all(const std::vector<std::string>& files, unsigned n_threads) {
    // Starting point: every file is checksummed on the calling thread. Share the work out instead.
    std::vector<Checksum> result(files.size());
    for (std::size_t i = 0; i < files.size(); ++i) result[i] = {crc32(files[i]), std::this_thread::get_id()};
    return result;
}

int checks = 0;
int failures = 0;

void check(bool passed, std::string_view what) {
    std::println("{}  {}", passed ? "ok    " : "FAILED", what);
    ++checks;
    if (!passed) ++failures;
}

// Checks one call: the right checksums in the right order, computed by `want` different threads, none of them main.
void check_call(const std::vector<std::string>& files, unsigned n_threads, std::size_t want) {
    const std::vector<Checksum> got = checksum_all(files, n_threads);
    bool same = got.size() == files.size();
    std::set<std::thread::id> workers;
    for (std::size_t i = 0; same && i < files.size(); ++i) {
        same = got[i].crc == crc32(files[i]);
        workers.insert(got[i].worker);
    }
    check(same, std::format("n_threads = {}: {} checksums, in order", n_threads, files.size()));
    check(workers.size() == want && !workers.contains(std::this_thread::get_id()),
          std::format("n_threads = {}: computed by {} worker thread{}, none of them main (got {}{})", n_threads, want,
                      want == 1 ? "" : "s", workers.size(), workers.contains(std::this_thread::get_id()) ? ", main included" : ""));
}

int main() {
    check(crc32("123456789") == 0xCBF4'3926u, "crc32(\"123456789\") is the standard check value CBF43926");

    std::vector<std::string> files;  // twelve "files" of different sizes, kept in memory
    for (int i = 0; i < 12; ++i) files.push_back(std::string(5'000 * (i + 1), static_cast<char>('a' + i)));

    for (unsigned n : {1u, 2u, 3u, 4u, 8u}) check_call(files, n, n);
    check_call(files, 50, files.size());  // more threads asked for than there are files
    const unsigned hc = std::thread::hardware_concurrency();
    check_call(files, 0, std::min<std::size_t>(hc == 0 ? 1 : hc, files.size()));
    check(checksum_all({}, 4).empty(), "no files: an empty result");

    std::println("{} of {} checks failed", failures, checks);
}

As it starts, every checksum is right, but every thread check fails, because main computed them all:

The starting point Python · exercise_session.py
from session import run

# The exercise as it starts: every checksum is right, but main computes them all, so the thread checks fail.
run("""
clang++ -std=c++23 -Wall -Wextra -fsanitize=address,undefined checksum_exercise.cpp -o checksum_exercise
./checksum_exercise
""")

Output

$ clang++ -std=c++23 -Wall -Wextra -fsanitize=address,undefined checksum_exercise.cpp -o checksum_exercise
checksum_exercise.cpp:35:84: warning: unused parameter 'n_threads' [-Wunused-parameter]
   35 | std::vector<Checksum> checksum_all(const std::vector<std::string>& files, unsigned n_threads) {
      |                                                                                    ^
1 warning generated.
$ ./checksum_exercise
ok      crc32("123456789") is the standard check value CBF43926
ok      n_threads = 1: 12 checksums, in order
FAILED  n_threads = 1: computed by 1 worker thread, none of them main (got 1, main included)
ok      n_threads = 2: 12 checksums, in order
FAILED  n_threads = 2: computed by 2 worker threads, none of them main (got 1, main included)
ok      n_threads = 3: 12 checksums, in order
FAILED  n_threads = 3: computed by 3 worker threads, none of them main (got 1, main included)
ok      n_threads = 4: 12 checksums, in order
FAILED  n_threads = 4: computed by 4 worker threads, none of them main (got 1, main included)
ok      n_threads = 8: 12 checksums, in order
FAILED  n_threads = 8: computed by 8 worker threads, none of them main (got 1, main included)
ok      n_threads = 50: 12 checksums, in order
FAILED  n_threads = 50: computed by 12 worker threads, none of them main (got 1, main included)
ok      n_threads = 0: 12 checksums, in order
FAILED  n_threads = 0: computed by 12 worker threads, none of them main (got 1, main included)
ok      no files: an empty result
7 of 16 checks failed

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 exercise_session.py

You are done when the program prints 0 of 16 checks failed. The rules are in the comment above the function: never more threads than files, n_threads == 0 means hardware_concurrency(), and 1 when that returns 0. Hints: files.size() * t / n and files.size() * (t + 1) / n give the slice of worker t of n without leaving any file out; a std::vector<std::jthread> declared inside a block joins every worker at the closing brace, before the function returns the result. Build it once more with -fsanitize=thread instead of address,undefined: a correct solution produces no ThreadSanitizer report.

CRC Calculator (CRC-8/16/32/64) Compute the CRC-32 of your own text to compare with crc32() in the exercise: pick CRC-32/ISO-HDLC.

When all 16 checks of the checksum program say ok, turn to the questions on joins, detach, stop tokens, arguments and thread counts. Your weakest of those ideas comes up first, and a wrong pick is explained on the spot:

Practice that adapts to you

Practise this lesson

16 questions about 7 ideas of this lesson. Each answer is explained at once, and what you get right is kept in this browser.

  1. Question 1 of 16 What does t.join() do?

    Choose one answer.

    Show the answer: Question 1 of 16

    Answer: It waits until the thread of t has finished, and then everything that thread wrote is visible to the caller

    join() blocks the calling thread until the joined thread has finished. The thread's completion synchronizes with the return from join(), so the caller then sees every write the thread made.

  2. Question 2 of 16 Worker t of n takes the files from files.size() * t / n up to, but not including, files.size() * (t + 1) / n, in integer arithmetic. With 12 files and 5 workers, how many files does worker 4, the last, take?

    Type a number.

    A hint

    Work out both ends for t = 4, rounding each division down.

    Show the answer: Question 2 of 16

    Answer: 3 files

    12 × 4 / 5 = 9.6, which integer division makes 9, and 12 × 5 / 5 = 12, so worker 4 takes files 9, 10 and 11: three files. The slices are 2, 2, 3, 2 and 3 files, which add up to 12 with no file left out or taken twice.

  3. Question 3 of 16 A function starts a std::thread and returns without calling join() or detach() on it. What happens?

    Choose one answer.

    Show the answer: Question 3 of 16

    Answer: The thread object's destructor calls std::terminate(), which ends the whole program

    The destructor of a std::thread that is still joinable calls std::terminate(). The recorded runs show it with both libraries: "libc++abi: terminating" and "terminate called without an active exception", then exit status 134. std::jthread is the type whose destructor asks the thread to stop and then joins it.

  4. Question 4 of 16 In dangling_detach.cpp, why can a normal build print the right word, garbage or nothing?

    Choose one answer.

    Show the answer: Question 4 of 16

    Answer: The detached thread reads channel after start_logger() has returned, which is undefined behaviour

    A detached thread can outlive the variables it refers to. Reading a destroyed string is undefined behaviour, so any output, or none, is possible; AddressSanitizer reports it as a stack-use-after-return.

  5. Question 5 of 16 The logger's lambda is changed to capture channel by value, [channel], and is still detached. What does that fix, and what not?

    Choose one answer.

    Show the answer: Question 5 of 16

    Answer: The thread owns a copy, so it no longer reads a dead variable; it is still stopped wherever it is when the process ends

    Passing data by value removes one danger of detach(), the dangling reference (CP.31 advises small data by value). The other two stay: no way to wait for the thread, and a thread stopped mid-work when main returns.

  6. Question 6 of 16 What does the destructor of a std::jthread that is still running a thread do?

    Choose one answer.

    Show the answer: Question 6 of 16

    Answer: It calls request_stop() and then join()

    ~jthread() asks the thread to stop through its stop state and then waits for it. The thread stops only if its function checks the std::stop_token it was given; a function that never looks at the token keeps the destructor waiting until it returns.

  7. Question 7 of 16 Code that must still build as C++17 needs a worker it can ask to stop. What does it use?

    Choose one answer.

    Show the answer: Question 7 of 16

    Answer: std::thread with a flag of its own that the thread checks, and a join() on every path

    The comparison table gives the rule: std::thread for code that must still build as C++17, with your own flag for stopping, and std::jthread for everything else.

  8. Question 8 of 16 main calls worker.request_stop() twice in a row. What do the two calls return?

    Choose one answer.

    Show the answer: Question 8 of 16

    Answer: true, then false

    request_stop() returns true when this call made the stop request and false when a stop had already been requested, so code can tell which caller was first.

  9. Question 9 of 16 A worker registers a std::stop_callback on its token, and later main calls worker.request_stop(). In which thread does the callback run?

    Choose one answer.

    Show the answer: Question 9 of 16

    Answer: In main, inside the request_stop() call, before it returns

    Callbacks registered before the request run synchronously in the thread that makes the request, which is why stop_token.cpp printed "callback: ran on the main thread: true". A callback registered after a stop was already requested runs at once, in the thread that registers it.

  10. Question 10 of 16 A worker blocks for minutes in a socket read, and it must stop soon after request_stop(). Which design follows the lesson?

    Choose one answer.

    Show the answer: Question 10 of 16

    Answer: Register a std::stop_callback that wakes the thread, for example by closing the socket, so that the read returns

    A stop request is only seen where the thread looks. For a thread that blocks, make the block end: use timeouts, or a std::stop_callback that closes the socket or otherwise wakes the thread up.

  11. Question 11 of 16 How does std::thread t(f, a); pass a to f by default?

    Choose one answer.

    Show the answer: Question 11 of 16

    Answer: It copies (or moves) a into storage the new thread owns, and f is called with that copy

    The constructor copies or moves every argument into storage of the new thread, so the thread never depends on the caller's variables unless it is given a reference or a pointer on purpose.

  12. Question 12 of 16 void bump(int& n) { ++n; } is given. Which line starts a thread that increments count in place?

    Read the code, then choose one answer.

    int count = 0;
    // (a) std::thread t(bump, count);
    // (b) std::thread t(bump, std::ref(count));
    // (c) std::thread t(bump, &count);
    // (d) std::thread t(bump, std::move(count));
    Show the answer: Question 12 of 16

    Answer: (b), and count must outlive the thread

    std::thread stores its own copy of every argument and passes that copy as an rvalue, which an int& cannot bind to, so (a) and (d) do not compile. (c) passes an int*, which does not convert to int&. std::ref wraps a reference that converts to int&; count must then live at least until the thread has been joined.

  13. Question 13 of 16 owned is a std::unique_ptr<std::string>, and consume takes one by value. Why does std::thread c(consume, owned); not compile, and what does?

    Choose one answer.

    Show the answer: Question 13 of 16

    Answer: A unique_ptr cannot be copied; std::thread c(consume, std::move(owned)) moves it in

    Move-only types are moved into the thread with std::move, and the caller's object is left empty, as arguments.cpp printed: main's unique_ptr is now empty.

  14. Question 14 of 16 std::thread::hardware_concurrency() returns 0. What does that mean?

    Choose one answer.

    Show the answer: Question 14 of 16

    Answer: The library could not tell how many hardware threads there are; choose a sensible default yourself

    The standard calls the value a hint and says an implementation should return 0 when it cannot be computed. Treat 0 as unknown: the exercise in this lesson uses 1 thread in that case, and many programs use a fixed number such as 4.

  15. Question 15 of 16 Why does speedup.cpp deal the numbers out in chunks of 10,000, round robin, instead of giving each thread one contiguous block?

    Choose one answer.

    Show the answer: Question 15 of 16

    Answer: Large numbers take longer to test, so contiguous blocks would leave the threads with small numbers idle while the last one finished

    A job is done when its slowest thread is done. Testing a large number by trial division takes longer than a small one, so a fair split gives every thread a similar mix.

  16. Question 16 of 16 A program has 20,000 small tasks of about a microsecond each. Which design does the lesson recommend?

    Choose one answer.

    Show the answer: Question 16 of 16

    Answer: A fixed set of threads that take tasks from a queue, a thread pool

    Starting a thread is work for the operating system. For thousands of small tasks, start a fixed set of threads once and let them take tasks from a queue; the thread pool lesson of this module builds one.

Interview questions

Fresher · What happens if a std::thread object is destroyed while it is still joinable, and how do you avoid it?

Its destructor calls std::terminate(), which ends the program, usually with abort(). A thread is joinable from the moment it starts until join() or detach() is called, even after its function has returned. To avoid it, make sure every path joins: call join() before the object goes out of scope, including the paths where an exception leaves the function early. The simplest fix is std::jthread (C++20), whose destructor requests a stop and then joins. detach() also avoids the terminate, but then nobody waits for the thread, which keeps running after the function returns and can use objects that no longer exist. The standard terminates on purpose: an automatic join could make a program wait where nobody expects it, and an automatic detach could leave a thread using dead objects.

Three follow-ups tend to come next: why the rule exists, whether detach() is a fix, and when std::thread still fits. Pick your reply to each, and see what it covers and what it misses:

Checkpoint

Interview round: three follow-up questions

  1. Question 1 of 3 Interview follow-up: why does the standard make ~thread() call std::terminate() instead of joining or detaching for you?

    Choose one answer.

    Show the answer: Question 1 of 3

    Answer: An automatic join could make a program wait where nobody expects it, and an automatic detach could leave a thread using objects that are gone; both would show up only when an exception skips the join

    A strong answer names both bad defaults, a hidden wait and a thread outliving its objects, and says why they would be found late: they happen on the paths where an exception skips the join. Then it names the fix, std::jthread.

  2. Question 2 of 3 Interview follow-up: a colleague fixes the std::terminate() by calling detach() right after starting each thread. What do you say?

    Choose one answer.

    Show the answer: Question 2 of 3

    Answer: It hides the crash but removes the owner. Nobody waits for the thread, it is stopped wherever it is when main returns, and it may use local objects that are gone; use std::jthread

    detach() trades one problem for three: no way to know when the thread is done, a thread cut off when the process ends, and references that can dangle. std::jthread keeps the owner and removes the std::terminate().

  3. Question 3 of 3 Interview follow-up: when is std::thread still the better choice than std::jthread?

    Choose one answer.

    Show the answer: Question 3 of 3

    Answer: In code that must still build as C++17, where std::jthread does not exist

    std::jthread does everything std::thread does and adds a destructor that joins and a stop token. Older standards are the reason to keep std::thread; then join on every path, including the ones an exception takes.

Two longer problems in the concurrency module’s question bank grow out of this lesson, each with a first step to try: audit records written by one detached thread per request, and a shutdown that must stop blocked workers within a second.

Model answers open with any pass: ₹79 for 1 week ($2.99), one payment that never renews.

  1. Mid to senior

    A service starts a std::thread for every request to write an audit record, and detaches it. Under load its memory grows, some records are missing after a restart, and the process sometimes crashes while shutting down. Explain each symptom and redesign the code.

    Model answer, Pro. Model answers open with any pass; prices are in the notice on this page. First step and follow-ups

  2. Mid to senior

    A program has worker threads that wait for jobs in a queue, sleep between polls or block reading a socket. It must exit within one second of Ctrl-C, without losing the job each worker is doing. How do you design the shutdown?

    Model answer, Pro. First step and follow-ups

Key takeaways

  • std::thread t(f, args...) starts running f(args...) at once; t.join() waits for it and makes everything it wrote visible to the joining thread.
  • A joinable std::thread must be joined or detached before it is destroyed, or the program terminates; prefer std::jthread, which requests a stop and joins in its destructor.
  • Threads that each write only their own data need no locks; split the work so that every thread has a similar amount.
  • Arguments are copied or moved into the thread; use std::ref only for objects that outlive the thread, and never detach a thread that refers to local variables.
  • Stopping is cooperative: request_stop() sets the token, and the thread checks stop_requested() at points where it is safe to stop.
  • hardware_concurrency() is a hint, and threads beyond it do not make work that needs the processor faster.

Terms in this lesson

CPU time
The processor time a program's threads used, added up over all of them, without the time spent waiting. CPU time divided by wall time is the number of cores the program kept busy on average.
hardware thread
One of the threads a computer's processors can run at the same moment. std::thread::hardware_concurrency() returns their number as a hint, 18 on the computer that ran this track's examples, one per core, or 0 when it cannot tell.
joinable
Said of a std::thread or std::jthread object that still represents a thread of execution: from the moment it starts the thread until join() or detach() is called, even after the thread's function has returned. Destroying a joinable std::thread calls std::terminate().
std::stop_token
A handle through which a thread can see whether a stop has been requested: its stop_requested() turns true once request_stop() is called on the matching std::jthread or std::stop_source. It never interrupts the thread; the thread checks it.
synchronizes with
A relation of the C++ memory model between an operation in one thread and an operation in another: everything the first thread did before its operation is visible to the second thread after its own. A thread's end synchronizes with the return of the join() that waits for it.
wall time
The time a clock on the wall would show between the start and the end of a run, waiting included. Threads that run at the same time can make it much shorter than the CPU time.

References

Related tools

Report a problem with this lesson

Quick answers and tool search

Type to search tools or to get a quick answer, for example 18% of 2500. Use the up and down arrow keys to move through the results, Enter to choose, and Escape to close.