template typename Data class concurrent_queue private Q_DISABLE_COPY c

 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
template<typename Data>
class concurrent_queue
{
private:
Q_DISABLE_COPY(concurrent_queue)
QQueue<Data> qu;
mutable QMutex mutex;
QWaitCondition emptyCond; // notFullCond
QWaitCondition FullCond; // notEmptyCond
public:
concurrent_queue() {}
int size() const
{
QMutexLocker locker(&mutex);
return qu.size();
}
void wait_and_pop(Data& popped_value)
{
{
QMutexLocker locker(&mutex);
while(qu.empty())
{
FullCond.wait(&mutex);
}
popped_value = qu.dequeue();
}
emptyCond.wakeOne();
}
void wait_and_push(Data const& data)
{
{
QMutexLocker locker(&mutex);
while(qu.size() >= 2000) // max_size
{
emptyCond.wait(&mutex);
}
qu.enqueue(data);
}
FullCond.wakeOne();
}
};