MSTL 1.4.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
scoped_thread.hpp
浏览该文件的文档.
1#ifndef MSTL_CORE_ASYNC_SCOPED_THREAD_HPP__
2#define MSTL_CORE_ASYNC_SCOPED_THREAD_HPP__
3
10
13
19
29public:
30 using id = thread::id;
32
33private:
34 template <typename Callable, typename Object, typename... Args>
35 static constexpr bool pmf_expects_stop_token = conjunction_v<
37 is_invocable<Callable, Object, stop_token, Args...>>;
38
39 stop_source stop_source_{none};
40 thread thread_{};
41
53 template <typename Callable, typename Object, typename... Args, enable_if_t<
54 pmf_expects_stop_token<Callable, Args...>, int> = 0>
55 static thread create(stop_source& source, Callable func, Object&& object, Args&&... args) {
56 return thread{func, _MSTL forward<Object>(object), source.get_token(), _MSTL forward<Args>(args)...};
57 }
58
68 template <typename Callable, typename... Args, enable_if_t<
69 !pmf_expects_stop_token<Callable, Args...> &&
70 is_invocable_v<decay_t<Callable>, stop_token, decay_t<Args>...>, int> = 0>
71 static thread create(stop_source& source, Callable func, Args&&... args) {
72 return thread{_MSTL forward<Callable>(func), source.get_token(), _MSTL forward<Args>(args)...};
73 }
74
83 template <typename Callable, typename... Args, enable_if_t<
84 !pmf_expects_stop_token<Callable, Args...> &&
85 !is_invocable_v<decay_t<Callable>, stop_token, decay_t<Args>...>, int> = 0>
86 static thread create(stop_source&, Callable func, Args&&... args) {
87 static_assert(
88 is_invocable_v<decay_t<Callable>, decay_t<Args>...>,
89 "jthread arguments must be invocable after conversion to rvalues");
90 return thread{_MSTL forward<Callable>(func), _MSTL forward<Args>(args)...};
91 }
92
93public:
99 scoped_thread() noexcept = default;
100
110 template <typename Callable, typename... Args, typename = enable_if_t<
111 !is_same_v<remove_cvref_t<Callable>, scoped_thread>>>
112 explicit scoped_thread(Callable&& func, Args&&... args)
113 : thread_{this->create(stop_source_, _MSTL forward<Callable>(func), _MSTL forward<Args>(args)...)} {}
114
115 scoped_thread(const scoped_thread&) = delete;
117
122 scoped_thread(scoped_thread&& other) noexcept = default;
123
130 scoped_thread(move(other)).swap(*this);
131 return *this;
132 }
133
140 if (joinable()) {
141 request_stop();
142 join();
143 }
144 }
145
150 void swap(scoped_thread& other) noexcept {
151 _MSTL swap(stop_source_, other.stop_source_);
152 _MSTL swap(thread_, other.thread_);
153 }
154
159 MSTL_NODISCARD bool joinable() const noexcept {
160 return thread_.joinable();
161 }
162
168 void join() {
169 thread_.join();
170 }
171
178 void detach() {
179 thread_.detach();
180 }
181
186 MSTL_NODISCARD id get_id() const noexcept {
187 return thread_.get_id();
188 }
189
194 MSTL_NODISCARD native_handle_type native_handle() const {
195 return thread_.native_handle();
196 }
197
202 MSTL_NODISCARD stop_source get_stop_source() noexcept {
203 return stop_source_;
204 }
205
210 MSTL_NODISCARD stop_token get_stop_token() const noexcept {
211 return stop_source_.get_token();
212 }
213
220 bool request_stop() noexcept {
221 return stop_source_.request_stop();
222 }
223};
224 // Thread
226
228#endif // MSTL_CORE_ASYNC_SCOPED_THREAD_HPP__
MSTL_NODISCARD bool joinable() const noexcept
检查线程是否可被等待
void swap(scoped_thread &other) noexcept
交换两个scoped_thread对象
scoped_thread & operator=(const scoped_thread &)=delete
禁止拷贝赋值
void detach()
分离线程
MSTL_NODISCARD stop_token get_stop_token() const noexcept
获取停止令牌
bool request_stop() noexcept
请求线程停止
thread::id id
线程ID类型
scoped_thread() noexcept=default
默认构造函数
scoped_thread(scoped_thread &&other) noexcept=default
移动构造函数
void join()
等待线程结束
MSTL_NODISCARD id get_id() const noexcept
获取线程ID
MSTL_NODISCARD native_handle_type native_handle() const
获取原生线程句柄
~scoped_thread()
析构函数
scoped_thread(const scoped_thread &)=delete
禁止拷贝构造
thread::native_handle_type native_handle_type
原生句柄类型
MSTL_NODISCARD stop_source get_stop_source() noexcept
获取停止源
MSTL_NODISCARD stop_token get_token() const noexcept
获取停止令牌
停止令牌类
线程类
::pthread_t native_handle_type
系统线程句柄类型
MSTL_NODISCARD constexpr T && forward(remove_reference_t< T > &x) noexcept
完美转发左值
#define _MSTL
全局命名空间MSTL前缀
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL
MSTL_INLINE17 constexpr none_t none
默认空表示
typename remove_cvref< T >::type remove_cvref_t
remove_cvref的便捷别名
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result)
移动范围元素
void swap()=delete
删除无参数的swap重载
typename decay< T >::type decay_t
decay的便捷别名
typename enable_if< Test, T >::type enable_if_t
enable_if的便捷别名
MSTL停止令牌实现
判断类型是否可调用
判断类型是否为成员函数指针
线程唯一标识符类