Subversion Repositories HomeAutomation

Rev

Rev 1318 | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /*
  2.  * Queue.hpp
  3.  *
  4.  *  Created on: Apr 27, 2009
  5.  *      Author: Mattias Runge
  6.  */
  7.  
  8. #ifndef QUEUE_HPP_
  9. #define QUEUE_HPP_
  10.  
  11. #include <queue>
  12. #include <boost/thread.hpp>
  13. #include <boost/thread/mutex.hpp>
  14. #include <boost/thread/locks.hpp>
  15.  
  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.  
  21. namespace atom {
  22. namespace thread {
  23.  
  24. template<typename T>
  25. class Queue
  26. {
  27. public:
  28.     Queue() { }
  29.     ~Queue() { }
  30.  
  31.     void Push(T item)
  32.     {
  33.         boost::mutex::scoped_lock guard(this->guard_mutex_);
  34.  
  35.         this->queue_.push(item);
  36.     }
  37.  
  38.     T pop()
  39.     {
  40.         boost::mutex::scoped_lock guard(this->guard_mutex_);
  41.  
  42.         T item = this->queue_.front();
  43.         this->queue_.pop();
  44.         return item;
  45.     }
  46.  
  47.     unsigned int size()
  48.     {
  49.         boost::mutex::scoped_lock guard(this->guard_mutex_);
  50.  
  51.         return this->queue_.size();
  52.     }
  53.  
  54. private:
  55.     std::queue<T> queue_;
  56.     boost::mutex guard_mutex_;
  57. };
  58.  
  59. } // namespace thread
  60. } // namespace atom
  61.  
  62. #endif /* QUEUE_HPP_ */
  63.  
  64.