Rev 1317 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1309 | runge | 1 | /* |
| 1318 | runge | 2 | * Queue.hpp |
| 1309 | runge | 3 | * |
| 4 | * Created on: Apr 27, 2009 |
||
| 5 | * Author: Mattias Runge |
||
| 6 | */ |
||
| 7 | |||
| 1318 | runge | 8 | #ifndef QUEUE_HPP_ |
| 9 | #define QUEUE_HPP_ |
||
| 1309 | runge | 10 | |
| 11 | #include <queue> |
||
| 12 | #include <boost/thread.hpp> |
||
| 13 | #include <boost/thread/mutex.hpp> |
||
| 14 | #include <boost/thread/locks.hpp> |
||
| 15 | |||
| 1318 | runge | 16 | /* Note on templates |
| 17 | * Can not have h/cpp structure because we will get linker problems |
||
| 18 | * Read more here: http://www.parashift.com/c++-faq-lite/templates.html#faq-35.13 |
||
| 19 | */ |
||
| 20 | |||
| 1309 | runge | 21 | namespace atom { |
| 1317 | runge | 22 | namespace thread { |
| 1309 | runge | 23 | |
| 1317 | runge | 24 | template<typename T> |
| 25 | class Queue |
||
| 1309 | runge | 26 | { |
| 27 | typedef boost::mutex::scoped_lock lock; |
||
| 28 | |||
| 29 | public: |
||
| 1318 | runge | 30 | Queue() |
| 31 | { |
||
| 32 | } |
||
| 1309 | runge | 33 | |
| 1318 | runge | 34 | ~Queue() |
| 35 | { |
||
| 36 | } |
||
| 1309 | runge | 37 | |
| 1318 | runge | 38 | void push(T item) |
| 39 | { |
||
| 40 | lock guard(this->myMutex); |
||
| 41 | this->myQueue.push(item); |
||
| 42 | } |
||
| 43 | |||
| 44 | T pop() |
||
| 45 | { |
||
| 46 | lock guard(this->myMutex); |
||
| 47 | T item = this->myQueue.front(); |
||
| 48 | this->myQueue.pop(); |
||
| 49 | return item; |
||
| 50 | } |
||
| 51 | |||
| 52 | unsigned int size() |
||
| 53 | { |
||
| 54 | lock guard(this->myMutex); |
||
| 55 | return this->myQueue.size(); |
||
| 56 | } |
||
| 57 | |||
| 1309 | runge | 58 | private: |
| 1317 | runge | 59 | std::queue<T> myQueue; |
| 60 | boost::mutex myMutex; |
||
| 1309 | runge | 61 | }; |
| 62 | |||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 1317 | runge | 66 | #endif /* QUEUE_H_ */ |
| 1309 | runge | 67 |