MSTL 1.4.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
thread.hpp
浏览该文件的文档.
1#ifndef MSTL_CORE_ASYNC_THREAD_HPP__
2#define MSTL_CORE_ASYNC_THREAD_HPP__
3
10
15#ifdef MSTL_PLATFORM_LINUX__
16#include <pthread.h>
17#endif
19
25
30class MSTL_API thread {
31public:
36 struct id : ihashable<id> {
37 private:
41 using native_id_type =
42#ifdef MSTL_PLATFORM_WINDOWS__
43 ::DWORD;
44#else
45 ::pthread_t;
46#endif
47
48 native_id_type id_{};
49
50 friend class thread;
51
52 public:
56 id() noexcept = default;
57
62 explicit id(const native_id_type id) noexcept : id_(id) {}
63
68 MSTL_NODISCARD native_id_type native_handle() const noexcept { return id_; }
69
74 MSTL_NODISCARD size_t to_hash() const noexcept {
75 return _MSTL FNV_hash(reinterpret_cast<const byte_t*>(&id_), sizeof(id));
76 }
77
83 MSTL_NODISCARD bool operator ==(const id& rhs) const noexcept {
84#ifdef MSTL_PLATFORM_WINDOWS__
85 return id_ == rhs.id_;
86#else
87 return ::pthread_equal(id_, rhs.id_) != 0;
88#endif
89 }
90
96 MSTL_NODISCARD bool operator !=(const id& rhs) const noexcept {
97 return !(*this == rhs);
98 }
99 };
100
101private:
106 enum state {
107 NOT_A_THREAD,
108 CREATED,
109 JOINED,
110 DETACHED
111 };
112
119 struct data_base {
120 virtual ~data_base() = default;
121 virtual void run() = 0;
122 };
123
129 template <typename Callable>
130 struct thread_data final : data_base {
131 Callable func_;
132
138 template <typename F>
139 explicit thread_data(F&& f) : func_(_MSTL forward<F>(f)) {}
140
144 void run() override { func_(); }
145 };
146
147public:
152#ifdef MSTL_PLATFORM_WINDOWS__
153 ::HANDLE;
154#else
155 ::pthread_t;
156#endif
157
158private:
159 native_handle_type handle_{};
160 id id_{};
161 state state_ = NOT_A_THREAD;
162
163#ifdef MSTL_PLATFORM_WINDOWS__
164 static unsigned int __stdcall
165#else
166 static void*
167#endif
168 thread_entry(void* arg);
169
176 void start_thread_impl(void* args);
177
185 template <typename F>
186 void start_thread(F&& f) {
188 this->start_thread_impl(data.get());
189 data.release();
190 state_ = CREATED;
191 }
192
193public:
199 thread() noexcept = default;
200
213 template <typename F, typename... Args, typename = enable_if_t<!is_same_v<decay_t<F>, thread>>>
214 explicit thread(F&& f, Args&&... args) {
215 auto func = [func = _MSTL move(f), args = _MSTL make_tuple(_MSTL forward<Args>(args)...)]() mutable {
216 return _MSTL apply(_MSTL move(func), _MSTL move(args));
217 };
218 thread::start_thread(_MSTL move(func));
219 }
220
224 thread(const thread&) = delete;
225
229 thread& operator =(const thread&) = delete;
230
235 thread(thread&& other) noexcept;
236
242 thread& operator =(thread&& other) noexcept;
243
249
254 MSTL_NODISCARD id get_id() const noexcept { return id_; }
255
260 MSTL_NODISCARD native_handle_type native_handle() const noexcept { return handle_; }
261
268 MSTL_NODISCARD bool joinable() const noexcept { return state_ == CREATED; }
269
276 void join();
277
284 void detach();
285
290 void swap(thread& other) noexcept {
291 _MSTL swap(handle_, other.handle_);
292 _MSTL swap(id_, other.id_);
293 _MSTL swap(state_, other.state_);
294 }
295};
296 // Thread
298
300
306
311MSTL_ALWAYS_INLINE_INLINE thread::id id() noexcept {
312#ifdef MSTL_PLATFORM_WINDOWS__
313 return thread::id(::GetCurrentThreadId());
314#else
315 return thread::id(::pthread_self());
316#endif
317}
318 // Thread
320
322
324#endif // MSTL_CORE_ASYNC_THREAD_HPP__
MSTL元组应用函数
~thread()
析构函数
thread() noexcept=default
默认构造函数
void swap(thread &other) noexcept
交换两个线程对象
thread(const thread &)=delete
thread(thread &&other) noexcept
移动构造函数
::pthread_t native_handle_type
系统线程句柄类型
MSTL_NODISCARD bool joinable() const noexcept
检查线程是否可被等待
void join()
等待线程结束
MSTL_NODISCARD id get_id() const noexcept
获取线程标识符
void detach()
分离线程
MSTL_NODISCARD native_handle_type native_handle() const noexcept
获取原生句柄
MSTL异常处理框架
MSTL_NODISCARD constexpr T && forward(remove_reference_t< T > &x) noexcept
完美转发左值
unsigned char byte_t
字节类型,定义为无符号字符
MSTL_CONSTEXPR14 size_t FNV_hash(const byte_t *first, const size_t count) noexcept
FNV-1a哈希算法
bool operator!=(const function< Res(Args...)> &f, nullptr_t null) noexcept
不等于空指针比较
bool operator==(const function< Res(Args...)> &f, nullptr_t null) noexcept
等于空指针比较
#define _MSTL
全局命名空间MSTL前缀
#define MSTL_BEGIN_THIS_THREAD__
this_thread命名空间
#define MSTL_END_THIS_THREAD__
结束this_thread命名空间
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result)
移动范围元素
void swap()=delete
删除无参数的swap重载
MSTL_ALWAYS_INLINE_INLINE thread::id id() noexcept
获取当前线程标识符
MSTL_NODISCARD constexpr tuple< unwrap_ref_decay_t< Types >... > make_tuple(Types &&... args)
从参数创建元组
constexpr auto apply(Func &&f, Tuple &&t) noexcept(_INNER __apply_unpack_tuple< _MSTL is_nothrow_invocable, Func, Tuple >::value) -> decltype(auto)
将元组元素解包作为参数调用函数
typename decay< T >::type decay_t
decay的便捷别名
MSTL_NODISCARD MSTL_ALWAYS_INLINE constexpr decltype(auto) data(Container &cont) noexcept(noexcept(cont.data()))
获取容器的底层数据指针
typename enable_if< Test, T >::type enable_if_t
enable_if的便捷别名
MSTL_CONSTEXPR20 unique_ptr< T > make_unique(Args &&... args)
创建unique_ptr
可哈希对象接口模板
线程唯一标识符类
MSTL_NODISCARD native_id_type native_handle() const noexcept
获取原生线程ID
id() noexcept=default
默认构造函数
MSTL_NODISCARD size_t to_hash() const noexcept
计算哈希值
MSTL当前线程操作
MSTL独占智能指针