NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
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
42class NEFORCE_API lazy_thread {
43public:
44 using id = thread::id;
46
47private:
48 function<void()> func_;
49 thread thread_;
50
51public:
55 lazy_thread() noexcept = default;
56
66 template <typename F, typename... Args>
67 explicit lazy_thread(F&& f, Args&&... args) {
68 func_ = [func = _NEFORCE forward<F>(f), args = _NEFORCE make_tuple(_NEFORCE forward<Args>(args)...)]() mutable {
69 _NEFORCE apply(_NEFORCE move(func), _NEFORCE move(args));
70 };
71 }
72
73 lazy_thread(const lazy_thread&) = delete;
74 lazy_thread& operator=(const lazy_thread&) = delete;
75
82 lazy_thread(lazy_thread&& other) noexcept;
83
91 lazy_thread& operator=(lazy_thread&& other) noexcept;
92
99
107 void start();
108
113 bool joinable() const noexcept { return thread_.joinable(); }
114
121 void join() { thread_.join(); }
122
129 void detach() { thread_.detach(); }
130
135 thread::id get_id() const noexcept { return thread_.get_id(); }
136
141 void swap(lazy_thread& other) noexcept {
142 _NEFORCE swap(func_, other.func_);
143 _NEFORCE swap(thread_, other.thread_);
144 }
145};
146 // Thread
148 // AsyncComponents
150
151NEFORCE_END_NAMESPACE__
152#endif // NEFORCE_CORE_ASYNC_LAZY_THREAD_HPP__
函数包装器主模板声明
延迟启动线程类
bool joinable() const noexcept
检查线程是否可被等待
thread::id get_id() const noexcept
获取线程标识符
lazy_thread & operator=(lazy_thread &&other) noexcept
移动赋值运算符
void swap(lazy_thread &other) noexcept
交换两个延迟线程对象
thread::native_handle_type native_handle_type
原生句柄类型
void join()
等待线程结束
void detach()
分离线程
thread::id id
线程ID类型
lazy_thread() noexcept=default
默认构造函数
~lazy_thread()
析构函数
lazy_thread(lazy_thread &&other) noexcept
移动构造函数
void start()
启动线程
线程类
::pthread_t native_handle_type
系统线程句柄类型
通用函数包装器
NEFORCE_NODISCARD 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)))
移动范围元素
void swap()=delete
删除无参数的swap重载
NEFORCE_NODISCARD constexpr tuple< unwrap_ref_decay_t< Types >... > make_tuple(Types &&... args)
从参数创建元组
constexpr decltype(auto) apply(Func &&f, Tuple &&t) noexcept(inner::__apply_unpack_tuple< _NEFORCE is_nothrow_invocable, Func, Tuple >::value)
将元组元素解包作为参数调用函数
线程唯一标识符类
线程管理类