std::stop_t...op_requested(3)
π std::stop_t...op_requested(3) π
Okay so this is the std::stop_token::stop_requested page from the C++ Standard Libary manual, bestie β¨ (yes, that's the spelling in the source, we are not fixing it π). She's giving C++ Standard Library realness, and we're gonna walk through it together π«Ά
πΈ NAME πΈ
std::stop_token::stop_requested - std::stop_token::stop_requested
β¨ Synopsis β¨
[[nodiscard]] bool stop_requested() const noexcept; (since C++20)
Checks if the stop_token object has associated stop-state and that state has received a stop request. A default constructed stop_token has no associated stop-state, and thus has not had stop requested.
π«Ά Parameters π«Ά
(none) β zero parameters, she said "no thoughts, just vibes" π
π₯ Return value π₯
true if the stop_token object has associated stop-state and it received a stop request, false otherwise.
π Example π
// Run this code
#include <chrono>
#include <condition_variable>
#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 << name << ": stop_possible = " <<
token.stop_possible();
std::cout << ", stop_requested = " << token.stop_requested() <<
'\n';
};
// 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
That's the whole page, bestie π«Ά Source/version: http://cppreference.com β¨ Date: 2024.06.10 π And the footer title was std::stop_t...op_requested(3) β slay, periodt π
π¦ 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