-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdoublebufferedqueue.h
78 lines (66 loc) · 1.67 KB
/
doublebufferedqueue.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
#ifndef DOUBLEBUFFEREDQUEUE_HDR
#define DOUBLEBUFFEREDQUEUE_HDR
// CRITICAL_SECTION
#include <windows.h>
template <class T>
class DoubleBufferedQueue
{
public:
//
// Constructor - construct a queue with the given buffer size
//
DoubleBufferedQueue(int bufferSize = 10000) :
m_bufferLen(0), m_bufferSize(bufferSize)
{
m_pBuffer0 = m_pCurrentBuffer = new T[bufferSize];
m_pBuffer1 = new T[bufferSize];
InitializeCriticalSection(&m_lock);
}
//
// Destructor
//
~DoubleBufferedQueue()
{
DeleteCriticalSection(&m_lock);
delete[] m_pBuffer0;
delete[] m_pBuffer1;
}
//
// Add an item to the queue. Returns true if successful, false if the buffer is full.
//
bool put(const T& datum)
{
EnterCriticalSection(&m_lock);
bool canWrite = m_bufferLen < m_bufferSize;
if (canWrite)
m_pCurrentBuffer[m_bufferLen++] = datum;
LeaveCriticalSection(&m_lock);
return canWrite;
}
//
// Get all the items in the queue. The T** argument should be the address of a variable to
// receive the pointer to the item array. The return value is the size of the array.
//
int get(T** data)
{
EnterCriticalSection(&m_lock);
*data = m_pCurrentBuffer;
int ret = m_bufferLen;
// Important !!!
m_pCurrentBuffer = (m_pCurrentBuffer == m_pBuffer0) ? m_pBuffer1 : m_pBuffer0;
m_bufferLen = 0;
LeaveCriticalSection(&m_lock);
return ret;
}
private:
// Disable copying and assignment
DoubleBufferedQueue & operator=(const DoubleBufferedQueue&);
DoubleBufferedQueue(const DoubleBufferedQueue&);
T* m_pBuffer0;
T* m_pBuffer1;
T* m_pCurrentBuffer;
int m_bufferLen;
int m_bufferSize;
CRITICAL_SECTION m_lock;
};
#endif