Console Library
8.0.0
A header-only library that makes C++ simple
Toggle main menu visibility
Loading...
Searching...
No Matches
event.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 <condition_variable>
33
#include <mutex>
34
35
#include "
../util/time.h
"
36
37
namespace
console
{
42
class
Event
{
43
bool
flag_
;
44
mutable
std::mutex
mutex_
;
45
mutable
std::condition_variable
cv_
;
46
47
public
:
49
Event
() :
flag_
(false) {}
50
52
void
set
() {
53
std::unique_lock<std::mutex> lock(
mutex_
);
54
flag_
=
true
;
55
cv_
.notify_all();
56
}
57
59
void
clear
() {
60
std::unique_lock<std::mutex> lock(
mutex_
);
61
flag_
=
false
;
62
}
63
65
void
wait
()
const
{
66
std::unique_lock<std::mutex> lock(
mutex_
);
67
cv_
.wait(lock, [
this
]() {
return
flag_
; });
68
}
69
75
template
<
class
Rep,
class
Period>
76
bool
wait
(
const
std::chrono::duration<Rep, Period> &duration)
const
{
77
std::unique_lock<std::mutex> lock(
mutex_
);
78
return
cv_
.wait_for(lock, duration, [
this
]() {
return
flag_
; });
79
}
80
86
bool
wait
(
Time
timeout)
const
{
87
return
wait
(std::chrono::nanoseconds(timeout.
count
()));
88
}
89
95
bool
wait
(
double
seconds
)
const
{
96
return
wait
(std::chrono::duration<double>(
seconds
));
97
}
98
103
bool
is_set
()
const
{
104
std::unique_lock<std::mutex> lock(
mutex_
);
105
return
flag_
;
106
}
107
109
Event
(
const
Event
&) =
delete
;
111
Event
&
operator=
(
const
Event
&) =
delete
;
113
Event
(
Event
&&other) =
delete
;
115
Event
&
operator=
(
Event
&&other) =
delete
;
116
};
117
}
console::Event::flag_
bool flag_
事件标志,用于线程间通信
Definition
event.h:43
console::Event::clear
void clear()
清除事件标志为 false。
Definition
event.h:59
console::Event::Event
Event()
默认构造函数,初始化事件标志为 false。
Definition
event.h:49
console::Event::Event
Event(Event &&other)=delete
禁止移动构造。
console::Event::is_set
bool is_set() const
检查事件标志是否为 true。
Definition
event.h:103
console::Event::cv_
std::condition_variable cv_
条件变量,用于线程间同步
Definition
event.h:45
console::Event::wait
bool wait(const std::chrono::duration< Rep, Period > &duration) const
等待事件标志变为 true,带有超时机制。
Definition
event.h:76
console::Event::set
void set()
设置事件标志为 true,并通知所有等待的线程。
Definition
event.h:52
console::Event::mutex_
std::mutex mutex_
互斥锁,保护事件标志
Definition
event.h:44
console::Event::wait
void wait() const
等待事件标志变为 true。
Definition
event.h:65
console::Event::operator=
Event & operator=(Event &&other)=delete
禁止移动赋值。
console::Event::Event
Event(const Event &)=delete
禁止拷贝构造。
console::Event::operator=
Event & operator=(const Event &)=delete
禁止拷贝赋值。
console::Event::wait
bool wait(double seconds) const
等待事件标志变为 true,带有超时机制。
Definition
event.h:95
console::Event::wait
bool wait(Time timeout) const
等待事件标志变为 true,带有超时机制。
Definition
event.h:86
console::Time
表示以纳秒为单位的时间量,支持单位转换、算术运算和自动选择合适的输出单位。
Definition
time.h:57
console::Time::count
intmax_t count() const
获取纳秒数。
Definition
time.h:68
console
本库所有组件所在的顶层命名空间。
console::seconds
Time seconds(double n)
静态函数,构造秒。
Definition
time.h:178
time.h
提供时间度量、计时、休眠和日期时间格式化功能。
include
async
event.h
Generated on
for Console Library by
1.18.0