NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
载入中...
搜索中...
未找到
lazy_thread.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_ASYNC_LAZY_THREAD_HPP__
2#define NEFORCE_CORE_ASYNC_LAZY_THREAD_HPP__
3
10
13NEFORCE_BEGIN_NAMESPACE__
14
20
26
40class NEFORCE_API lazy_thread {
41public:
42 using id = thread::id;
44
45private:
46 function<void()> func_;
47 thread thread_;
48
49public:
53 lazy_thread() noexcept = default;
54
64 template <typename F, typename... Args>
65 explicit lazy_thread(F&& f, Args&&... args) {
66 func_ = [func = _NEFORCE forward<F>(f), args = _NEFORCE make_tuple(_NEFORCE forward<Args>(args)...)]() mutable {
67 _NEFORCE apply(_NEFORCE move(func), _NEFORCE move(args));
68 };
69 }
70
71 lazy_thread(const lazy_thread&) = delete;
72 lazy_thread& operator=(const lazy_thread&) = delete;
73
80 lazy_thread(lazy_thread&& other) noexcept;
81
89 lazy_thread& operator=(lazy_thread&& other) noexcept;
90
97
105 void start();
106
111 NEFORCE_NODISCARD bool joinable() const noexcept { return thread_.joinable(); }
112
119 void join() { thread_.join(); }
120
127 void detach() { thread_.detach(); }
128
133 NEFORCE_NODISCARD thread::id get_id() const noexcept { return thread_.get_id(); }
134
139 void swap(lazy_thread& other) noexcept {
140 _NEFORCE swap(func_, other.func_);
141 _NEFORCE swap(thread_, other.thread_);
142 }
143};
144 // Thread
146 // AsyncComponents
148
149NEFORCE_END_NAMESPACE__
150#endif // NEFORCE_CORE_ASYNC_LAZY_THREAD_HPP__
函数包装器主模板声明
延迟启动线程类
thread::id id
线程ID类型
void start()
启动线程
void swap(lazy_thread &other) noexcept
交换两个延迟线程对象
void join()
等待线程结束
thread::id get_id() const noexcept
获取线程标识符
thread::native_handle_type native_handle_type
原生句柄类型
lazy_thread(lazy_thread &&other) noexcept
移动构造函数
void detach()
分离线程
lazy_thread & operator=(lazy_thread &&other) noexcept
移动赋值运算符
bool joinable() const noexcept
检查线程是否可被等待
lazy_thread() noexcept=default
默认构造函数
~lazy_thread()
析构函数
::HANDLE native_handle_type
系统线程句柄类型
通用函数包装器
constexpr T && forward(remove_reference_t< T > &x) noexcept
完美转发左值
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result) noexcept(noexcept(inner::__move_aux(first, last, result)))
移动范围元素
constexpr decltype(auto) apply(Func &&f, Tuple &&t) noexcept(inner::__apply_unpack_tuple< _NEFORCE is_nothrow_invocable, Func, Tuple >::value)
将元组元素解包作为参数调用函数
constexpr tuple< unwrap_ref_decay_t< Types >... > make_tuple(Types &&... args)
从参数创建元组
线程唯一标识符类
线程管理类