NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
scoped_thread.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_ASYNC_SCOPED_THREAD_HPP__
2#define NEFORCE_CORE_ASYNC_SCOPED_THREAD_HPP__
3
10
12NEFORCE_BEGIN_NAMESPACE__
13
19
25
35public:
36 using id = thread::id;
38
39private:
40 template <typename Callable, typename Object, typename... Args>
41 static constexpr bool pmf_expects_stop_token =
43 is_invocable<Callable, Object, stop_token, Args...>>;
44
45 stop_source stop_source_{none};
46 thread thread_{};
47
48 template <typename Callable, typename Object, typename... Args,
49 enable_if_t<pmf_expects_stop_token<Callable, Args...>, int> = 0>
50 static thread create(stop_source& source, Callable func, Object&& object, Args&&... args) {
51 return thread{func, _NEFORCE forward<Object>(object), source.get_token(), _NEFORCE forward<Args>(args)...};
52 }
53
54 template <typename Callable, typename... Args,
55 enable_if_t<!pmf_expects_stop_token<Callable, Args...> &&
57 int> = 0>
58 static thread create(stop_source& source, Callable func, Args&&... args) {
59 return thread{_NEFORCE forward<Callable>(func), source.get_token(), _NEFORCE forward<Args>(args)...};
60 }
61
62 template <typename Callable, typename... Args,
63 enable_if_t<!pmf_expects_stop_token<Callable, Args...> &&
65 int> = 0>
66 static thread create(stop_source&, Callable func, Args&&... args) {
68 "jthread arguments must be invocable after conversion to rvalues");
69 return thread{_NEFORCE forward<Callable>(func), _NEFORCE forward<Args>(args)...};
70 }
71
72public:
78 scoped_thread() noexcept = default;
79
89 template <typename Callable, typename... Args,
90 typename = enable_if_t<!is_same_v<remove_cvref_t<Callable>, scoped_thread>>>
91 explicit scoped_thread(Callable&& func, Args&&... args) :
92 thread_{this->create(stop_source_, _NEFORCE forward<Callable>(func), _NEFORCE forward<Args>(args)...)} {}
93
94 scoped_thread(const scoped_thread&) = delete;
96
101 scoped_thread(scoped_thread&& other) noexcept = default;
102
109 scoped_thread(move(other)).swap(*this);
110 return *this;
111 }
112
119 if (joinable()) {
120 request_stop();
121 join();
122 }
123 }
124
129 void swap(scoped_thread& other) noexcept {
130 _NEFORCE swap(stop_source_, other.stop_source_);
131 _NEFORCE swap(thread_, other.thread_);
132 }
133
138 NEFORCE_NODISCARD bool joinable() const noexcept { return thread_.joinable(); }
139
145 void join() { thread_.join(); }
146
153 void detach() { thread_.detach(); }
154
159 NEFORCE_NODISCARD id get_id() const noexcept { return thread_.get_id(); }
160
165 NEFORCE_NODISCARD native_handle_type native_handle() const { return thread_.native_handle(); }
166
171 NEFORCE_NODISCARD stop_source get_stop_source() noexcept { return stop_source_; }
172
177 NEFORCE_NODISCARD stop_token get_stop_token() const noexcept { return stop_source_.get_token(); }
178
185 bool request_stop() noexcept { return stop_source_.request_stop(); }
186};
187 // Thread
189 // AsyncComponents
191
192NEFORCE_END_NAMESPACE__
193#endif // NEFORCE_CORE_ASYNC_SCOPED_THREAD_HPP__
void swap(scoped_thread &other) noexcept
交换两个scoped_thread对象
scoped_thread & operator=(const scoped_thread &)=delete
禁止拷贝赋值
scoped_thread & operator=(scoped_thread &&other) noexcept
移动赋值运算符
void detach()
分离线程
NEFORCE_NODISCARD native_handle_type native_handle() const
获取原生线程句柄
NEFORCE_NODISCARD stop_source get_stop_source() noexcept
获取停止源
NEFORCE_NODISCARD id get_id() const noexcept
获取线程ID
NEFORCE_NODISCARD bool joinable() const noexcept
检查线程是否可被等待
NEFORCE_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()
等待线程结束
~scoped_thread()
析构函数
scoped_thread(const scoped_thread &)=delete
禁止拷贝构造
thread::native_handle_type native_handle_type
原生句柄类型
NEFORCE_NODISCARD stop_token get_token() const noexcept
获取停止令牌
停止令牌类
线程类
::pthread_t native_handle_type
系统线程句柄类型
NEFORCE_NODISCARD constexpr T && forward(remove_reference_t< T > &x) noexcept
完美转发左值
NEFORCE_INLINE17 constexpr bool is_invocable_v
is_invocable的便捷变量模板
NEFORCE_INLINE17 constexpr none_t none
默认空表示
typename remove_cvref< T >::type remove_cvref_t
remove_cvref的便捷别名
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result) noexcept(noexcept(inner::__move_aux(first, last, result)))
移动范围元素
void swap()=delete
删除无参数的swap重载
typename decay< T >::type decay_t
decay的便捷别名
NEFORCE_INLINE17 constexpr bool is_same_v
is_same的便捷变量模板
NEFORCE_INLINE17 constexpr bool conjunction_v
conjunction的便捷变量模板
typename enable_if< Test, T >::type enable_if_t
enable_if的便捷别名
停止令牌实现
判断类型是否可调用
线程唯一标识符类