std::stop_token Class
The std::stop_token is a class that provides a way to call off asynchronous operation requests. It is equivalent to a token that can be transferred through different program fragments, providing the ability to notify about the cancellation of an ongoing process. Interestingly, the std::stop_token objects are highly convenient as they can be easily moved and copied, without adding much burden to the system’s processing.
Syntax
stop_token token_name = token;
std::stop_token Member Methods
The two primary functions of the std::stop_token class are:
- stop_requested(): This function responds with a value of true when a cancellation request has been made, otherwise, it returns false.
- stop_possible(): This function returns true if the related std::stop_source object can be utilized to request a cancellation, otherwise it returns false.
C++ 20 – Header
C++20 has introduced the <stop_token> Header, presenting an alternative method for canceling asynchronous operations. A new class std::stop_token is provided by the <stop_token> header that allows for seamless communication of a termination request between two threads.
In this article, we will discuss the basics of <stop_token> and how to use it in C++20.
Contact Us