pointerQueue.h Source File

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
pointerQueue.h
Go to the documentation of this file.
1 /* queue.h
2 */
3 #ifndef OSL_POINTERQUEUE_H
4 #define OSL_POINTERQUEUE_H
5
6 #include <deque>
7 #include <boost/thread/thread.hpp>
8 #include <boost/thread/condition.hpp>
9
10 namespace osl
11 {
12 namespace misc
13 {
14 template <class T>
15 class PointerQueue
16 {
17 typedef std::deque<boost::shared_ptr<T> > queue_t;
18 queue_t data;
19 typedef boost::mutex Mutex;
20 mutable Mutex mutex;
21 volatile bool finish;
22 boost::condition condition;
23 public:
24 PointerQueue() : finish(false)
25 {
26 }
27 ~PointerQueue()
28 {
29 if (! finish)
30 quit();
31 }
32 size_t size() const
33 {
34 Mutex::scoped_lock lk(mutex);
35 return data.size();
36 }
37 void push_back(boost::shared_ptr<T>& ptr)
38 {
39 Mutex::scoped_lock lk(mutex);
40 data.push_back(boost::shared_ptr<T>());
41 data.back().swap(ptr);
42 condition.notify_one();
43 }
44 private:
45 boost::shared_ptr<T> pop_front_in_lock()
46 {
47 if (! data.empty())
48 {
49 boost::shared_ptr<T> result = data.front();
50 data.pop_front();
51 return result;
52 }
53 return boost::shared_ptr<T>();
54 }
55 public:
56 boost::shared_ptr<T> pop_front_non_block()
57 {
58 Mutex::scoped_lock lk(mutex);
59 return pop_front_in_lock();
60 }
61 boost::shared_ptr<T> pop_front()
62 {
63 while (! finish)
64 {
65 Mutex::scoped_lock lk(mutex);
66 boost::shared_ptr<T> result = pop_front_in_lock();
67 if (result.get() || finish)
68 return result;
69 condition.wait(lk);
70 }
71 return boost::shared_ptr<T>();
72 }
73 void quit(int seconds=0)
74 {
75 finish = true;
76 if (seconds > 0)
77 boost::this_thread::sleep(boost::posix_time::seconds(seconds));
78 Mutex::scoped_lock lk(mutex);
79 condition.notify_all();
80 }
81 };
82 }
83 }
84
85 #endif /* OSL_POINTERQUEUE_H */
86 // ;;; Local Variables:
87 // ;;; mode:c++
88 // ;;; c-basic-offset:2
89 // ;;; End: