std::stop_token::stop_possible

From cppreference.com
< cpp‎ | thread‎ | stop token
 
 
Concurrency support library
Threads
(C++11)
(C++20)
(C++20)
this_thread namespace
(C++11)
(C++11)
(C++11)
Atomic types
(C++11)
(C++20)
Initialization of atomic types
(C++11)(deprecated in C++20)
(C++11)(deprecated in C++20)
Free functions for atomic operations
Free functions for atomic flags
Memory ordering
Mutual exclusion
(C++11)
Generic lock management
(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
(C++11)
(C++11)
Condition variables
(C++11)
Semaphores
Latches and barriers
(C++20)
(C++20)
Futures
(C++11)
(C++11)
(C++11)
(C++11)
 
 
[[nodiscard]] bool stop_possible() const noexcept;
(since C++20)

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.

Parameters

(none)

Return value

false if the stop_token object has no associated stop-state, or it did not yet receive a stop request and there are no associated std::stop_source object(s); true otherwise.

Notes

If the stop_token object has associated stop-state and a stop request has already been made, this function still returns true.

If the stop_token object has associated stop-state from a std::jthread—for example, the stop_token was retrieved by invoking get_stop_token() on a std::jthread object—then this function always returns true. A std::jthread always has an internal std::stop_source object, even if the thread's invoking function does not check it.

Example