std::stop_t...op_possible(3)
π NAME π
okay bestie, you found it: std::stop_t...op_possible(3) from the C++ Standard Libary manual. The actual thing being documented is std::stop_token::stop_possible - std::stop_token::stop_possible. Yes, the name is giving "same thing twice" and honestly? iconic. β¨
This is the member function that tells you whether a stop_token can ever be asked to stop. It's the vibe check for stop-state, basically. π
β¨ Synopsis β¨
[[nodiscard]] bool stop_possible() const noexcept;
(since C++20)
It checks if the stop_token object has associated stop-state, and that state either has already had a stop requested or it has associated std::stop_source object(s). π«Ά
A default constructed stop_token has no associated stop-state, and thus cannot be stopped; the associated stop-state for which no std::stop_source object(s) exist can also not be stopped if such a request has not already been made.
It's also [[nodiscard]] and noexcept β both right there in the signature. π
π Parameters π
(none) β bestie, there are literally zero parameters here. Not a single one. Slay. π
π¦ Return value π¦
It returns false if the stop_token object has no associated stop-state, or if it did not yet receive a stop request and there are no associated std::stop_source object(s). It returns true otherwise. Simple as that, fr fr. β¨
πΈ Notes πΈ
- If the
stop_tokenobject has associated stop-state and a stop request has already been made, this function still returnstrue. No take-backs, no "well actually it's done now" β stilltrue. π - If the
stop_tokenobject has associated stop-state from astd::jthreadβ for example, thestop_tokenwas retrieved by invokingget_stop_token()on astd::jthreadobject β then this function always returnstrue. Astd::jthreadalways has an internalstd::stop_sourceobject, even if the thread's invoking function does not check it. So yeah: jthread tokens are always possible-to-stop, even when nobody's checking. π
π₯ Example π₯
The example even has that little // Run this code marker, and here's the whole thing, right here:
#include <chrono>
#include <condition_variable>
#include <format>
#include <iostream>
#include <mutex>
#include <string_view>
#include <thread>
using namespace std::chrono_literals;
int main()
{
std::cout << std::boolalpha;
auto print = [](std::string_view name, const std::stop_token&
token)
{
std::cout << std::format("{}: stop_possible = {:s}, stop_requested = {:s}\n",
name, token.stop_possible(), token.stop_requested()
);
};
// A worker thread that will listen to stop requests
auto stop_worker = std::jthread([](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 worker thread that will only stop when completed
auto inf_worker = std::jthread([]()
{
for (int i = 5; i; --i)
{
std::this_thread::sleep_for(300ms);
std::cout << " Run as long as we want\n";
}
});
std::stop_token def_token;
std::stop_token stop_token = stop_worker.get_stop_token();
std::stop_token inf_token = inf_worker.get_stop_token();
print("def_token ", def_token);
print("stop_token", stop_token);
print("inf_token ", inf_token);
std::cout << "\nRequest and join stop_worker:\n";
stop_worker.request_stop();
stop_worker.join();
std::cout << "\nRequest and join inf_worker:\n";
inf_worker.request_stop();
inf_worker.join();
std::cout << '\n';
print("def_token ", def_token);
print("stop_token", stop_token);
print("inf_token ", inf_token);
}
β Possible output β
def_token : stop_possible = false, stop_requested = false
stop_token: stop_possible = true, stop_requested = false
inf_token : stop_possible = true, stop_requested = false
Request and join stop_worker:
Run as long as we want
Sleepy worker is requested to stop
Request and join inf_worker:
Run as long as we want
Run as long as we want
Run as long as we want
Run as long as we want
def_token : stop_possible = false, stop_requested = false
stop_token: stop_possible = true, stop_requested = true
inf_token : stop_possible = true, stop_requested = true
π source: http://cppreference.com Β· version/date: 2024.06.10 Β· now go be iconic with your stop tokens, bestie π«Ά
π¦ 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