std::jthread::request_stop(3)
π NAME π
okay bestie, welcome to std::jthread::request_stop(3) from the C++ Standard Libary manual β¨ The title and the name are literally both std::jthread::request_stop, and that's iconic in a "the man page said what it said" way π
std::jthread::request_stop - std::jthread::request_stop
β¨ Synopsis β¨
bool request_stop() noexcept; (since C++20)
So this little icon issues a stop request to the internal stop-state, if it has not yet already had stop requested π«Ά
The determination is made atomically, and if stop was requested, the stop-state is atomically updated to avoid race conditions, such that:
stop_requested()andstop_possible()can be concurrently invoked on otherstd::stop_tokensandstd::stop_sourcesof the same shared stop-state.request_stop()can be concurrently invoked from multiple threads on the samejthreadobject or on otherstd::stop_sourceobjects associated with the same stop-state, and only one will actually perform the stop request.
However, see the Notes section π
π Parameters π
(none) β literally no arguments, bestie. It just does the thing.
πΈ Return value πΈ
true if this invocation made a stop request, otherwise false.
π¦ Postconditions π¦
For a std::stop_token retrieved by get_stop_token() or a std::stop_source retrieved by get_stop_source(), stop_requested() is true. Periodt.
π Notes π
Okay so here's the tea, and it's actually important π:
- If the
request_stop()does issue a stop request (i.e., returnstrue), then anystd::stop_callbacksregistered for the same associated stop-state will be invoked synchronously, on the same threadrequest_stop()is issued on. If an invocation of a callback exits via an exception,std::terminateis called. π - If a stop request has already been made, this function returns
false. However there is no guarantee that another thread orstd::stop_sourceobject which has just (successfully) requested stop for the same stop-state is not still in the middle of invoking astd::stop_callbackfunction. - If the
request_stop()does issue a stop request (i.e., returnstrue), then all condition variables of base typestd::condition_variable_anyregistered with an interruptible wait forstd::stop_tokensassociated with the jthread's internal stop-state will be awoken.
π Example π
// Run this code
#include <chrono>
#include <condition_variable>
#include <iostream>
#include <mutex>
#include <thread>
using namespace std::chrono_literals;
int main()
{
// A sleepy worker thread
std::jthread sleepy_worker(
[](std::stop_token stoken)
{
for (int i = 10; i; --i)
{
std::this_thread::sleep_for(300ms);
if (stoken.stop_requested())
{
std::cout << "Sleepy worker is requested to stop\n";
return;
}
std::cout << "Sleepy worker goes back to sleep\n";
}
});
// A waiting worker thread
// The condition variable will be awoken by the stop request.
std::jthread waiting_worker(
[](std::stop_token stoken)
{
std::mutex mutex;
std::unique_lock lock(mutex);
std::condition_variable_any().wait(lock, stoken, []{ return false; });
std::cout << "Waiting worker is requested to stop\n";
return;
});
// std::jthread::request_stop() can be called explicitly:
std::cout << "Requesting stop of sleepy worker\n";
sleepy_worker.request_stop();
sleepy_worker.join();
std::cout << "Sleepy worker joined\n";
// Or automatically using RAII:
// waiting_worker's destructor will call request_stop()
// and join the thread automatically.
}
π Possible output π
Requesting stop of sleepy worker
Sleepy worker is requested to stop
Sleepy worker joined
Waiting worker is requested to stop
source/version: http://cppreference.com Β· date: 2024.06.10 Β· page: std::jthread::request_stop(3) β bye bestie, go stop those threads responsibly πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms