Console Library
8.0.0
A header-only library that makes C++ simple
Toggle main menu visibility
Loading...
Searching...
No Matches
group.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 <condition_variable>
34
#include <exception>
35
#include <functional>
36
#include <mutex>
37
38
namespace
console
{
45
class
Group
{
46
int
count_
{0};
47
std::function<void()>
callback_
;
48
bool
done_
{
false
};
49
mutable
std::mutex
mutex_
;
50
mutable
std::condition_variable
cv_
;
51
53
static
void
no_op
() {}
54
55
public
:
61
Group
(
size_t
count = 0, std::function<
void
()>
callback
=
no_op
) :
62
count_
(count),
callback_
(
callback
) {}
63
68
~Group
() {
69
std::lock_guard<std::mutex> lock(
mutex_
);
70
if
(
count_
!= 0 || !
done_
) std::terminate();
71
}
72
78
void
add
(
size_t
count) {
79
std::lock_guard<std::mutex> lock(
mutex_
);
80
if
(
done_
) std::terminate();
81
count_
+= count;
82
}
83
89
void
done
() {
90
std::lock_guard<std::mutex> lock(
mutex_
);
91
if
(
count_
<= 0) std::terminate();
92
if
(--
count_
== 0) {
93
done_
=
true
;
94
callback_
();
95
cv_
.notify_all();
96
}
97
}
98
102
void
wait
()
const
{
103
std::unique_lock<std::mutex> lock(
mutex_
);
104
cv_
.wait(lock, [
this
] {
return
count_
== 0 &&
done_
; });
105
}
106
111
std::function<void()>
callback
()
const
{
112
std::lock_guard<std::mutex> lock(
mutex_
);
113
return
callback_
;
114
}
115
120
void
connect
(std::function<
void
()>
callback
) {
121
std::lock_guard<std::mutex> lock(
mutex_
);
122
callback_
=
callback
;
123
}
124
126
Group
(
const
Group
&) =
delete
;
128
Group
&
operator=
(
const
Group
&) =
delete
;
130
Group
(
Group
&&) =
delete
;
132
Group
&
operator=
(
Group
&&) =
delete
;
133
};
134
}
console::Group::operator=
Group & operator=(const Group &)=delete
删除复制赋值函数。
console::Group::mutex_
std::mutex mutex_
互斥锁
Definition
group.h:49
console::Group::wait
void wait() const
等待所有任务完成。
Definition
group.h:102
console::Group::connect
void connect(std::function< void()> callback)
连接回调函数。
Definition
group.h:120
console::Group::~Group
~Group()
析构函数,确保所有任务完成。
Definition
group.h:68
console::Group::Group
Group(size_t count=0, std::function< void()> callback=no_op)
构造函数,初始化任务计数器和回调函数。
Definition
group.h:61
console::Group::callback_
std::function< void()> callback_
任务完成回调
Definition
group.h:47
console::Group::done
void done()
任务完成。
Definition
group.h:89
console::Group::no_op
static void no_op()
空操作函数,用于默认回调。
Definition
group.h:53
console::Group::cv_
std::condition_variable cv_
条件变量
Definition
group.h:50
console::Group::count_
int count_
任务计数器
Definition
group.h:46
console::Group::done_
bool done_
任务完成标志
Definition
group.h:48
console::Group::Group
Group(const Group &)=delete
删除复制构造函数。
console::Group::operator=
Group & operator=(Group &&)=delete
删除移动赋值函数。
console::Group::add
void add(size_t count)
添加任务数量。
Definition
group.h:78
console::Group::callback
std::function< void()> callback() const
获取回调函数。
Definition
group.h:111
console::Group::Group
Group(Group &&)=delete
删除移动构造函数。
console
本库所有组件所在的顶层命名空间。
include
async
group.h
Generated on
for Console Library by
1.18.0