Go to the documentation of this file.00001 #ifndef TEMPLATE_QUEUE_H
00002 #define TEMPLATE_QUEUE_H
00003 #include <queue>
00004 #include <boost/thread.hpp>
00005 #include <boost/chrono.hpp>
00006 #include <boost/thread.hpp>
00007 #include <boost/chrono.hpp>
00008 #include <iostream>
00009 template <typename T>
00010 class Queue
00011 {
00012 public:
00013
00019 int getNumberOfEntriesInQueue()
00020 {
00021 int retVal = 0;
00022 boost::mutex::scoped_lock mlock(mutex_);
00023 retVal = queue_.size();
00024 return(retVal);
00025 }
00026
00027
00028
00029 bool isQueueEmpty()
00030 {
00031 bool retVal = false;
00032 boost::mutex::scoped_lock mlock(mutex_);
00033 retVal = queue_.empty();
00034 return(retVal);
00035 }
00036
00037 bool waitForIncomingObject(int timeOutInMs)
00038 {
00039 boost::mutex::scoped_lock mlock(mutex_);
00040 bool ret = true;
00041 boost::posix_time::time_duration td = boost::posix_time::millisec(timeOutInMs);
00042 while (queue_.empty() && (ret == true))
00043 {
00044 ret = cond_.timed_wait(mlock, td);
00045 }
00046 return(ret);
00047 }
00048
00049 T pop()
00050 {
00051 boost::mutex::scoped_lock mlock(mutex_);
00052 while (queue_.empty())
00053 {
00054 cond_.wait(mlock);
00055 }
00056 T item = queue_.front();
00057 queue_.pop();
00058 return item;
00059 }
00060
00061 void pop(T& item)
00062 {
00063 boost::mutex::scoped_lock mlock(mutex_);
00064 while (queue_.empty())
00065 {
00066 cond_.wait(mlock);
00067 }
00068 item = queue_.front();
00069 queue_.pop();
00070 }
00071
00072 void push(const T& item)
00073 {
00074 boost::mutex::scoped_lock mlock(mutex_);
00075 queue_.push(item);
00076 mlock.unlock();
00077 cond_.notify_one();
00078 }
00079
00080 void push(T& item)
00081 {
00082 boost::mutex::scoped_lock mlock(mutex_);
00083 queue_.push(item);
00084 mlock.unlock();
00085 cond_.notify_one();
00086 }
00087
00088 private:
00089 std::queue<T> queue_;
00090 boost::mutex mutex_;
00091 boost::condition_variable cond_;
00092 };
00093 #endif