Subversion Repositories HomeAutomation

Rev

Rev 1317 | Go to most recent revision | 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.     typedef boost::mutex::scoped_lock lock;
  28.  
  29. public:
  30.     Queue()
  31.     {
  32.     }
  33.  
  34.     ~Queue()
  35.     {
  36.     }
  37.  
  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.  
  58. private:
  59.     std::queue<T> myQueue;
  60.     boost::mutex myMutex;
  61. };
  62.  
  63. }
  64. }
  65.  
  66. #endif /* QUEUE_H_ */
  67.  
  68.