C++20 Measured

How Fast Does std::jthread Actually Stop? Measured Shutdown Latency

The stop request is instant. The worker noticing it is not. With a 100 ms poll interval, shutdown takes a median 46 to 73 ms; with a condition variable wait, about 0.10 ms. Both measured, on both compilers.

Written November 8, 2026 · Tested with GCC 13.3.0 · 11 min read

By the end of this guide
  • The measured table
  • What the numbers say
  • The polling worker
  • The wait form

What this tutorial measures

std::jthread joins at destruction and carries a stop token, so a forgotten join and a thread with no way to stop are both gone by construction. The daily tip teaches that much, with a worker that checks its token in a loop. This tutorial does not repeat it. It measures the one thing the tip leaves open: how long passes between request_stop() and join() returning, for a worker that checks its token on a timer.

Call that delay the shutdown latency. It is the time your program spends waiting for a thread that has already been asked to stop. The request itself costs nothing; the worker still has to notice it, and the notice can only happen at the next check. The size of that gap is set by how the worker waits, not by how fast the request is sent.

The measured table

The worker is a std::jthread that polls its stop token every N ms, sleeping between checks. The main thread sleeps a jittered 250 to 449 ms, then calls request_stop() and join(), and the latency is the time from the request to the join returning. Compilers are GCC 13.3.0 and Clang 18.1.3, both with -std=c++20 -O2 -pthread.

Case GCC run A median GCC run B median Clang 18 median Fastest Slowest
poll 1 ms 0.66 0.61 0.75 0.14 1.20
poll 10 ms 5.86 3.38 7.92 1.11 10.29
poll 100 ms 72.69 56.27 46.28 6.45 96.33
cv-wait 0.100 0.101 0.094 0.076 0.164

All times are milliseconds, request to join return. Fastest and Slowest are the extremes across all 21 runs of the case.

What the numbers say

The median shutdown latency is about half the poll interval. A 100 ms poll costs a median 46 to 73 ms to notice the stop. A 10 ms poll costs a median 3.4 to 7.9 ms. A 1 ms poll costs a median 0.61 to 0.75 ms, which is half of 1 ms plus about 0.1 ms of wake and join overhead.

The worst case is the full interval. The slowest 100 ms run took 96 ms. The slowest 10 ms run took 10.3 ms. The slowest 1 ms run took 1.2 ms. That is the shape to plan around: on average you pay half the interval, and sometimes you pay all of it, because the request can land just after a check, leaving a full sleep still to run.

The condition variable wait does not pay any of this. Its medians are 0.094 to 0.101 ms on both compilers, with a slowest run of 0.164 ms. That is roughly 500 to 700 times faster than the 100 ms poll median. The stop request wakes the wait directly, so no sleep interval stands between the request and the exit.

One warning about the medians: 7 runs is a small sample, and medians move between runs. The Clang 10 ms median was 3.88 ms on 2026-11-07 and 7.92 ms on 2026-11-08. Read the table for the shape, half the interval on average and the full interval worst case, not as constants to quote to two decimals.

The polling worker

The worker loop is four lines. It checks the token, sleeps for the interval, and checks again. The stop can only be noticed after a sleep returns, which is why the interval sets the latency.

C++
std::jthread w([interval_ms](std::stop_token st) {
    while (!st.stop_requested()) {
        std::this_thread::sleep_for(std::chrono::milliseconds(interval_ms));
    }
});

Shortening the interval shortens the latency, and the table shows the price: a 1 ms poll still costs a median 0.61 to 0.75 ms, and the thread wakes a thousand times a second to do nothing. Polling buys a faster stop by spending wakeups, and even the fastest poll is six times slower than the wait.

The wait form

The fix is to wait on the token instead of polling it. The worker blocks on a condition variable, and the stop request wakes it. The call to use is the stop_token overload of condition_variable_any::wait, which takes the lock, the token, and a predicate, and returns true when the predicate holds and false when the stop request woke it with the predicate still false.

