-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiQueueProcessor.h
More file actions
143 lines (125 loc) · 3.38 KB
/
Copy pathMultiQueueProcessor.h
File metadata and controls
143 lines (125 loc) · 3.38 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
#pragma once
#include <map>
#include <list>
#include <thread>
#include <mutex>
#include <bits/stdc++.h>
template<typename Key, typename Value>
struct IConsumer
{
virtual void Consume(Key id, const Value &value)
{
id;
value;
}
virtual ~IConsumer() = default;
};
#define MaxCapacity 1000
template<typename Key, typename Value>
class MultiQueueProcessor
{
public:
MultiQueueProcessor() :
running{ true },
th(std::bind(&MultiQueueProcessor::Process, this)) {}
~MultiQueueProcessor()
{
StopProcessing();
cv.notify_one();
th.join();
}
void StopProcessing()
{
running = false;
}
void Subscribe(Key id, IConsumer<Key, Value> * consumer)
{
std::lock_guard<std::mutex> lock{ ctx };
auto iter = consumers.find(id);
if (iter == consumers.end())
{
consumers.insert(std::make_pair(id, consumer));
}
}
void Unsubscribe(Key id)
{
std::lock_guard<std::mutex> lock{ ctx };
auto iter = consumers.find(id);
if (iter != consumers.end())
consumers.erase(id);
}
bool Enqueue(Key id, Value value)
{
std::lock_guard<std::mutex> lock{ mtx };
auto iter = queues.find(id);
if (iter != queues.end())
{
if (iter->second.size() < MaxCapacity)
iter->second.push_back(value);
else
return false;
}
else
{
queues.insert(std::make_pair(id, std::list<Value>()));
iter = queues.find(id);
if (iter != queues.end())
{
if (iter->second.size() < MaxCapacity)
iter->second.push_back(value);
else
return false;
}
}
cv.notify_one();
return true;
}
protected:
Value Dequeue(Key id)
{
auto iter = queues.find(id);
if (iter != queues.end())
{
if (iter->second.size() > 0)
{
auto front = iter->second.front();
iter->second.pop_front();
return front;
}
}
return Value{};
}
void Process()
{
while (running)
{
std::unique_lock lock{ mtx };
cv.wait(lock, [&](){
return !queues.empty() || running;
});
for (auto iter = queues.begin(); iter != queues.end(); ++iter)
{
std::lock_guard<std::mutex> lock{ ctx };
auto consumerIter = consumers.find(iter->first);
if (consumerIter != consumers.end())
{
Value front = Dequeue(iter->first);
mtx.unlock();
if (front != Value{})
{
consumerIter->second->Consume(iter->first, front);
}
mtx.lock();
}
}
}
}
protected:
std::map<Key, IConsumer<Key, Value> *> consumers;
std::map<Key, std::list<Value>> queues;
std::atomic_bool running;
std::mutex mtx;
std::mutex ctx;
std::thread th;
std::condition_variable cv;
};