-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathmylib-blockingqueue.hpp
More file actions
178 lines (119 loc) · 3.58 KB
/
Copy pathmylib-blockingqueue.hpp
File metadata and controls
178 lines (119 loc) · 3.58 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
/******************************************************
*
* File name: mylib-blockingqueue.hpp
*
* Author: Name: Thanh Nguyen
* Email: thanh.it1995(at)gmail(dot)com
*
* License: 3-Clause BSD License
*
* Description: The blocking queue implementation in C++11 POSIX threading
*
******************************************************/
#ifndef _MYLIB_BLOCKING_QUEUE_HPP_
#define _MYLIB_BLOCKING_QUEUE_HPP_
#include <limits>
#include <queue>
#include <pthread.h>
namespace mylib
{
template <typename T>
class BlockingQueue {
private:
pthread_cond_t condEmpty = PTHREAD_COND_INITIALIZER;
pthread_cond_t condFull = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
size_t capacity;
std::queue<T> q;
struct PendingData {
BlockingQueue<T> * thisPtr;
const T data;
PendingData(BlockingQueue<T> * thisPtr, const T data)
: thisPtr(thisPtr), data(data) { }
};
public:
BlockingQueue() : capacity(std::numeric_limits<size_t>::max()) {
}
BlockingQueue(size_t capacity) : capacity(capacity) {
}
~BlockingQueue() {
pthread_cond_destroy(&condEmpty);
pthread_cond_destroy(&condFull);
pthread_mutex_destroy(&mut);
}
BlockingQueue(const BlockingQueue& other) = delete;
BlockingQueue(const BlockingQueue&& other) = delete;
void operator=(const BlockingQueue& other) = delete;
void operator=(const BlockingQueue&& other) = delete;
bool empty() const {
return q.empty();
}
size_t size() const {
return q.size();
}
// sync enqueue
void put(const T& value) {
int tmp = 0;
tmp = pthread_mutex_lock(&mut);
while (q.size() >= capacity) {
tmp = pthread_cond_wait(&condFull, &mut);
}
q.push(value);
tmp = pthread_mutex_unlock(&mut);
tmp = pthread_cond_signal(&condEmpty);
}
// sync dequeue
T take() {
T result;
int tmp = 0;
tmp = pthread_mutex_lock(&mut);
while (q.empty()) {
// Queue is empty, must wait for 'put'
tmp = pthread_cond_wait(&condEmpty, &mut);
}
result = q.front();
q.pop();
tmp = pthread_mutex_unlock(&mut);
tmp = pthread_cond_signal(&condFull);
return result;
}
// async enqueue
void add(const T& value) {
// Note: For asynchronous operations, we should use a long-live background thread
// instead of using a temporary thread
pthread_t tid;
int tmp;
auto arg = new PendingData(this, value);
tmp = pthread_create(&tid, nullptr, &BlockingQueue<T>::putPending, arg);
tmp = pthread_detach(tid);
}
// returns false if queue is empty, otherwise returns true and assigns the result
bool peek(T& result) const {
bool ret = false;
int tmp;
tmp = pthread_mutex_lock(&mut);
if (false == q.empty()) {
result = q.front();
ret = true;
}
tmp = pthread_mutex_unlock(&mut);
return ret;
}
void clear() {
int tmp;
tmp = pthread_mutex_lock(&mut);
std::queue<T>().swap(q);
tmp = pthread_mutex_unlock(&mut);
}
private:
static void* putPending(void* argVoid) {
auto arg = (PendingData*) argVoid;
arg->thisPtr->put(arg->data);
delete arg;
arg = nullptr;
pthread_exit(nullptr);
return nullptr;
}
}; // BlockingQueue
} // namespace mylib
#endif // _MYLIB_BLOCKING_QUEUE_HPP_