/
ThreadSafeQueue.h
185 lines (163 loc) · 5.23 KB
/
ThreadSafeQueue.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
/*
QueuePtr - Self returning buffer pool
Copyright (C) 2014 Chris Desjardins
http://blog.chrisd.info cjd@chrisd.info
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
** This provides a thread safe queue
*/
#ifndef CB_THREAD_SAFE_QUEUE_Hxx
#define CB_THREAD_SAFE_QUEUE_Hxx
#include <list>
#include <mutex>
#include <condition_variable>
/*
** On the wait functions in this class if timeout == -1, then wait forever...
** When dequeing a batch of messages, the return code is the total number of bytes
** in all messages in the batch.
*/
template <class T> class ThreadSafeQueue
{
public:
ThreadSafeQueue()
: _queue(),
_queueMutex(),
_msgNotification(),
_numEnqueued(0)
{
}
virtual ~ThreadSafeQueue()
{
}
virtual void enqueue(const T& data)
{
{ // create a new scope for the mutex
std::unique_lock<std::mutex> lock(_queueMutex);
pushData(data);
_msgNotification.notify_all();
}
}
virtual bool dequeue(T& data, const int msTimeout = 0)
{
bool ret = false;
{ // create a new scope for the mutex
std::unique_lock<std::mutex> lock(_queueMutex);
waitForData(lock, msTimeout);
ret = popData(data);
}
return ret;
}
// Dequeue everything
virtual size_t dequeue(std::vector<T>& dataVec, const int msTimeout = 0)
{
size_t size = 0;
{ // create a new scope for the mutex
std::unique_lock<std::mutex> lock(_queueMutex);
waitForData(lock, msTimeout);
size = popData(dataVec);
}
return size;
}
size_t size() const
{
return _numEnqueued;
}
/*
** This function allows you to perform operations on the
** vector in a thread safe way. The functor is a function
** with the following signature:
** int func(std::list<T> &);
** The return value is the number of elements added or removed
** from the list, for example if 5 elements were removed
** and 3 new elements were added then the return value should
** be -2.
*/
template <typename Functor> void iterate(Functor functor)
{
{ // create a new scope for the mutex
std::unique_lock<std::mutex> lock(_queueMutex);
// the return value of this functor is added to the _numEnqueued
// so if you add buffers then return the number of buffers added
// or if you remove buffers then return -number of buffers removed
int numChanged = functor(_queue);
_numEnqueued += numChanged;
}
}
protected:
void waitForData(std::unique_lock<std::mutex>& lock, const int msTimeout)
{
if (msTimeout != 0)
{
// This function assumes that _queueMutex is locked already!
std::chrono::system_clock::time_point timeLimit = std::chrono::system_clock::now() +
std::chrono::milliseconds(msTimeout);
while (_queue.empty() == true)
{
// if timeout is specified, then wait until the time is up
// otherwise wait forever (forever is msTimeout = -1)
if (msTimeout > 0)
{
_msgNotification.wait_until(lock, timeLimit);
if (std::chrono::system_clock::now() >= timeLimit)
{
break;
}
}
else
{
_msgNotification.wait(lock);
}
}
}
}
void pushData(const T& data)
{
// This function assumes that _queueMutex is locked already!
_queue.push_back(data);
_numEnqueued++;
}
bool popData(T& data)
{
// This function assumes that _queueMutex is locked already!
bool ret = false;
if (_queue.empty() == false)
{
data = _queue.front();
_queue.pop_front();
_numEnqueued--;
ret = true;
}
return ret;
}
size_t popData(std::vector<T>& dataVec)
{
// This function assumes that _queueMutex is locked already!
size_t size = 0;
T data;
while (popData(data) == true)
{
size += sizeOfData(data);
dataVec.push_back(data);
}
return size;
}
virtual size_t sizeOfData(const T&) const
{
return sizeof(T);
}
std::list<T> _queue;
std::mutex _queueMutex;
std::condition_variable _msgNotification;
size_t _numEnqueued;
};
#endif