-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathts_queue.hpp
112 lines (94 loc) · 2.47 KB
/
ts_queue.hpp
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
#include <pthread.h>
#ifndef TS_QUEUE_HPP
#define TS_QUEUE_HPP
#define DEFAULT_BUFFER_SIZE 200
// A thread safe queue that allows multi-thread access.
template <class T>
class TSQueue
{
public:
// constructor
TSQueue();
explicit TSQueue(int max_buffer_size);
// destructor
~TSQueue();
// add an element to the end of the queue
void enqueue(T item);
// remove and return the first element of the queue
T dequeue();
// Return the number of elements in the queue. The result is useful only when this queue not undergoing concurrent updates
// in other threads. Otherwise the result is just a transient state that may be adequate for monitoring or estimation purposes,
// but not for program control.
int get_size();
private:
// the maximum buffer size
int buffer_size;
// the buffer containing values of the queue
T *buffer;
// the current size of the buffer
int size;
// the index of first item in the queue
int head;
// the index of last item in the queue
int tail;
// pthread mutex lock
pthread_mutex_t mutex;
// pthread conditional variable
pthread_cond_t cond_enqueue, cond_dequeue;
};
// Implementation start
template <class T>
TSQueue<T>::TSQueue() : TSQueue(DEFAULT_BUFFER_SIZE)
{
}
template <class T>
TSQueue<T>::TSQueue(int buffer_size) : buffer_size(buffer_size), buffer(new T[buffer_size]), size(0), head(0), tail(buffer_size - 1)
{
pthread_mutex_init(&mutex, nullptr);
pthread_cond_init(&cond_enqueue, nullptr);
pthread_cond_init(&cond_dequeue, nullptr);
}
template <class T>
TSQueue<T>::~TSQueue()
{
delete[] buffer;
pthread_cond_destroy(&cond_enqueue);
pthread_cond_destroy(&cond_dequeue);
pthread_mutex_destroy(&mutex);
}
template <class T>
void TSQueue<T>::enqueue(T item)
{
pthread_mutex_lock(&mutex);
while (size == buffer_size) // the queue is full, wait until dequeue
{
pthread_cond_wait(&cond_enqueue, &mutex);
}
buffer[head] = item;
head = (head + 1) % buffer_size;
++size;
pthread_cond_signal(&cond_dequeue); // we have item(s) in queue, notify dequeue
pthread_mutex_unlock(&mutex);
}
template <class T>
T TSQueue<T>::dequeue()
{
T res;
pthread_mutex_lock(&mutex);
while (!size) // the queue is empty, wait until enqueue
{
pthread_cond_wait(&cond_dequeue, &mutex);
}
tail = (tail + 1) % buffer_size;
res = buffer[tail];
--size;
pthread_cond_signal(&cond_enqueue); // we have slot(s), notify enqueue
pthread_mutex_unlock(&mutex);
return res;
}
template <class T>
int TSQueue<T>::get_size()
{
return size;
}
#endif // TS_QUEUE_HPP