-
Notifications
You must be signed in to change notification settings - Fork 1
/
block_queue.h
202 lines (177 loc) · 5.47 KB
/
block_queue.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
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
#ifndef BLOCK_QUEUE_H
#define BLOCK_QUEUE_H
#include <iostream>
#include <stdlib.h>
#include <pthread.h>
#include <sys/time.h>
using namespace std;
template<class T>
class block_queue
{
public:
block_queue(int max_size = 1000)
{
if(max_size <= 0)
{
exit(-1);
}
m_max_size = max_size;
m_array = new T[max_size];
m_size = 0;
m_front = -1;
m_back = -1;
m_mutex = new pthread_mutex_t;
m_cond = new pthread_cond_t;
pthread_mutex_init(m_mutex, NULL);
pthread_cond_init(m_cond, NULL);
}
void clear()
{
pthread_mutex_lock(m_mutex);
m_size = 0;
m_front = -1;
m_back = -1;
pthread_mutex_unlock(m_mutex);
}
~block_queue()
{
pthread_mutex_lock(m_mutex);
if(m_array != NULL)
delete m_array;
pthread_mutex_unlock(m_mutex);
pthread_mutex_destroy(m_mutex);
pthread_cond_destroy(m_cond);
delete m_mutex;
delete m_cond;
}
bool full()const
{
pthread_mutex_lock(m_mutex);
if(m_size >= m_max_size)
{
pthread_mutex_unlock(m_mutex);
return true;
}
pthread_mutex_unlock(m_mutex);
return false;
}
bool empty()const
{
pthread_mutex_lock(m_mutex);
if(0 == m_size)
{
pthread_mutex_unlock(m_mutex);
return true;
}
pthread_mutex_unlock(m_mutex);
return false;
}
bool front(T& value)const
{
pthread_mutex_lock(m_mutex);
if(0 == m_size)
{
pthread_mutex_unlock(m_mutex);
return false;
}
value = m_array[m_front];
pthread_mutex_unlock(m_mutex);
return true;
}
bool back(T& value)const
{
pthread_mutex_lock(m_mutex);
if(0 == m_size)
{
pthread_mutex_unlock(m_mutex);
return false;
}
value = m_array[m_back];
pthread_mutex_unlock(m_mutex);
return true;
}
int size()const
{
int tmp = 0;
pthread_mutex_lock(m_mutex);
tmp = m_size;
pthread_mutex_unlock(m_mutex);
return tmp;
}
int max_size()const
{
int tmp = 0;
pthread_mutex_lock(m_mutex);
tmp = m_max_size;
pthread_mutex_unlock(m_mutex);
return tmp;
}
bool push(const T& item)
{
pthread_mutex_lock(m_mutex);
if(m_size >= m_max_size)
{
pthread_cond_broadcast(m_cond);
pthread_mutex_unlock(m_mutex);
return false;
}
m_back = (m_back + 1) % m_max_size;
m_array[m_back] = item;
m_size++;
pthread_cond_broadcast(m_cond);
pthread_mutex_unlock(m_mutex);
return true;
}
bool pop(T& item)
{
pthread_mutex_lock(m_mutex);
while(m_size <= 0)
{
if(0 != pthread_cond_wait(m_cond, m_mutex))
{
pthread_mutex_unlock(m_mutex);
return false;
}
}
m_front = (m_front + 1) % m_max_size;
item = m_array[m_front];
m_size--;
pthread_mutex_unlock(m_mutex);
return true;
}
bool pop(T& item, int ms_timeout)
{
struct timespec t = {0,0};
struct timeval now = {0,0};
gettimeofday(&now, NULL);
pthread_mutex_lock(m_mutex);
if(m_size <= 0)
{
t.tv_sec = now.tv_sec + ms_timeout/1000;
t.tv_nsec = (ms_timeout % 1000)*1000;
if(0 != pthread_cond_timedwait(m_cond, m_mutex, &t))
{
pthread_mutex_unlock(m_mutex);
return false;
}
}
if(m_size <= 0)
{
pthread_mutex_unlock(m_mutex);
return false;
}
m_front = (m_front + 1) % m_max_size;
item = m_array[m_front];m_size--;
pthread_mutex_unlock(m_mutex);
return true;
}
private:
pthread_mutex_t *m_mutex;
pthread_cond_t *m_cond;
T *m_array;
int m_size;
int m_max_size;
int m_front;
int m_back;
};
#endif