Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00013 #ifndef THREAD_QUEUE_H
00014 #define THREAD_QUEUE_H
00015
00016 #include <boost/thread.hpp>
00017 #include <queue>
00018 #include <iostream>
00019
00025 template<typename T>
00026 class thread_queue {
00027 public:
00029 void push(const T &t) {
00030 boost::unique_lock<boost::mutex> lock(m_mut);
00031 m_queue.push(t);
00032 m_cond.notify_all();
00033 };
00034
00038 void pop() {
00039 boost::unique_lock<boost::mutex> lock(m_mut);
00040 while (m_queue.empty()) {
00041 m_cond.wait(lock);
00042 }
00043 m_queue.pop();
00044 }
00045
00047 T pop_front() {
00048 boost::unique_lock<boost::mutex> lock(m_mut);
00049 while (m_queue.empty()) {
00050 m_cond.wait(lock);
00051 }
00052
00053
00054 T result = m_queue.front();
00055 m_queue.pop();
00056 return result;
00057 }
00058
00065 bool try_pop_front(T &result) {
00066 boost::unique_lock<boost::mutex> lock(m_mut);
00067 if (m_queue.empty()) {
00068 return false;
00069 }
00070 result = m_queue.front();
00071 m_queue.pop();
00072 return true;
00073 }
00074
00076 T& front() {
00077 boost::unique_lock<boost::mutex> lock(m_mut);
00078 while (m_queue.empty()) {
00079 m_cond.wait(lock);
00080 }
00081 return m_queue.front();
00082 }
00083
00085 T& back() {
00086 boost::unique_lock<boost::mutex> lock(m_mut);
00087 while (m_queue.empty()) {
00088 m_cond.wait(lock);
00089 }
00090 return m_queue.back();
00091 }
00092
00094 int size() {
00095 boost::unique_lock<boost::mutex> lock(m_mut);
00096 return m_queue.size();
00097 }
00098
00099 private:
00100 std::queue<T> m_queue;
00101
00102 boost::condition_variable m_cond;
00103 boost::mutex m_mut;
00104 };
00105
00106 #endif // THREAD_QUEUE_H