32 Thread support library [thread]

32.3 Stop tokens [thread.stoptoken]

32.3.5 Class template stop_­callback [stopcallback]

namespace std {
  template<class Callback>
  class stop_callback {
  public:
    using callback_type = Callback;

    // [stopcallback.cons], constructors and destructor
    template<class C>
      explicit stop_callback(const stop_token& st, C&& cb)
        noexcept(is_nothrow_constructible_v<Callback, C>);
    template<class C>
      explicit stop_callback(stop_token&& st, C&& cb)
        noexcept(is_nothrow_constructible_v<Callback, C>);
    ~stop_callback();

    stop_callback(const stop_callback&) = delete;
    stop_callback(stop_callback&&) = delete;
    stop_callback& operator=(const stop_callback&) = delete;
    stop_callback& operator=(stop_callback&&) = delete;

  private:
    Callback callback;      // exposition only
  };

  template<class Callback>
    stop_callback(stop_token, Callback) -> stop_callback<Callback>;
}
Mandates: stop_­callback is instantiated with an argument for the template parameter Callback that satisfies both invocable and destructible.
Preconditions: stop_­callback is instantiated with an argument for the template parameter Callback that models both invocable and destructible.

32.3.5.1 Constructors and destructor [stopcallback.cons]

template<class C> explicit stop_callback(const stop_token& st, C&& cb) noexcept(is_nothrow_constructible_v<Callback, C>); template<class C> explicit stop_callback(stop_token&& st, C&& cb) noexcept(is_nothrow_constructible_v<Callback, C>);
Constraints: Callback and C satisfy constructible_­from<Callback, C>.
Preconditions: Callback and C model constructible_­from<Callback, C>.
Effects: Initializes callback with std​::​forward<C>(cb).
If st.stop_­requested() is true, then std​::​forward<Callback>(callback)() is evaluated in the current thread before the constructor returns.
Otherwise, if st has ownership of a stop state, acquires shared ownership of that stop state and registers the callback with that stop state such that std​::​forward<Callback>(callback)() is evaluated by the first call to request_­stop() on an associated stop_­source.
Remarks: If evaluating std​::​forward<Callback>(callback)() exits via an exception, then terminate is called ([except.terminate]).
Throws: Any exception thrown by the initialization of callback.
~stop_callback();
Effects: Unregisters the callback from the owned stop state, if any.
The destructor does not block waiting for the execution of another callback registered by an associated stop_­callback.
If callback is concurrently executing on another thread, then the return from the invocation of callback strongly happens before ([intro.races]) callback is destroyed.
If callback is executing on the current thread, then the destructor does not block ([defns.block]) waiting for the return from the invocation of callback.
Releases ownership of the stop state, if any.