ddc
ddcThreadQueue.h
Go to the documentation of this file.
1 //-*- Mode: C++ -*-
2 //
3 // DDC originally by Alexey Sokirko
4 // This file by Bryan Jurish, adapted from https://vichargrave.github.io/articles/2013-01/multithreaded-work-queue-in-cpp
5 //
6 // This file is part of DDC.
7 //
8 // DDC is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU Lesser General Public License as published by
10 // the Free Software Foundation, either version 3 of the License, or
11 // (at your option) any later version.
12 //
13 // DDC is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public License
19 // along with DDC. If not, see <http://www.gnu.org/licenses/>.
20 //
21 #ifndef DDC_THREAD_QUEUE_H
22 #define DDC_THREAD_QUEUE_H
23 
24 #include "ddcConfig.h"
25 #include <pthread.h>
26 #include <list>
27 
29 #define DDC_DEFAULT_THREAD_QUEUE_SIZE 256
30 
31 //======================================================================
36 template<typename T> class ddcThreadQueue
37 {
38 public:
39  //--------------------------------------------------------------
41 
42  typedef T ItemT;
45 
46 public:
47  //--------------------------------------------------------------
49  // (all public, because private data is annoying)
51 
53  std::list<T> m_queue;
54 
56  size_t m_max_size;
57 
59  pthread_mutex_t m_mutex;
60 
62  pthread_cond_t m_condv_added;
63 
65  pthread_cond_t m_condv_removed;
67 
68 public:
69  //--------------------------------------------------------------
71 
72 
74  : m_max_size(max_size)
75  {
76  pthread_mutex_init(&m_mutex, NULL);
77  pthread_cond_init(&m_condv_added, NULL);
78  pthread_cond_init(&m_condv_removed, NULL);
79  };
80 
83  {
84  pthread_mutex_destroy(&m_mutex);
85  pthread_cond_destroy(&m_condv_added);
86  pthread_cond_destroy(&m_condv_removed);
87  };
89 
90  //--------------------------------------------------------------
92 
93 
99  void add(T item)
100  {
101  pthread_mutex_lock(&m_mutex);
102  while (m_queue.size() == m_max_size) {
103  //fprintf(stderr, "ddcThreadQueue::add(): max queue size (%zu) reached; waiting...\n", m_queue.size());
104  pthread_cond_wait(&m_condv_removed, &m_mutex);
105  }
106  //fprintf(stderr, "ddcThreadQueue::add(): appending queue item to queue of size %zu\n", m_queue.size());
107  m_queue.push_back(item);
108  pthread_cond_signal(&m_condv_added);
109  pthread_mutex_unlock(&m_mutex);
110  };
111 
117  T remove()
118  {
119  pthread_mutex_lock(&m_mutex);
120  while (m_queue.size() == 0) {
121  pthread_cond_wait(&m_condv_added, &m_mutex);
122  }
123 
124  T item = m_queue.front();
125  m_queue.pop_front();
126 
127  pthread_cond_signal(&m_condv_removed);
128  pthread_mutex_unlock(&m_mutex);
129  return item;
130  };
131 
132 
136  size_t size() const
137  {
138  pthread_mutex_lock(&m_mutex);
139  size_t sz = m_queue.size();
140  pthread_mutex_unlock(&m_mutex);
141  return sz;
142  };
143 };
144 
145 #endif /* DDC_THREAD_QUEUE_H */
146 
147 /*--- emacs style variables ---
148  * Local Variables:
149  * mode: C++
150  * c-file-style: "ellemtel"
151  * c-basic-offset: 4
152  * tab-width: 8
153  * indent-tabs-mode: nil
154  * End:
155  */
generic thread-safe queue template class should use POD or pointer types for message-type T ...
Definition: ddcThreadQueue.h:36
ddcThreadQueue(size_t max_size=256)
Definition: ddcThreadQueue.h:73
size_t size() const
Definition: ddcThreadQueue.h:136
~ddcThreadQueue()
Definition: ddcThreadQueue.h:82
void add(T item)
Definition: ddcThreadQueue.h:99
size_t m_max_size
maximum number of enqueued messages (default=DDC_DEFAULT_THREAD_QUEUE_SIZE: unlimited) ...
Definition: ddcThreadQueue.h:56
std::list< T > m_queue
underlying message queue
Definition: ddcThreadQueue.h:53
#define DDC_DEFAULT_THREAD_QUEUE_SIZE
Definition: ddcThreadQueue.h:29
pthread_cond_t m_condv_added
pthread condition variable for signalling queue insertions
Definition: ddcThreadQueue.h:62
T ItemT
message item type
Definition: ddcThreadQueue.h:43
pthread_mutex_t m_mutex
pthread mutex for locking queue access
Definition: ddcThreadQueue.h:59
pthread_cond_t m_condv_removed
pthread condition variable for signalling queue removals
Definition: ddcThreadQueue.h:65