Queue.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* -*- C++ -*- */
  2. /****************************************************************************
  3. ** Copyright (c) 2001-2014
  4. **
  5. ** This file is part of the QuickFIX FIX Engine
  6. **
  7. ** This file may be distributed under the terms of the quickfixengine.org
  8. ** license as defined by quickfixengine.org and appearing in the file
  9. ** LICENSE included in the packaging of this file.
  10. **
  11. ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  12. ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  13. **
  14. ** See http://www.quickfixengine.org/LICENSE for licensing information.
  15. **
  16. ** Contact ask@quickfixengine.org if any conditions of this licensing are
  17. ** not clear to you.
  18. **
  19. ****************************************************************************/
  20. #ifndef FIX_QUEUE_H
  21. #define FIX_QUEUE_H
  22. #include "Utility.h"
  23. #include "Event.h"
  24. #include "Mutex.h"
  25. #include <queue>
  26. namespace FIX
  27. {
  28. /// A thread safe monitored queue
  29. template < typename T > class Queue
  30. {
  31. public:
  32. void push( const T& value )
  33. {
  34. Locker locker( m_mutex );
  35. m_queue.push( value );
  36. signal();
  37. }
  38. bool pop( T& value )
  39. {
  40. Locker locker( m_mutex );
  41. if ( !m_queue.size() ) return false;
  42. value = m_queue.front();
  43. m_queue.pop();
  44. return true;
  45. }
  46. int size()
  47. {
  48. Locker locker( m_mutex );
  49. return m_queue.size();
  50. }
  51. void wait( double s )
  52. {
  53. m_event.wait( s );
  54. }
  55. void signal()
  56. {
  57. m_event.signal();
  58. }
  59. private:
  60. Event m_event;
  61. Mutex m_mutex;
  62. std::queue < T > m_queue;
  63. };
  64. }
  65. #endif