-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventProcessor.h
More file actions
46 lines (36 loc) · 974 Bytes
/
EventProcessor.h
File metadata and controls
46 lines (36 loc) · 974 Bytes
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
#pragma once
#include <thread>
#include <mutex>
#include <vector>
namespace ndtech {
template <typename ItemType>
struct EventProcessor {
std::mutex m_itemsMutex;
std::thread m_thread;
bool m_done = false;
std::vector< std::function<ItemType(ItemType, Action)>>
void Start() {
m_thread = std::thread{ &EventProcessor::Run, this };
}
void Run() {
while (!m_done) {
Process();
}
}
void Process() {
std::lock_guard lockGuard(m_itemsMutex);
for (auto metadata = m_itemsMetadataMap.begin(); metadata != m_itemsMetadataMap.end(); metadata++) {
if (!metadata->second.m_ready) {
auto item = m_itemsMap.find(metadata->first);
auto retVal = m_itemFunction(*item);
metadata->second.m_ready = true;
metadata->second.m_conditionVariable->notify_all();
}
}
};
void Join() {
m_done = true;
m_thread.join();
}
};
}