-
Notifications
You must be signed in to change notification settings - Fork 0
/
TriggerQueue.h
117 lines (97 loc) · 2.49 KB
/
TriggerQueue.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
//*****************************************************************************
//
// Copyright (C) 2001 Steve Connet. All rights reserved.
//
// Source File Name : TriggerQueue.h
// Author : Steve Connet
//
// Version : $Id: TriggerQueue.h,v 1.3 2015/03/14 23:44:56 clu Exp $
//
// File Overview : queue template with auto trigger on push
//
// Revision History :
//
// $Log: TriggerQueue.h,v $
// Revision 1.3 2015/03/14 23:44:56 clu
// astyle code formatter applied
//
// Revision 1.2 2002/01/11 03:41:49 sconnet
// *** empty log message ***
//
// Revision 1.1 2001/11/08 06:17:14 sconnet
// Initial revision
//
//
//*****************************************************************************
#ifndef __TRIGGERQUEUE_H_
#define __TRIGGERQUEUE_H_
#include <cerrno>
#include <queue>
//namespace ConnetUtils
//{
template<class T>
class TriggerQueue
{
std::queue<T> data_queue;
bool trigger;
pthread_cond_t cond;
pthread_mutex_t mutex;
public:
TriggerQueue::TriggerQueue()
{
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
}
TriggerQueue::~TriggerQueue()
{
// signal all threads to wake up
trigger(true);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
}
void TriggerQueue::waitOnTrigger()
{
pthread_mutex_lock(&mutex);
// turn off the trigger if the queue is empty
if(0 == data_queue.size()) {
trigger = false;
}
while(!trigger) {
pthread_cond_wait(&cond, &mutex);
}
pthread_mutex_unlock(&mutex);
} // waitOnTrigger
void TriggerQueue::trigger(bool all = false)
{
// turn the trigger on
pthread_mutex_lock(&mutex);
trigger = true;
pthread_mutex_unlock(&mutex);
if(all) {
pthread_cond_broadcast(&cond); // signal all waiting threads
}
else {
pthread_cond_signal(&cond); // signal a single waiting thread
}
} // trigger
int size() const {
return data_queue.size();
}
void push(T obj, bool all = false)
{
data_queue.push(obj);
trigger(all); // true to trigger all waiting threads
}
bool pop_front(T &obj)
{
bool is_empty((bool)data_queue.empty());
if(!is_empty)
{
obj = data_queue.front();
data_queue.pop();
}
return !bEmpty;
}
};
//}
#endif // __TRIGGER_QUEUE_H_