Console Library 8.0.0
A header-only library that makes C++ simple
Loading...
Searching...
No Matches
thread.h
Go to the documentation of this file.
1
8
9/*
10Copyright (c) 2026 MrXie1109
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files (the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions:
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29*/
30
31#pragma once
32#include <memory>
33#include <thread>
34
35#include "../core/sfinae.h"
36#include "./event.h"
37
38namespace console {
44 class Thread {
45 std::unique_ptr<Event> event_;
46 std::thread thread_;
47
56 template <class F, class... Args>
57 std::thread make_thread(std::true_type, F &&f, Args &&...args) {
58 return std::thread(std::forward<F>(f),
59 std::cref(*event_),
60 std::forward<Args>(args)...);
61 }
62
71 template <class F, class... Args>
72 std::thread make_thread(std::false_type, F &&f, Args &&...args) {
73 return std::thread(std::forward<F>(f), std::forward<Args>(args)...);
74 }
75
76 public:
81 Thread() = default;
82
92 template <class F, class... Args>
93 Thread(F &&f, Args &&...args) :
94 event_(is_callable<F, const Event &, Args...>::value ? new Event()
95 : nullptr),
96 thread_(make_thread(is_callable<F, const Event &, Args...>{},
97 std::forward<F>(f),
98 std::forward<Args>(args)...)) {}
99
102 stop();
103 if (thread_.joinable()) thread_.join();
104 }
105
107 void join() {
108 if (thread_.joinable()) thread_.join();
109 }
110
112 void detach() {
113 if (thread_.joinable()) thread_.detach();
114 }
115
117 void stop() {
118 if (event_) event_->set();
119 }
120
122 bool joinable() const { return thread_.joinable(); }
123
125 std::thread::id get_id() const { return thread_.get_id(); }
126
128 auto native_handle() -> decltype(thread_.native_handle()) {
129 return thread_.native_handle();
130 }
131
133 Thread(const Thread &) = delete;
135 Thread &operator=(const Thread &) = delete;
136
142 event_(std::move(t.event_)), thread_(std::move(t.thread_)) {}
143
149 Thread &operator=(Thread &&t) noexcept {
150 if (this == &t) return *this;
151 stop();
152 if (thread_.joinable()) thread_.join();
153 event_ = std::move(t.event_);
154 thread_ = std::move(t.thread_);
155 return *this;
156 }
157
162 void swap(Thread &t) noexcept {
163 std::swap(thread_, t.thread_);
164 std::swap(event_, t.event_);
165 }
166
172 friend void swap(Thread &lhs, Thread &rhs) noexcept { lhs.swap(rhs); }
173 };
174}
线程同步事件类,用于线程间通信。
Definition event.h:42
Thread()=default
默认构造函数,创建线程对象但不启动线程。
~Thread()
析构函数,设置事件标志并等待线程结束。
Definition thread.h:101
void join()
等待线程结束。
Definition thread.h:107
void detach()
分离线程。
Definition thread.h:112
std::thread make_thread(std::true_type, F &&f, Args &&...args)
true_type分支,用于处理带有const Event &参数的可调用对象。
Definition thread.h:57
Thread(const Thread &)=delete
禁止拷贝构造。
Thread(Thread &&t)
移动构造。
Definition thread.h:141
Thread & operator=(const Thread &)=delete
禁止拷贝赋值。
void swap(Thread &t) noexcept
交换线程对象。
Definition thread.h:162
std::thread thread_
线程对象
Definition thread.h:46
Thread & operator=(Thread &&t) noexcept
移动赋值。
Definition thread.h:149
friend void swap(Thread &lhs, Thread &rhs) noexcept
交换两个线程对象。
Definition thread.h:172
std::unique_ptr< Event > event_
指向事件标志的指针
Definition thread.h:45
void stop()
停止线程。
Definition thread.h:117
bool joinable() const
判断线程是否可连接。
Definition thread.h:122
std::thread make_thread(std::false_type, F &&f, Args &&...args)
false_type分支,用于处理不带const Event &参数的可调用对象。
Definition thread.h:72
std::thread::id get_id() const
获取线程ID。
Definition thread.h:125
auto native_handle() -> decltype(thread_.native_handle())
获取线程的原生句柄。
Definition thread.h:128
Thread(F &&f, Args &&...args)
构造函数,创建线程对象并启动线程。
Definition thread.h:93
提供线程同步的 Event 类,支持设置、清除、等待和超时等待等操作。
is_callable_impl< F(Args...), void > is_callable
Definition sfinae.h:82
本库所有组件所在的顶层命名空间。
提供编译期类型特征检测(SFINAE 工具), 用于判断容器、可调用对象、迭代器、下标访问、字符串、可打印类型、字符类型等。