-
Notifications
You must be signed in to change notification settings - Fork 272
Expand file tree
/
Copy pathtask_group.h
More file actions
58 lines (48 loc) · 1.11 KB
/
task_group.h
File metadata and controls
58 lines (48 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#pragma once
namespace cppwinrt
{
struct task_group
{
task_group(task_group const&) = delete;
task_group& operator=(task_group const&) = delete;
task_group() noexcept = default;
~task_group() noexcept
{
for (auto&& task : m_tasks)
{
task.wait();
}
}
void synchronous(bool synchronous) noexcept
{
m_synchronous = synchronous;
}
template <typename T>
void add(T&& callback)
{
if (m_synchronous)
{
callback();
}
else
{
m_tasks.push_back(std::async(std::forward<T>(callback)));
}
}
void get()
{
auto tasks = std::move(m_tasks);
for (auto&& task : tasks)
{
task.wait();
}
for (auto&& task : tasks)
{
task.get();
}
}
private:
std::vector<std::future<void>> m_tasks;
bool m_synchronous{};
};
}