标准库头文件 <stop_token>

来自cppreference.com
< cpp‎ | header
 
 
 

此头文件是线程支持库的一部分。

查询是否已经做出 std::jthread 取消请求的接口
(类)
表示请求停止一个或多个 std::jthread 的类
(类)
std::jthread 取消上注册回调的接口
(类模板)
用于 stop_source 构造函数的占位符类型
(类)

常量

用于 stop_source 构造函数的 std::nostopstate_t 实例
(常量)

概要

namespace std {
  // 类 stop_token
  class stop_token;
 
  // 类 stop_source
  class stop_source;
 
  // 无共享停止状态指示器
  struct nostopstate_t {
    explicit nostopstate_t() = default;
  };
  inline constexpr nostopstate_t nostopstate{};
 
  // 类 stop_callback
  template<class Callback>
  class stop_callback;
}

std::stop_token

namespace std {
  class stop_token {
  public:
    // 构造函数、复制与赋值
    stop_token() noexcept;
 
    stop_token(const stop_token&) noexcept;
    stop_token(stop_token&&) noexcept;
    stop_token& operator=(const stop_token&) noexcept;
    stop_token& operator=(stop_token&&) noexcept;
    ~stop_token();
    void swap(stop_token&) noexcept;
 
    // 停止处理
    [[nodiscard]] bool stop_requested() const noexcept;
    [[nodiscard]] bool stop_possible() const noexcept;
 
    [[nodiscard]]
    friend bool operator==(const stop_token& lhs, const stop_token& rhs) noexcept;
    friend void swap(stop_token& lhs, stop_token& rhs) noexcept;
  };
}

std::stop_source

namespace std {
  // 无共享停止状态指示器
  struct nostopstate_t {
    explicit nostopstate_t() = default;
  };
  inline constexpr nostopstate_t nostopstate{};
 
  class stop_source {
  public:
    // 构造函数、复制与赋值
    stop_source();
    explicit stop_source(nostopstate_t) noexcept;
 
    stop_source(const stop_source&) noexcept;
    stop_source(stop_source&&) noexcept;
    stop_source& operator=(const stop_source&) noexcept;
    stop_source& operator=(stop_source&&) noexcept;
    ~stop_source();
    void swap(stop_source&) noexcept;
 
    // 停止处理
    [[nodiscard]] stop_token get_token() const noexcept;
    [[nodiscard]] bool stop_possible() const noexcept;
    [[nodiscard]] bool stop_requested() const noexcept;
    bool request_stop() noexcept;
 
    [[nodiscard]] friend bool
    operator==(const stop_source& lhs, const stop_source& rhs) noexcept;
    friend void swap(stop_source& lhs, stop_source& rhs) noexcept;
  };
}

类模板 std::stop_callback

namespace std {
  template<class Callback>
  class stop_callback {
  public:
    using callback_type = Callback;
 
    // 构造函数与析构函数
    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;      // 仅用于阐释
  };
 
  template<class Callback>
  stop_callback(stop_token, Callback) -> stop_callback<Callback>;
}