Go to the documentation of this file.00001
00002
00003 #ifndef OSL_POINTERQUEUE_H
00004 #define OSL_POINTERQUEUE_H
00005
00006 #include <deque>
00007 #include <boost/thread/thread.hpp>
00008 #include <boost/thread/condition.hpp>
00009
00010 namespace osl
00011 {
00012 namespace misc
00013 {
00014 template <class T>
00015 class PointerQueue
00016 {
00017 typedef std::deque<boost::shared_ptr<T> > queue_t;
00018 queue_t data;
00019 typedef boost::mutex Mutex;
00020 mutable Mutex mutex;
00021 volatile bool finish;
00022 boost::condition condition;
00023 public:
00024 PointerQueue() : finish(false)
00025 {
00026 }
00027 ~PointerQueue()
00028 {
00029 if (! finish)
00030 quit();
00031 }
00032 size_t size() const
00033 {
00034 Mutex::scoped_lock lk(mutex);
00035 return data.size();
00036 }
00037 void push_back(boost::shared_ptr<T>& ptr)
00038 {
00039 Mutex::scoped_lock lk(mutex);
00040 data.push_back(boost::shared_ptr<T>());
00041 data.back().swap(ptr);
00042 condition.notify_one();
00043 }
00044 private:
00045 boost::shared_ptr<T> pop_front_in_lock()
00046 {
00047 if (! data.empty())
00048 {
00049 boost::shared_ptr<T> result = data.front();
00050 data.pop_front();
00051 return result;
00052 }
00053 return boost::shared_ptr<T>();
00054 }
00055 public:
00056 boost::shared_ptr<T> pop_front_non_block()
00057 {
00058 Mutex::scoped_lock lk(mutex);
00059 return pop_front_in_lock();
00060 }
00061 boost::shared_ptr<T> pop_front()
00062 {
00063 while (! finish)
00064 {
00065 Mutex::scoped_lock lk(mutex);
00066 boost::shared_ptr<T> result = pop_front_in_lock();
00067 if (result.get() || finish)
00068 return result;
00069 condition.wait(lk);
00070 }
00071 return boost::shared_ptr<T>();
00072 }
00073 void quit(int seconds=0)
00074 {
00075 finish = true;
00076 if (seconds > 0)
00077 boost::this_thread::sleep(boost::posix_time::seconds(seconds));
00078 Mutex::scoped_lock lk(mutex);
00079 condition.notify_all();
00080 }
00081 };
00082 }
00083 }
00084
00085 #endif
00086
00087
00088
00089