Console Library
8.0.0
A header-only library that makes C++ simple
Toggle main menu visibility
Loading...
Searching...
No Matches
sched.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 <atomic>
33
#include <chrono>
34
#include <condition_variable>
35
#include <mutex>
36
#include <thread>
37
#include <vector>
38
39
#include "
../util/time.h
"
40
41
namespace
console
{
51
class
Scheduler
{
52
std::mutex
mut
;
53
std::condition_variable
cv
;
54
std::atomic<bool>
canceled
{
55
false
};
56
std::vector<std::thread>
threads
;
57
58
public
:
69
template
<
class
Rep,
class
Period,
class
Func>
70
void
schedule
(
71
const
std::chrono::duration<Rep, Period> &duration, Func &&func) {
72
std::thread(
73
[
this
](std::chrono::duration<Rep, Period> duration, Func func) {
74
auto
deadline = std::chrono::steady_clock::now() + duration;
75
std::unique_lock<std::mutex> lock(
mut
);
76
bool
interrupted =
cv
.wait_until(lock, deadline, [
this
] {
77
return
canceled
.load(std::memory_order_acquire);
78
});
79
if
(!interrupted) {
80
lock.unlock();
81
func();
82
}
83
},
84
duration,
85
std::forward<Func>(func))
86
.detach();
87
}
88
94
template
<
class
Func>
95
void
schedule
(
Time
duration, Func &&func) {
96
schedule
(std::chrono::nanoseconds(duration.
count
()),
97
std::forward<Func>(func));
98
}
99
106
template
<
class
Func>
107
void
schedule
(
double
seconds
, Func &&func) {
108
schedule
(std::chrono::duration_cast<std::chrono::nanoseconds>(
109
std::chrono::duration<double>(
seconds
)),
110
std::forward<Func>(func));
111
}
112
123
template
<
class
Rep,
class
Period,
class
Func>
124
void
interval
(
125
const
std::chrono::duration<Rep, Period> &duration, Func &&func) {
126
mut
.lock();
127
threads
.push_back(std::thread(
128
[
this
](std::chrono::duration<Rep, Period> duration, Func func) {
129
std::unique_lock<std::mutex> lock(
mut
, std::adopt_lock);
130
auto
deadline = std::chrono::steady_clock::now();
131
while
(
true
) {
132
deadline += duration;
133
bool
interrupted =
cv
.wait_until(
134
lock, deadline, [
this
] {
135
return
canceled
.load(std::memory_order_acquire);
136
});
137
if
(interrupted)
break
;
138
lock.unlock();
139
func();
140
lock.lock();
141
}
142
},
143
duration,
144
std::forward<Func>(func)));
145
}
146
152
template
<
class
Func>
153
void
interval
(
Time
duration, Func &&func) {
154
interval
(std::chrono::nanoseconds(duration.
count
()),
155
std::forward<Func>(func));
156
}
157
164
template
<
class
Func>
165
void
interval
(
double
seconds
, Func &&func) {
166
interval
(std::chrono::duration_cast<std::chrono::nanoseconds>(
167
std::chrono::duration<double>(
seconds
)),
168
std::forward<Func>(func));
169
}
170
176
void
cancel_all
() {
177
canceled
.store(
true
, std::memory_order_release);
178
cv
.notify_all();
179
}
180
182
~Scheduler
() {
183
cancel_all
();
184
for
(
auto
&thread :
threads
)
185
if
(thread.joinable()) thread.join();
186
}
187
};
188
}
console::Scheduler
基于条件变量的简单调度器,用于延迟执行或周期性的后台任务。
Definition
sched.h:51
console::Scheduler::interval
void interval(const std::chrono::duration< Rep, Period > &duration, Func &&func)
按照固定间隔周期性地执行函数。
Definition
sched.h:124
console::Scheduler::mut
std::mutex mut
保护共享状态的互斥锁
Definition
sched.h:52
console::Scheduler::schedule
void schedule(const std::chrono::duration< Rep, Period > &duration, Func &&func)
在指定延时后执行一次函数。
Definition
sched.h:70
console::Scheduler::cancel_all
void cancel_all()
取消所有待执行 / 正在等待的任务,并唤醒所有等待线程。
Definition
sched.h:176
console::Scheduler::threads
std::vector< std::thread > threads
interval() 创建的持续运行线程集合
Definition
sched.h:56
console::Scheduler::schedule
void schedule(Time duration, Func &&func)
便捷重载:以 console::Time 指定延时后执行一次函数。
Definition
sched.h:95
console::Scheduler::canceled
std::atomic< bool > canceled
取消标志,置位后所有等待线程立即唤醒
Definition
sched.h:54
console::Scheduler::~Scheduler
~Scheduler()
析构函数,先取消所有任务,再等待所有 interval 线程退出。
Definition
sched.h:182
console::Scheduler::interval
void interval(Time duration, Func &&func)
便捷重载:以 console::Time 指定间隔周期性地执行函数。
Definition
sched.h:153
console::Scheduler::interval
void interval(double seconds, Func &&func)
便捷重载:以秒为单位指定间隔周期性地执行函数。
Definition
sched.h:165
console::Scheduler::cv
std::condition_variable cv
用于唤醒/取消等待线程的条件变量
Definition
sched.h:53
console::Scheduler::schedule
void schedule(double seconds, Func &&func)
便捷重载:以秒为单位指定延时后执行一次函数。
Definition
sched.h:107
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
sched.h
Generated on
for Console Library by
1.18.0