00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00029 #ifndef MESSAGE_QUEUE_HPP
00030 #define MESSAGE_QUEUE_HPP
00031
00032 #include <boost/thread/pthread/mutex.hpp>
00033 #include <boost/thread/pthread/condition_variable.hpp>
00034 #include <queue>
00035 using namespace std;
00036
00040 template< typename T>
00041 class message_queue{
00042 private:
00043 message_queue();
00044
00045 queue<T> m_q;
00046 unsigned int m_size;
00047
00048 boost::mutex m_global_lock;
00049 boost::condition_variable cond_full;
00050 boost::condition_variable cond_empty;
00051 public:
00056 message_queue(int size);
00057
00061 bool is_empty();
00062
00066 unsigned int current_size();
00067
00071 int max_size();
00072
00076 void enqueue(T t);
00077
00081 T dequeue(void);
00082 };
00083
00084 template< typename T>
00085 message_queue<T>::message_queue(int size){
00086 m_size = size;
00087 }
00088
00089 template< typename T>
00090 bool message_queue<T>::is_empty(){
00091 boost::lock_guard<boost::mutex> lock(m_global_lock);
00092
00093 return m_q.empty();
00094 }
00095
00096
00097 template< typename T>
00098 unsigned int message_queue<T>::current_size(){
00099 boost::lock_guard<boost::mutex> lock(m_global_lock);
00100
00101 return m_q.size();
00102 }
00103
00104 template< typename T>
00105 int message_queue<T>::max_size(){
00106 return m_size;
00107 }
00108
00109 template< typename T>
00110 void message_queue<T>::enqueue(T t){
00111
00112 boost::unique_lock<boost::mutex> lock(m_global_lock);
00113 while(m_q.size() >= m_size){
00114 cond_full.wait(lock);
00115 }
00116
00117 m_q.push(t);
00118 cond_empty.notify_one();
00119 }
00120
00121 template< typename T>
00122 T message_queue<T>::dequeue(void){
00123
00124 boost::unique_lock<boost::mutex> lock(m_global_lock);
00125 while(m_q.size() == 0){
00126 cond_empty.wait(lock);
00127 }
00128
00129 T t= m_q.front();
00130 m_q.pop();
00131 cond_full.notify_one();
00132 return t;
00133 }
00134
00135 #endif // MESSAGE_QUEUE_HPP
00136