-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrioritizedStagedProcessor.h
More file actions
163 lines (120 loc) · 4.23 KB
/
PrioritizedStagedProcessor.h
File metadata and controls
163 lines (120 loc) · 4.23 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#pragma once
#include "pch.h"
#include <vector>
#include <functional>
#include <utility>
#include <chrono>
#include <condition_variable>
#include <algorithm>
#include <thread>
#include <memory>
#include <mutex>
#include "TypeUtilities.h"
using namespace std::chrono;
using namespace std::chrono_literals;
namespace ndtech {
template<typename ItemType>
struct PrioritizedProcessingStage {
using StageProcessingFunctionType = std::function<ItemType(ItemType)>;
std::vector<ItemType> m_items;
PrioritizedProcessingStage::StageProcessingFunctionType m_stageProcessingFunction;
std::string m_name;
~PrioritizedProcessingStage() {
std::cout << "~PrioritizedProcessingStage" << std::endl;
}
PrioritizedProcessingStage(std::function<ItemType(ItemType)> stageProcessingFunction) :
m_stageProcessingFunction(stageProcessingFunction) {
}
PrioritizedProcessingStage(std::string name, std::function<ItemType(ItemType)> stageProcessingFunction) :
m_stageProcessingFunction(stageProcessingFunction),
m_name(name)
{
}
void Process() {
for (auto item : m_items) {
m_stageProcessingFunction(item);
//std::cout << "item = " << item << std::endl;
}
}
};
template<typename ItemType>
struct PrioritizedStagedProcessor {
~PrioritizedStagedProcessor() {
std::cout << "~PrioritizedStagedProcessor" << std::endl;
}
PrioritizedStagedProcessor(std::vector<PrioritizedProcessingStage<ItemType>> stages) :
m_stages(stages) {
//m_thread = std::thread{ &PrioritizedStagedProcessor::Run, this };
}
PrioritizedStagedProcessor(std::thread thread)
:m_thread(std::move(thread)) {
}
void Start() {
m_thread = std::thread{ &PrioritizedStagedProcessor::Run, this };
}
void Run() {
while (!m_done) {
Process();
}
}
void Process() {
{ // to scope the lock
std::lock_guard<std::mutex> guard(m_stagesMutex);
size_t stageNumber = 0;
bool isFirstStage = true;
for (auto it = m_stages.begin(); it != m_stages.end(); it++) {
if (stageNumber == 0) {
// The first stage
it->Process();
}
else if (stageNumber == m_stages.size() - 1) {
// The last stage
auto previousStageIterator = --it;
it++;
it->m_items.insert(
it->m_items.end(),
std::make_move_iterator(previousStageIterator->m_items.begin()),
std::make_move_iterator(previousStageIterator->m_items.end()));
previousStageIterator->m_items.clear();
it->Process();
// Since this is the last stage, we can clear the items when finished
it->m_items.clear();
}
else {
// This is a middle stage
auto previousStageIterator = --it;
it++;
it->m_items.insert(
it->m_items.end(),
std::make_move_iterator(previousStageIterator->m_items.begin()),
std::make_move_iterator(previousStageIterator->m_items.end()));
previousStageIterator->m_items.clear();
it->Process();
}
isFirstStage = false;
stageNumber++;
}
}
}
void AddItem(ItemType item, std::size_t stageNumber) {
std::lock_guard<std::mutex> guard(m_stagesMutex);
m_stages[stageNumber].m_items.push_back(item);
}
void AddItem(ItemType item) {
AddItem(item, 0);
}
void Join() {
this->m_done = true;
m_thread.join();
}
private:
std::thread m_thread;
std::vector<ndtech::PrioritizedProcessingStage<ItemType>> m_stages;
std::mutex m_stagesMutex;
bool m_done = false;
};
template <typename ItemType>
PrioritizedStagedProcessor<ItemType> CreatePrioritizedStagedProcessor(std::vector<PrioritizedProcessingStage<ItemType>> stages) {
return PrioritizedStagedProcessor(stages);
}
}