C++
std::mutex m;
std::condition_variable_any cv;
bool done = false;
std::jthread w([&](std::stop_token st) {
    std::unique_lock lk(m);
    cv.wait(lk, st, [&]{ return done; });
});

Two details matter here. First, the type is condition_variable_any, not condition_variable. Only the condition_variable_any class has the stop_token wait overloads; the plain condition_variable class has no overload that takes a token, so a wait on the plain type cannot be woken by a stop request. Second, the return value tells you which wake happened: true means the predicate became true, false means a stop was requested while the predicate was still false, so the caller can tell a real signal from a shutdown.

The probe that produced the 0.10 ms row also sets done to true and calls notify_all() after request_stop(), as a second wake path. The measured latency is the stop-request wake plus that redundant notify, not a notify-only result. The stop request alone wakes the wait; the extra notify changes nothing about the shape of the result.

The full probe program

This is the program that produced the table. It measures all four cases in one run: the three poll intervals and the condition variable wait. The warm-up before each run is jittered, for the reason the method note below gives.

C++
// jthread stop-check interval vs shutdown latency probe (v2)
// Measures time from request_stop() to join() return for a worker that
// polls its stop_token every `interval_ms`, plus a condition_variable
// wait variant that wakes immediately. 7 runs per case; report min/med/max.
// v2 fix: per-run warm-up is jittered over a 200ms window so the stop
// request lands at a random phase of the worker's sleep cycle — a fixed
// 300ms warm-up phase-locks to the 1/10/100ms sleeps and the "median"
// becomes one phase offset, not a latency distribution (v1 lesson).
#include <algorithm>
#include <chrono>
#include <condition_variable>
#include <cstdlib>
#include <iostream>
#include <mutex>
#include <random>
#include <thread>
#include <vector>

using sclock = std::chrono::steady_clock;

static int jittered_warmup(std::mt19937& rng) {
    // Uniform 250-449ms: spans >1 full 100ms sleep cycle, decorrelates phase.
    return std::uniform_int_distribution<int>(250, 449)(rng);
}

static void measure_poll(int interval_ms, int runs, std::mt19937& rng) {
    std::vector<double> lat;
    for (int r = 0; r < runs; ++r) {
        std::jthread w([interval_ms](std::stop_token st) {
            while (!st.stop_requested()) {
                std::this_thread::sleep_for(std::chrono::milliseconds(interval_ms));
            }
        });
        std::this_thread::sleep_for(std::chrono::milliseconds(jittered_warmup(rng)));
        auto t0 = sclock::now();
        w.request_stop();
        w.join();
        auto t1 = sclock::now();
        lat.push_back(std::chrono::duration<double, std::milli>(t1 - t0).count());
    }
    std::sort(lat.begin(), lat.end());
    std::cout << "poll " << interval_ms << "ms: runs=";
    for (double v : lat) std::cout << v << " ";
    std::cout << "| min=" << lat.front() << " med=" << lat[lat.size()/2]
              << " max=" << lat.back() << "\n";
}

static void measure_cv(int runs, std::mt19937& rng) {
    std::vector<double> lat;
    for (int r = 0; r < runs; ++r) {
        std::mutex m;
        std::condition_variable_any cv;
        bool done = false;
        std::jthread w([&](std::stop_token st) {
            std::unique_lock lk(m);
            cv.wait(lk, st, [&]{ return done; });
        });
        std::this_thread::sleep_for(std::chrono::milliseconds(jittered_warmup(rng)));
        auto t0 = sclock::now();
        w.request_stop();
        { std::lock_guard lk(m); done = true; }
        cv.notify_all();
        w.join();
        auto t1 = sclock::now();
        lat.push_back(std::chrono::duration<double, std::milli>(t1 - t0).count());
    }
    std::sort(lat.begin(), lat.end());
    std::cout << "cv-wait: runs=";
    for (double v : lat) std::cout << v << " ";
    std::cout << "| min=" << lat.front() << " med=" << lat[lat.size()/2]
              << " max=" << lat.back() << "\n";
}

