-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoopQueue.h
150 lines (114 loc) · 2.49 KB
/
LoopQueue.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
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
/************************************************************************/
///*功能: 数据池 及管理
///*作者:winner
///*修改时间:2018.07.27
/************************************************************************/
#ifndef __LOOPQUEUE_H__
#define __LOOPQUEUE_H__
#include <set>
#include "AssertEx.h"
template<typename DataType>
class LoopQueue : protected TC_ThreadLock, public taf::TC_Singleton< LoopQueue<DataType> >
{
public:
LoopQueue()
{
m_data = NULL;
m_MaxCount = 0;
}
~LoopQueue()
{
__ENTER_FUNCTION
SAFE_DELETE_ARRAY(m_data);
__LEAVE_FUNCTION
}
bool Init(UINT maxCount)
{
__ENTER_FUNCTION
Lock lock(*this);
m_MaxCount = maxCount;
m_data = new DataType[m_MaxCount];
CrashAssert(m_data);
front = 0;
rear = 0;
return TRUE;
__LEAVE_FUNCTION
return FALSE;
}
bool isEmpty() {
if (front == rear)
{
return true;
}
return false;
}
bool isFull() {
if (front == (rear + 1) % m_MaxCount)
{
return true;
}
return false;
}
DataType* NewData()
{
__ENTER_FUNCTION
Lock lock(*this);
if (isFull())
{
return NULL;
}
notify();
DataType* res = &(m_data[rear]);
rear = (rear + 1) % m_MaxCount;
return res;
__LEAVE_FUNCTION
return NULL;
}
DataType* DelData(size_t millsecond)
{
__ENTER_FUNCTION
Lock lock(*this);
if (isEmpty())
{
if (millsecond == 0)
{
return NULL;
}
if (millsecond == (size_t)-1)
{
wait();
}
else
{
//超时了
if (!timedWait(millsecond))
{
return NULL;
}
}
}
if (isEmpty())
{
return NULL;
}
DataType* res = &(m_data[front]);
front = (front + 1) % m_MaxCount;
return res;
__LEAVE_FUNCTION
return NULL;
}
UINT MaxSize(){
Lock lock(*this);
return m_MaxCount;
}
UINT Size() {
Lock lock(*this);
return (rear + m_MaxCount - front) % m_MaxCount;
}
private:
DataType* m_data;
UINT m_MaxCount; //总的
int front; //队首
int rear; //对尾
};
#endif