标准库标头 <stop_token> (C++20)
来自cppreference.com
此头文件是线程支持库的一部分。
类 | |
(C++20) |
用于查询是否已经做出 std::jthread 取消请求的接口 (类) |
(C++20) |
表示请求停止一个或多个 std::jthread 的类 (类) |
(C++20) |
用于在 std::jthread 取消上注册回调的接口 (类模板) |
(C++26) |
提供一种停止令牌接口,它始终不可能停止或请求停止 (类) |
(C++26) |
停止令牌,指代其关联的 std::inplace_stop_source 对象的停止状态 (类) |
(C++26) |
一种作为停止状态唯一拥有者的 stoppable-source (类) |
(C++26) |
用于 std::inplace_stop_token 的停止回调 (类模板) |
别名模板 | |
(C++26) |
获得用于给定停止令牌类型的回调类型 (别名模板) |
概念 | |
(C++26) |
指定停止令牌的基本接口,其允许查询是否有停止请求及是否可能提出停止请求 (概念) |
(C++26) |
指定一种不允许停止的停止令牌 (概念) |
标签 | |
(C++20) |
用于 stop_source 的标签,指定构造时没有关联的停止状态 (标签) |
概要
namespace std {
// 停止令牌概念
template<class CallbackFn, class Token, class Init = CallbackFn>
concept /*stoppable-callback-for*/ = /* 见描述 */; // 仅用于阐释
template<class Token>
concept stoppable_token = /* 见描述 */;
template<class Token>
concept unstoppable_token = /* 见描述 */;
template<class Source>
concept /*stoppable-source*/ = /* 见描述 */; // 仅用于阐释
// 类 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;
// 类 never_stop_token
class never_stop_token;
// 类 inplace_stop_token
class inplace_stop_token;
// 类 inplace_stop_source
class inplace_stop_source;
// 类模板 inplace_stop_callback
template<class CallbackFn>
class inplace_stop_callback;
template<class T, class CallbackFn>
using stop_callback_for_t = T::template callback_type<CallbackFn>;
}
停止令牌概念
namespace std {
template<class CallbackFn, class Token, class Init = CallbackFn>
concept /*stoppable-callback-for*/ = // 仅用于阐释
invocable<CallbackFn> &&
constructible_from<CallbackFn, Init> &&
requires { typename stop_callback_for_t<Token, CallbackFn>; } &&
constructible_from<stop_callback_for_t<Token, CallbackFn>, const Token&, Init>;
template<template<class> class>
struct /*check-type-alias-exists*/; // 仅用于阐释
template<class Token>
concept stoppable_token =
requires (const Token tok) {
typename /*check-type-alias-exists*/<Token::template callback_type>;
{ tok.stop_requested() } noexcept -> same_as<bool>;
{ tok.stop_possible() } noexcept -> same_as<bool>;
{ Token(tok) } noexcept; // 参见隐式表达式变体
} &&
copyable<Token> &&
equality_comparable<Token>;
template<class Token>
concept unstoppable_token =
stoppable_token<Token> &&
requires (const Token tok) {
requires bool_constant<(!tok.stop_possible())>::value;
};
template<class Source>
concept /*stoppable-source*/ = // 仅用于阐释
requires (Source& src, const Source csrc) {
{ csrc.get_token() } -> stoppable_token;
{ csrc.stop_possible() } noexcept -> same_as<bool>;
{ csrc.stop_requested() } noexcept -> same_as<bool>;
{ src.request_stop() } -> same_as<bool>;
};
}
类 std::stop_token
namespace std {
class stop_token {
public:
template<class CallbackFn>
using callback_type = stop_callback<CallbackFn>;
// 构造函数、复制与赋值
stop_token() noexcept = default;
// 成员函数
void swap(stop_token&) noexcept;
// 停止处理
bool stop_requested() const noexcept;
bool stop_possible() const noexcept;
friend bool operator==(const stop_token& lhs, const stop_token& rhs) noexcept;
friend void swap(stop_token& lhs, stop_token& rhs) noexcept;
private:
shared_ptr</* 未指明 */> stop_state_; // 仅用于阐释
};
}
类 std::stop_source
namespace std {
class stop_source {
public:
// 构造函数、复制与赋值
stop_source();
explicit stop_source(nostopstate_t) noexcept {}
// 成员函数
void swap(stop_source&) noexcept;
// 停止处理
stop_token get_token() const noexcept;
bool stop_possible() const noexcept;
bool stop_requested() const noexcept;
bool request_stop() noexcept;
friend bool
operator==(const stop_source& lhs, const stop_source& rhs) noexcept;
friend void swap(stop_source& lhs, stop_source& rhs) noexcept;
private:
shared_ptr</* 未指明 */> stop_state_; // 仅用于阐释
};
}
类模板 std::stop_callback
namespace std {
template<class CallbackFn>
class stop_callback {
public:
using callback_type = CallbackFn;
// 构造函数与析构函数
template<class Init>
explicit stop_callback(const stop_token& st, Init&& init)
noexcept(is_nothrow_constructible_v<CallbackFn, Init>);
template<class Init>
explicit stop_callback(stop_token&& st, Init&& init)
noexcept(is_nothrow_constructible_v<CallbackFn, Init>);
~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:
CallbackFn callback_fn_; // 仅用于阐释
};
template<class CallbackFn>
stop_callback(stop_token, CallbackFn) -> stop_callback<CallbackFn>;
}
类模板 std::never_stop_token
namespace std {
class never_stop_token {
struct /*callback-type*/ { // 仅用于阐释
explicit /*callback-type*/(never_stop_token, auto&&) noexcept {}
};
public:
template<class>
using callback_type = /*callback-type*/;
static constexpr bool stop_requested() noexcept { return false; }
static constexpr bool stop_posible() noexcept { return false; }
bool operator==(const never_stop_token&) const = default;
};
}
类模板 std::inplace_stop_token
namespace std {
class inplace_stop_token {
public:
template<class CallbackFn>
using callback_type = inplace_stop_callback<CallbackFn>;
inplace_stop_token() = default;
bool operator==(const inplace_stop_token&) const = default;
// 成员函数
bool stop_requested() const noexcept;
bool stop_possible() const noexcept;
void swap(inplace_stop_token&) noexcept;
private:
const inplace_stop_source* stop_source_ = nullptr; // 仅用于阐释
};
}
类模板 std::inplace_stop_source
namespace std {
class inplace_stop_source {
// 构造函数、赋值与析构函数
constexpr inplace_stop_source() noexcept;
inplace_stop_source(inplace_stop_source&&) = delete;
inplace_stop_source(const inplace_stop_source&) = delete;
inplace_stop_source& operator=(inplace_stop_source&&) = delete;
inplace_stop_source& operator=(const inplace_stop_source&) = delete;
~inplace_stop_source();
// 停止处理
constexpr inplace_stop_token get_token() const noexcept;
static constexpr bool stop_possible() noexcept { return true; }
bool stop_requested() const noexcept;
bool request_stop() noexcept;
};
}
类模板 std::inplace_stop_callback
namespace std {
template<class CallbackFn>
class inplace_stop_callback {
public:
using callback_type = CallbackFn;
// 构造函数与析构函数
template<class Init>
explicit inplace_stop_callback(inplace_stop_token st, Init&& init)
noexcept(is_nothrow_constructible_v<CallbackFn, Init>);
~inplace_stop_callback();
inplace_stop_callback(const inplace_stop_callback&) = delete;
inplace_stop_callback(inplace_stop_callback&&) = delete;
inplace_stop_callback& operator=(const inplace_stop_callback&) = delete;
inplace_stop_callback& operator=(inplace_stop_callback&&) = delete;
private:
CallbackFn callback_fn_; // 仅用于阐释
};
template<class CallbackFn>
inplace_stop_callback(inplace_stop_token, CallbackFn)
-> inplace_stop_callback<CallbackFn>;
}