1#ifndef MSTL_CORE_ASYNC_THREAD_HPP__
2#define MSTL_CORE_ASYNC_THREAD_HPP__
15#ifdef MSTL_PLATFORM_LINUX__
41 using native_id_type =
42#ifdef MSTL_PLATFORM_WINDOWS__
56 id() noexcept = default;
62 explicit
id(const native_id_type
id) noexcept : id_(
id) {}
68 MSTL_NODISCARD native_id_type
native_handle() const noexcept {
return id_; }
74 MSTL_NODISCARD
size_t to_hash() const noexcept {
83 MSTL_NODISCARD
bool operator ==(
const id& rhs)
const noexcept {
84#ifdef MSTL_PLATFORM_WINDOWS__
85 return id_ == rhs.id_;
87 return ::pthread_equal(id_, rhs.id_) != 0;
96 MSTL_NODISCARD
bool operator !=(
const id& rhs)
const noexcept {
97 return !(*
this == rhs);
120 virtual ~data_base() =
default;
121 virtual void run() = 0;
129 template <
typename Callable>
130 struct thread_data final : data_base {
138 template <
typename F>
139 explicit thread_data(F&& f) : func_(
_MSTL forward<F>(f)) {}
144 void run()
override { func_(); }
152#ifdef MSTL_PLATFORM_WINDOWS__
161 state state_ = NOT_A_THREAD;
163#ifdef MSTL_PLATFORM_WINDOWS__
164 static unsigned int __stdcall
168 thread_entry(
void* arg);
176 void start_thread_impl(
void* args);
185 template <
typename F>
186 void start_thread(F&& f) {
188 this->start_thread_impl(
data.get());
254 MSTL_NODISCARD
id get_id() const noexcept {
return id_; }
268 MSTL_NODISCARD
bool joinable() const noexcept {
return state_ == CREATED; }
312#ifdef MSTL_PLATFORM_WINDOWS__
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
检查线程是否可被等待
MSTL_NODISCARD id get_id() const noexcept
获取线程标识符
MSTL_NODISCARD native_handle_type native_handle() const noexcept
获取原生句柄
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
计算哈希值