forked from Smorodov/Multitarget-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.h
More file actions
302 lines (266 loc) · 6.73 KB
/
Queue.h
File metadata and controls
302 lines (266 loc) · 6.73 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#pragma once
#include <queue>
#include <deque>
#include <list>
#include <vector>
#include <algorithm>
#include <mutex>
#include <condition_variable>
///
/// \brief currTime
/// \return
///
inline std::string CurrTime_()
{
auto t = std::time(nullptr);
auto tm = *std::localtime(&t);
std::ostringstream oss;
oss << std::put_time(&tm, "%d.%m.%Y %H:%M:%S:");
return oss.str();
}
#define QUE_LOG std::cout << CurrTime_()
#define QUE_ERR_LOG (std::cerr << CurrTime_())
///
/// A threadsafe-queue
///
template <class T>
class SafeQueue
{
public:
///
SafeQueue(void)
: m_que()
, m_mutex()
, m_cond()
{
}
///
virtual ~SafeQueue(void)
{
}
protected:
typedef std::list<T> queue_t;
queue_t m_que;
mutable std::mutex m_mutex;
std::condition_variable m_cond;
///
/// Add an element to the queue
///
void enqueue(T t)
{
std::lock_guard<std::mutex> lock(m_mutex);
m_que.push_back(t);
m_cond.notify_all();
}
///
/// Add an element to the queue
///
void enqueue(T t, size_t maxQueueSize)
{
std::lock_guard<std::mutex> lock(m_mutex);
m_que.push(t);
if (m_que.size() > maxQueueSize)
{
m_que.pop();
}
m_cond.notify_all();
}
///
/// Add an element to the queue
///
template<typename RET_V, typename RET_F>
RET_V enqueue(T t, RET_F && F)
{
std::lock_guard<std::mutex> lock(m_mutex);
m_que.push(t);
RET_V ret = F(m_que.front());
m_cond.notify_all();
return ret;
}
///
/// Get the "front"-element
/// If the queue is empty, wait till a element is avaiable
///
T dequeue(void)
{
std::unique_lock<std::mutex> lock(m_mutex);
while (m_que.empty())
{
// release lock as long as the wait and reaquire it afterwards
m_cond.wait(lock);
}
T val = m_que.front();
m_que.pop();
return val;
}
///
size_t size()
{
std::unique_lock<std::mutex> lock(m_mutex);
size_t res = m_que.size();
return res;
}
};
struct FrameInfo;
typedef std::shared_ptr<FrameInfo> frame_ptr;
///
/// A threadsafe-queue with Frames
///
class FramesQueue : public SafeQueue<frame_ptr>
{
public:
///
/// \brief AddNewFrame
/// \param frameInfo
///
void AddNewFrame(frame_ptr frameInfo)
{
//QUE_LOG << "AddNewFrame start: " << frameInfo->m_dt << std::endl;
enqueue(frameInfo);
//QUE_LOG << "AddNewFrame end: " << frameInfo->m_dt << std::endl;
}
///
/// \brief PrintQue
///
void PrintQue()
{
QUE_LOG << "m_que (" << m_que.size() << "): ";
size_t i = 0;
for (auto it : m_que)
{
if (it->m_inDetector && it->m_inTracker)
{
std::cout << i << " d" << it->m_inDetector << " t" << it->m_inTracker << "; ";
}
else if (it->m_inDetector)
{
std::cout << i << " d" << it->m_inDetector << "; ";
}
else if (it->m_inTracker)
{
std::cout << i << " t" << it->m_inTracker << "; ";
}
else
{
std::cout << i << "; ";
}
++i;
}
std::cout << std::endl;
}
///
/// \brief GetLastUndetectedFrame
/// \return
///
frame_ptr GetLastUndetectedFrame()
{
//QUE_LOG << "GetLastUndetectedFrame start" << std::endl;
std::unique_lock<std::mutex> lock(m_mutex);
while (m_que.empty() || m_que.back()->m_inDetector)
{
if (m_break)
{
break;
}
m_cond.wait(lock);
//PrintQue();
}
if (!m_break)
{
frame_ptr frameInfo = m_que.back();
frameInfo->m_inDetector = 1;
//QUE_LOG << "GetLastUndetectedFrame end: " << frameInfo->m_dt << std::endl;
return frameInfo;
}
return nullptr;
}
///
/// \brief SearchUntracked
/// \return
///
queue_t::iterator SearchUntracked()
{
queue_t::iterator res_it = m_que.end();
for (queue_t::iterator it = m_que.begin(); it != m_que.end(); ++it)
{
if ((*it)->m_inDetector != 1 && (*it)->m_inTracker == 0)
{
res_it = it;
break;
}
}
return res_it;
}
///
/// \brief GetFirstDetectedFrame
/// \return
///
frame_ptr GetFirstDetectedFrame()
{
//QUE_LOG << "GetFirstDetectedFrame start" << std::endl;
std::unique_lock<std::mutex> lock(m_mutex);
queue_t::iterator it = SearchUntracked();
while (it == m_que.end())
{
if (m_break)
{
break;
}
m_cond.wait(lock);
it = SearchUntracked();
//PrintQue();
}
if (!m_break)
{
frame_ptr frameInfo = *it;
frameInfo->m_inTracker = 1;
//QUE_LOG << "GetFirstDetectedFrame end: " << frameInfo->m_dt << std::endl;
return frameInfo;
}
return nullptr;
}
///
/// \brief GetFirstProcessedFrame
/// \return
///
frame_ptr GetFirstProcessedFrame()
{
//QUE_LOG << "GetFirstProcessedFrame start" << std::endl;
std::unique_lock<std::mutex> lock(m_mutex);
while (m_que.empty() || m_que.front()->m_inTracker != 2)
{
if (m_break)
{
break;
}
m_cond.wait(lock);
//PrintQue();
}
if (!m_break)
{
frame_ptr frameInfo = m_que.front();
m_que.pop_front();
//QUE_LOG << "GetFirstProcessedFrame end: " << frameInfo->m_dt << std::endl;
return frameInfo;
}
return nullptr;
}
///
/// \brief Signal
///
void Signal(int64 ts)
{
//QUE_LOG << "Signal start:" << ts << std::endl;
m_cond.notify_all();
//QUE_LOG << "Signal end: " << ts << std::endl;
}
void SetBreak(bool val)
{
//QUE_LOG << "SetBreak start:" << val << std::endl;
m_break = val;
Signal(0);
//QUE_LOG << "SetBreak end:" << val << std::endl;
}
private:
bool m_break = false;
};