CKTIS / C++
std::jthread — automatic joining and cooperative stopping
std::jthread owns a thread and, when a joinable object is destroyed, requests stopping and then waits for completion. std::jthread::request_stop does not forcibly interrupt execution: the worker must cooperate, for example by checking a std::stop_token.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
#include <thread>
#include <stop_token>
std::jthread worker([](std::stop_token token) {
// Cooperatively observe token.stop_requested().
});
bool first_request = worker.request_stop();std::jthread, declared in <thread>, is a movable, noncopyable thread owner. The constructor tests invocability using the decayed types of the callable and arguments, not the original supplied expressions. If std::is_invocable_v<std::decay_t<F>, std::stop_token, std::decay_t<Args>...> is true (where F and Args... are the deduced callable and argument types), the constructor selects the call with std::stop_token as the first argument. Otherwise it selects the call without a token, which must be valid for the same decayed types. Receiving a token alone does not implement stopping.
std::jthread::request_stop() has the signature bool request_stop() noexcept. It returns true when this call first records a request in the shared stop state; subsequent requests return false. It also returns false when there is no stop state. The result does not confirm that the thread has finished. Registered stop callbacks execute synchronously during the first successful request.
If joinable() returns true, the destructor calls request_stop() and then join(). Unlike std::thread, it therefore needs no manual join before ordinary scope exit. This is not forced cancellation: ignoring the token or remaining in a blocking operation can delay completion indefinitely. Captured references must remain valid until the worker finishes.
In the example, the first request produces true and the second produces false. The destructor waits for the worker before observed is read, so this ordinary bool needs no atomic operations here. The yield() loop is only a small busy-wait demonstration, not an efficient waiting pattern. The repository mode is C++23; this does not attest production runtime support. The supplied reference is an evolving standard draft, not a frozen C++23 edition.
Examples
#include <iostream>
#include <stop_token>
#include <thread>
int main() {
bool observed = false;
bool first = false;
bool second = false;
{
std::jthread worker([&observed](std::stop_token token) {
while (!token.stop_requested()) {
std::this_thread::yield();
}
observed = true;
});
first = worker.request_stop();
second = worker.request_stop();
}
std::cout << std::boolalpha
<< first << ' ' << second << ' ' << observed << '\n';
}
Result
true false true
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.
Sources
- C++ working draft — Concurrency support library [thread]
- C++ working draft — Constructors, move, and assignment [thread.jthread.cons]
- C++ working draft — Stop token handling [thread.jthread.stop]
- C++ working draft — Member functions [stopsource.mem]
- C++ working draft — Stop token concepts [stoptoken.concepts]
- C++ working draft — Member functions [thread.jthread.mem] (unofficial HTML rendering)