Console Library 8.0.0
A header-only library that makes C++ simple
Loading...
Searching...
No Matches
event.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 <condition_variable>
33#include <mutex>
34
35#include "../util/time.h"
36
37namespace 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}
bool flag_
事件标志,用于线程间通信
Definition event.h:43
void clear()
清除事件标志为 false。
Definition event.h:59
Event()
默认构造函数,初始化事件标志为 false。
Definition event.h:49
Event(Event &&other)=delete
禁止移动构造。
bool is_set() const
检查事件标志是否为 true。
Definition event.h:103
std::condition_variable cv_
条件变量,用于线程间同步
Definition event.h:45
bool wait(const std::chrono::duration< Rep, Period > &duration) const
等待事件标志变为 true,带有超时机制。
Definition event.h:76
void set()
设置事件标志为 true,并通知所有等待的线程。
Definition event.h:52
std::mutex mutex_
互斥锁,保护事件标志
Definition event.h:44
void wait() const
等待事件标志变为 true。
Definition event.h:65
Event & operator=(Event &&other)=delete
禁止移动赋值。
Event(const Event &)=delete
禁止拷贝构造。
Event & operator=(const Event &)=delete
禁止拷贝赋值。
bool wait(double seconds) const
等待事件标志变为 true,带有超时机制。
Definition event.h:95
bool wait(Time timeout) const
等待事件标志变为 true,带有超时机制。
Definition event.h:86
表示以纳秒为单位的时间量,支持单位转换、算术运算和自动选择合适的输出单位。
Definition time.h:57
intmax_t count() const
获取纳秒数。
Definition time.h:68
本库所有组件所在的顶层命名空间。
Time seconds(double n)
静态函数,构造秒。
Definition time.h:178
提供时间度量、计时、休眠和日期时间格式化功能。