41 #ifndef INCLUDE_OKVIS_THREADSAFE_THREADSAFEQUEUE_HPP_
42 #define INCLUDE_OKVIS_THREADSAFE_THREADSAFEQUEUE_HPP_
50 #include <glog/logging.h>
56 namespace threadsafe {
65 virtual size_t Size()
const = 0;
66 virtual bool Empty()
const = 0;
73 template<
typename QueueType>
75 friend bool test_funcs(
void* (*)(
void*),
void* (*)(
void*),
76 const std::string&,
bool);
89 pthread_mutex_init(&
mutex_, NULL);
98 pthread_mutex_destroy(&
mutex_);
116 void Push(
const QueueType& value) {
122 pthread_mutex_lock(&
mutex_);
125 pthread_mutex_unlock(&
mutex_);
129 virtual size_t Size() const final {
130 pthread_mutex_lock(&
mutex_);
131 size_t size =
queue_.size();
132 pthread_mutex_unlock(&
mutex_);
138 pthread_mutex_lock(&
mutex_);
139 bool empty =
queue_.empty();
140 pthread_mutex_unlock(&
mutex_);
150 pthread_mutex_lock(&
mutex_);
151 size_t size =
queue_.size();
152 if (size >= max_queue_size) {
155 if (size >= max_queue_size) {
156 pthread_mutex_unlock(&
mutex_);
161 pthread_mutex_unlock(&
mutex_);
172 size_t max_queue_size) {
173 pthread_mutex_lock(&
mutex_);
175 if (
queue_.size() >= max_queue_size) {
181 pthread_mutex_unlock(&
mutex_);
190 bool Pop(QueueType* value) {
200 CHECK_NOTNULL(value);
202 pthread_mutex_lock(&
mutex_);
207 pthread_mutex_unlock(&
mutex_);
210 QueueType _value =
queue_.front();
213 pthread_mutex_unlock(&
mutex_);
226 CHECK_NOTNULL(value);
227 pthread_mutex_lock(&
mutex_);
229 pthread_mutex_unlock(&
mutex_);
234 pthread_mutex_unlock(&
mutex_);
247 bool PopTimeout(QueueType* value, int64_t timeout_nanoseconds) {
248 CHECK_NOTNULL(value);
249 pthread_mutex_lock(&
mutex_);
253 gettimeofday(&tv, NULL);
254 ts.tv_sec = tv.tv_sec;
255 ts.tv_nsec = tv.tv_usec * 1e3 + timeout_nanoseconds;
259 pthread_mutex_unlock(&
mutex_);
262 QueueType _value =
queue_.front();
265 pthread_mutex_unlock(&
mutex_);
278 CHECK_NOTNULL(value);
279 pthread_mutex_lock(&
mutex_);
281 pthread_mutex_unlock(&
mutex_);
286 pthread_mutex_unlock(&
mutex_);
297 CHECK_NOTNULL(value);
299 pthread_mutex_lock(&
mutex_);
304 pthread_mutex_unlock(&
mutex_);
308 pthread_mutex_unlock(&
mutex_);
322 CHECK_NOTNULL(value);
323 pthread_mutex_lock(&
mutex_);
325 pthread_mutex_unlock(&
mutex_);
330 pthread_mutex_unlock(&
mutex_);
347 #endif // INCLUDE_OKVIS_THREADSAFE_THREADSAFEQUEUE_HPP_
ThreadSafeQueue()
Constructor.
Definition: ThreadsafeQueue.hpp:87
virtual ~ThreadSafeQueue()
Destructor.
Definition: ThreadsafeQueue.hpp:95
pthread_mutex_t mutex_
The queue mutex.
Definition: ThreadsafeQueue.hpp:335
bool PushBlockingIfFull(const QueueType &value, size_t max_queue_size)
Push to the queue if the size is less than max_queue_size, else block.
Definition: ThreadsafeQueue.hpp:148
bool PushNonBlockingDroppingIfFull(const QueueType &value, size_t max_queue_size)
Push to the queue. If full, drop the oldest entry.
Definition: ThreadsafeQueue.hpp:171
void PushNonBlocking(const QueueType &value)
Push to the queue.
Definition: ThreadsafeQueue.hpp:121
bool PopBlocking(QueueType *value)
Get the oldest entry still in the queue. Blocking if queue is empty.
Definition: ThreadsafeQueue.hpp:199
virtual void Shutdown() final
Tell the queue shut down. This will notify all threads to wake up.
Definition: ThreadsafeQueue.hpp:104
virtual ~ThreadSafeQueueBase()
Definition: ThreadsafeQueue.hpp:61
bool PopNonBlocking(QueueType *value)
Get the oldest entry still in the queue. If queue is empty value is not altered.
Definition: ThreadsafeQueue.hpp:225
virtual bool Empty() const final
Return true if the queue is empty.
Definition: ThreadsafeQueue.hpp:137
pthread_cond_t condition_empty_
Condition variable to wait and signal that queue is not empty.
Definition: ThreadsafeQueue.hpp:336
void Push(const QueueType &value)
Push non-blocking to the queue.
Definition: ThreadsafeQueue.hpp:116
virtual void NotifyAll() const final
Notify all waiting threads. Only used in destructor and when shutting down.
Definition: ThreadsafeQueue.hpp:81
bool getCopyOfFront(QueueType *value)
Get a copy of the front / oldest element in the queue. If queue is empty value is not altered...
Definition: ThreadsafeQueue.hpp:277
virtual bool Empty() const =0
bool getCopyOfFrontBlocking(QueueType *value)
Get a copy of the front / oldest element in the queue. Blocking if queue is empty. The queue itself is not changed, i.e. the returned element is still in the queue.
Definition: ThreadsafeQueue.hpp:296
virtual void NotifyAll() const =0
virtual void Resume() final
Tell the queue to resume after a shutdown request.
Definition: ThreadsafeQueue.hpp:110
Definition: ThreadsafeQueue.hpp:58
ThreadSafeQueueBase()=default
bool PopTimeout(QueueType *value, int64_t timeout_nanoseconds)
Get the oldest entry still in the queue. If the queue is empty wait for a given amount of time...
Definition: ThreadsafeQueue.hpp:247
Class that implements a threadsafe FIFO queue.
Definition: ThreadsafeQueue.hpp:74
bool getCopyOfBack(QueueType *value)
Get a copy of the back / newest element in the queue. If queue is empty value is not altered...
Definition: ThreadsafeQueue.hpp:321
virtual void Shutdown()=0
friend bool test_funcs(void *(*)(void *), void *(*)(void *), const std::string &, bool)
bool Pop(QueueType *value)
Get the oldest entry still in the queue. Blocking if queue is empty.
Definition: ThreadsafeQueue.hpp:190
std::atomic_bool shutdown_
Flag if shutdown is requested.
Definition: ThreadsafeQueue.hpp:339
virtual size_t Size() const final
Return the size of the queue.
Definition: ThreadsafeQueue.hpp:129
virtual size_t Size() const =0
pthread_cond_t condition_full_
Condition variable to wait and signal when an element is popped.
Definition: ThreadsafeQueue.hpp:337
std::queue< QueueType > queue_
Actual queue.
Definition: ThreadsafeQueue.hpp:338