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
void push(T &item)
std::queue< T > queue_
boost::condition_variable cond_
bool waitForIncomingObject(int timeOutInMs)
bool isQueueEmpty()
void pop(T &item)
void push(const T &item)
boost::mutex mutex_
int getNumberOfEntriesInQueue()
get number of entries in queue


sick_scan
Author(s): Michael Lehning , Jochen Sprickerhof , Martin Günther
autogenerated on Wed May 5 2021 03:05:48