-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkerqueue.h
More file actions
97 lines (69 loc) · 1.43 KB
/
Copy pathworkerqueue.h
File metadata and controls
97 lines (69 loc) · 1.43 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
#ifndef WORKERQUEUE_H
#define WORKERQUEUE_H
#include <QList>
#include <QMutex>
#include <QDebug>
template <class T>
class WorkerQueue
{
public:
explicit WorkerQueue(int maxSize) : _maxSize(maxSize), _list(), _readMutex(), _writeMutex() {};
void push(const T &data);
T pop();
int count();
bool isFull();
void clear();
private:
int _maxSize;
QList<T> _list;
QMutex _readMutex;
QMutex _writeMutex;
};
template <class T>
void WorkerQueue<T>::push(const T &data)
{
_writeMutex.lock();
if (_list.count() == _maxSize)
{
_writeMutex.unlock();
return;
}
// qDebug() << "push list.count():" << _list.count();
_list.push_back(data);
_writeMutex.unlock();
}
template <class T>
T WorkerQueue<T>::pop()
{
if (_list.isEmpty()) throw;
_readMutex.lock();
// qDebug() << "pop list.count():" << _list.count();
T data = _list.front();
_list.pop_front();
_readMutex.unlock();
return data;
}
template <class T>
int WorkerQueue<T>::count()
{
_readMutex.lock();
int count = _list.count();
_readMutex.unlock();
return count;
}
template <class T>
bool WorkerQueue<T>::isFull()
{
_readMutex.lock();
bool isFull = _list.count() == _maxSize;
_readMutex.unlock();
return isFull;
}
template <class T>
void WorkerQueue<T>::clear()
{
_writeMutex.lock();
_list.clear();
_writeMutex.unlock();
}
#endif // WORKERQUEUE_H