Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/thread/stop callback"

From cppreference.com
< cpp‎ | thread
(created)
 
(links)
Line 6: Line 6:
 
}}
 
}}
  
The {{tt|stop_callback}} class template provides an RAII object type that registers a callback function for an associated {{tt|std::stop_token}} object, such that the callback function will be invoked when the {{tt|stop_token}}'s associated {{tt|std::stop_source}} is requested to stop.
+
The {{tt|stop_callback}} class template provides an RAII object type that registers a callback function for an associated {{ltt|cpp/thread/stop_token|std::stop_token}} object, such that the callback function will be invoked when the {{ltt|cpp/thread/stop_token|std::stop_token}}'s associated {{ltt|cpp/thread/stop_source|std::stop_source}} is requested to stop.
  
Callback functions registered via {{tt|stop_callback}}'s constructor are invoked either in the same thread that successfully invokes {{c|request_stop()}} for a {{tt|std::stop_source}} of the {{tt|stop_callback}}'s associated {{tt|stop_token}}; or if stop has already been requested prior to the constructor's registration, then the callback is invoked in the thread constructing the {{tt|stop_callback}}.
+
Callback functions registered via {{tt|stop_callback}}'s constructor are invoked either in the same thread that successfully invokes {{c|request_stop()}} for a {{ltt|cpp/thread/stop_source|std::stop_source}} of the {{tt|stop_callback}}'s associated {{ltt|cpp/thread/stop_token|std::stop_token}}; or if stop has already been requested prior to the constructor's registration, then the callback is invoked in the thread constructing the {{tt|stop_callback}}.
  
More than one {{tt|stop_callback}} can be created for the same {{tt|stop_token}}, from the same or different threads concurrently. No guarantee is provided for the order in which they will be executed, but they will be invoked synchronously; except for {{tt|stop_callback}}(s) constructed after stop has already been requested for the {{tt|stop_token}}, as described previously.
+
More than one {{tt|stop_callback}} can be created for the same {{ltt|cpp/thread/stop_token|std::stop_token}}, from the same or different threads concurrently. No guarantee is provided for the order in which they will be executed, but they will be invoked synchronously; except for {{tt|stop_callback}}(s) constructed after stop has already been requested for the {{ltt|cpp/thread/stop_token|std::stop_token}}, as described previously.
  
 
If an invocation of a callback exits via an exception then {{lc|std::terminate}} is called.
 
If an invocation of a callback exits via an exception then {{lc|std::terminate}} is called.

Revision as of 01:59, 14 December 2019

 
 
Concurrency support library
Threads
(C++11)
(C++20)
this_thread namespace
(C++11)
(C++11)
(C++11)
Cooperative cancellation
(C++20)
stop_callback
(C++20)
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
(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
Free functions for atomic operations
Free functions for atomic flags
 
 
Defined in header <stop_token>
template< class Callback >
class stop_callback;
(since C++20)

The stop_callback class template provides an RAII object type that registers a callback function for an associated std::stop_token object, such that the callback function will be invoked when the std::stop_token's associated std::stop_source is requested to stop.

Callback functions registered via stop_callback's constructor are invoked either in the same thread that successfully invokes request_stop() for a std::stop_source of the stop_callback's associated std::stop_token; or if stop has already been requested prior to the constructor's registration, then the callback is invoked in the thread constructing the stop_callback.

More than one stop_callback can be created for the same std::stop_token, from the same or different threads concurrently. No guarantee is provided for the order in which they will be executed, but they will be invoked synchronously; except for stop_callback(s) constructed after stop has already been requested for the std::stop_token, as described previously.

If an invocation of a callback exits via an exception then std::terminate is called.

std::stop_callback is not CopyConstructible, CopyAssignable, MoveConstructible, nor MoveAssignable.

The template param Callback type must be both invocable and destructible. Any return value is ignored.

Member types

Type Definition
callback_type Callback

Member functions

constructs new stop_callback object
(public member function) [edit]
destructs the stop_callback object
(public member function) [edit]

Example