[ Web Proxy ]
URL:
Viewing: https://en.cppreference.com/cpp/thread/inplace_stop_token [Back]  [Original]

std::inplace_stop_token - cppreference.com
cppreference.com
Namespaces
Variants

std::inplace_stop_token

From cppreference.com
 
 
Concurrency support library
Threads
(C++11)
(C++20)
this_thread namespace
(C++11)
(C++11)
(C++11)
Cooperative cancellation
Mutual exclusion
(C++11)
Generic lock management
(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)
Safe Reclamation
Read-Copy-Update Mechanism
(C++26)
Hazard Pointers
Atomic types
(C++11)
(C++20)
Initialization of atomic types
(C++11)(deprecated in C++20)
(C++11)(deprecated in C++20)
Memory ordering
(C++11)(deprecated in C++26)
Free functions for atomic operations
Free functions for atomic flags
 
 
Defined in header <stop_token>
class inplace_stop_token;
(since C++26)

Provides a lightweight, cooperative cancellation token tied to a non-shared or locally-scoped std::inplace_stop_source, avoiding the heap-allocated control blocks of std::stop_token when lifetimes are well-nested. The inplace_stop_token references the stop-state of its associated std::inplace_stop_source object, if any.

The inplace_stop_token models the concept stoppable_token.

Member alias templates

Type Definition
callback_type<Callback> (since C++26) std::inplace_stop_callback<Callback>

Data members

Member Description
const std::inplace_stop_source* stop_source_ (private) a pointer to an associated object
(exposition-only member object*)

Member functions

constructs new inplace_stop_token object
(public member function) [edit]
(destructor)
(implicitly declared)
destructs the inplace_stop_token object
(public member function)
compares two inplace_stop_token objects
(public member function) [edit]
Modifiers
swaps two inplace_stop_token objects
(public member function) [edit]
Observers
checks whether the associated stop-state has been requested to stop
(public member function) [edit]
checks whether associated stop-state can be requested to stop
(public member function) [edit]

Notes

Feature-test macro Value Std Feature
__cpp_lib_senders 202406L (C++26) std::inplace_stop_token

Example

#include <chrono>
#include <iostream>
#include <stop_token>
#include <thread>
using namespace std::chrono_literals;

void perform_heavy_computation(std::inplace_stop_token stop_token)
{
    while (!stop_token.stop_requested())
    {
        std::cout << "Working...\n" << std::flush;
        std::this_thread::sleep_for(100ms);
    }
    std::cout << "Stopping due to cancellation!\n" << std::flush;
}

int main()
{
    std::inplace_stop_source stop_source;

    // Retrieve the token and pass it to a worker thread
    std::jthread worker(perform_heavy_computation, stop_source.get_token());

    std::this_thread::sleep_for(350ms);

    std::cout << "Requesting stop...\n" << std::flush;
    stop_source.request_stop();
} // worker thread joins automatically here

Possible output:

Working...
Working...
Working...
Working...
Requesting stop...
Stopping due to cancellation!

See also

an interface for querying if a std::jthread cancellation request has been made
(class) [edit]
a stoppable source that is the sole owner of its stop-state
(class) [edit]

External links

1.  NVIDIA: stdexec::inplace_stop_token — github.com
2.  Beman-project: execution::inplace_stop_token — github.com

Web Proxy Viewer  |  New URL  |  Original Page