forked from cathy-kim/Pelee-TensorRT
-
Notifications
You must be signed in to change notification settings - Fork 3
/
imageBuffer.h
executable file
·84 lines (69 loc) · 1.47 KB
/
imageBuffer.h
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
#ifndef IMAGEBUFFER_H
#define IMAGEBUFFER_H
#include <opencv2/opencv.hpp>
#include <mutex>
#include <condition_variable>
#include <queue>
template<typename T>
class ConsumerProducerQueue
{
public:
ConsumerProducerQueue(int mxsz,bool dropFrame) :
maxSize(mxsz),dropFrame(dropFrame)
{ }
bool add(T request)
{
std::unique_lock<std::mutex> lock(mutex);
if(dropFrame && isFull())
{
lock.unlock();
return false;
}
else {
cond.wait(lock, [this]() { return !isFull(); });
cpq.push(request);
//lock.unlock();
cond.notify_all();
return true;
}
}
void consume(T &request)
{
std::unique_lock<std::mutex> lock(mutex);
cond.wait(lock, [this]()
{ return !isEmpty(); });
request = cpq.front();
cpq.pop();
//lock.unlock();
cond.notify_all();
}
bool isFull() const
{
return cpq.size() >= maxSize;
}
bool isEmpty() const
{
return cpq.size() == 0;
}
int length() const
{
return cpq.size();
}
void clear()
{
std::unique_lock<std::mutex> lock(mutex);
while (!isEmpty())
{
cpq.pop();
}
lock.unlock();
cond.notify_all();
}
private:
std::condition_variable cond;
std::mutex mutex;
std::queue<T> cpq;
int maxSize;
bool dropFrame;
};
#endif