int main() {
    std::cout << "compiler: " << __VERSION__ << "\n";
    std::mt19937 rng(std::random_device{}());
    measure_poll(1, 7, rng);
    measure_poll(10, 7, rng);
    measure_poll(100, 7, rng);
    measure_cv(7, rng);
    return 0;
}

How to run it

The table was produced with these commands, run twice under GCC and once under Clang. Each run prints one line per case with all 7 runs and the min, median, and max.

Bash
g++ -std=c++20 -O2 -pthread -o probe-gcc probe.cpp
./probe-gcc
clang++ -std=c++20 -O2 -pthread -o probe-clang probe.cpp
./probe-clang

One GCC run printed:

Output
compiler: 13.3.0
poll 1ms: runs=0.209884 0.249074 0.491555 0.658958 0.67416 1.06188 1.13375 | min=0.209884 med=0.658958 max=1.13375
poll 10ms: runs=2.00354 2.01631 4.97491 5.8619 6.62935 7.00025 7.03136 | min=2.00354 med=5.8619 max=7.03136
poll 100ms: runs=6.44932 24.7949 43.6047 72.6945 76.5485 86.3719 93.3118 | min=6.44932 med=72.6945 max=93.3118
cv-wait: runs=0.075705 0.095314 0.097725 0.100455 0.101214 0.105618 0.151794 | min=0.075705 med=0.100455 max=0.151794

Why the first version of this probe lied

The first version of this probe used a fixed 300 ms warm-up before each stop request. 300 is a multiple of 1, 10, and 100, so the stop request landed at the same point in the worker's sleep cycle every time. The 100 ms case reported a median of 4.7 ms, and the 7 runs in one process split into two clusters, near 4 ms or near 98 ms. That 4.7 ms was one phase offset, not a latency distribution: the probe was measuring where the request fell in the cycle, not how long the worker took to notice it.

The fix is in the program above. The warm-up is jittered uniformly over 250 to 449 ms per run, which spans more than one full 100 ms cycle and puts the request at a random phase each time. After that change, the two GCC processes and Clang agree in shape, with medians near half the interval and maxima near the full interval. The rule to take from this: any latency probe whose stimulus and subject share a clock must jitter the stimulus phase across runs, and a median is only trusted when two independent processes agree.

What this does not cover

The table rests on 7 runs per case, so the medians move between runs, as the Clang 10 ms case shows. The numbers are this box's, under its load at the time, not constants of the standard. The worker does no work between checks; a worker that does real work between checks still only notices the stop at its next check, so the same interval rule applies, with the work time added on top.

The wait_for and wait_until overloads that take a stop token were not measured here. They wake on a stop request the same way wait does, so the same shape is expected, but that is an inference from the reference text, not a measurement in this table.

FAQ

Does std::jthread stop the thread by itself?

No. request_stop() only sets the flag in the shared stop state. The worker has to notice it, and join() waits until the worker finishes. A worker that never checks its token never stops, and the destructor's join waits forever. The destructor requests a stop and joins; it cannot force a thread out of a loop that never looks.

Why not just poll every 1 ms?

It works, and the table shows it: a median 0.61 to 0.75 ms. But the thread wakes a thousand times a second to check a flag that is almost never set, and even that fastest poll is about six times slower than the wait, which sleeps until it is needed. Polling buys speed with wakeups and still loses.

Does the wait form need the notify_all()?

No. The stop request wakes the wait on its own through the token overload. The notify in the probe is an extra wake path that the measurement does not need. In real code, the predicate is the real signal and the stop request is the shutdown path, and wait tells them apart by its return value.

Sources

Summary
  • If shutdown speed matters, do not poll a stop token from a sleep. Wait on it.
  • If you must poll, the interval you pick is the shutdown latency budget: about half the interval on average, the whole interval worst case.
  • Use condition_variable_any for the stop token wait. The plain condition_variable has no stop token overload.
  • Jitter the stimulus phase in any latency probe that shares a clock with its subject, and trust a median only when two independent processes agree.

Keep Exploring