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