template_queue.h
Go to the documentation of this file.
1 #ifndef TEMPLATE_QUEUE_H
2 #define TEMPLATE_QUEUE_H
3 
4 #include <queue>
5 #include <boost/thread.hpp>
6 #include <boost/chrono.hpp>
7 #include <boost/thread.hpp>
8 #include <boost/chrono.hpp>
9 #include <iostream>
10 
11 template<typename T>
12 class Queue
13 {
14 public:
15 
22  {
23  int retVal = 0;
24  boost::mutex::scoped_lock mlock(mutex_);
25  retVal = queue_.size();
26  return (retVal);
27  }
28 
29 
30  bool isQueueEmpty()
31  {
32  bool retVal = false;
33  boost::mutex::scoped_lock mlock(mutex_);
34  retVal = queue_.empty();
35  return (retVal);
36  }
37 
38  bool waitForIncomingObject(int timeOutInMs)
39  {
40  boost::mutex::scoped_lock mlock(mutex_);
41  bool ret = true;
42  boost::posix_time::time_duration td = boost::posix_time::millisec(timeOutInMs);
43  while (queue_.empty() && (ret == true))
44  {
45  ret = cond_.timed_wait(mlock, td);
46  }
47  return (ret);
48  }
49 
50  T pop()
51  {
52  boost::mutex::scoped_lock mlock(mutex_);
53  while (queue_.empty())
54  {
55  cond_.wait(mlock);
56  }
57  T item = queue_.front();
58  queue_.pop();
59  return item;
60  }
61 
62  void pop(T &item)
63  {
64  boost::mutex::scoped_lock mlock(mutex_);
65  while (queue_.empty())
66  {
67  cond_.wait(mlock);
68  }
69  item = queue_.front();
70  queue_.pop();
71  }
72 
73  void push(const T &item)
74  {
75  boost::mutex::scoped_lock mlock(mutex_);
76  queue_.push(item);
77  mlock.unlock();
78  cond_.notify_one();
79  }
80 
81  void push(T &item)
82  {
83  boost::mutex::scoped_lock mlock(mutex_);
84  queue_.push(item);
85  mlock.unlock();
86  cond_.notify_one();
87  }
88 
89 private:
90  std::queue<T> queue_;
91  boost::mutex mutex_;
92  boost::condition_variable cond_;
93 };
94 
95 #endif
Queue::getNumberOfEntriesInQueue
int getNumberOfEntriesInQueue()
get number of entries in queue
Definition: template_queue.h:21
Queue::queue_
std::queue< T > queue_
Definition: template_queue.h:90
Queue::cond_
boost::condition_variable cond_
Definition: template_queue.h:92
Queue::waitForIncomingObject
bool waitForIncomingObject(int timeOutInMs)
Definition: template_queue.h:38
Queue::pop
void pop(T &item)
Definition: template_queue.h:62
Queue::pop
T pop()
Definition: template_queue.h:50
Queue
Definition: template_queue.h:12
Queue::isQueueEmpty
bool isQueueEmpty()
Definition: template_queue.h:30
Queue::mutex_
boost::mutex mutex_
Definition: template_queue.h:91
Queue::push
void push(T &item)
Definition: template_queue.h:81
Queue::push
void push(const T &item)
Definition: template_queue.h:73


sick_scan
Author(s): Michael Lehning , Jochen Sprickerhof , Martin Günther
autogenerated on Thu Sep 8 2022 02:30:19