circular_buffer_bound_example.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright 2003-2008 Jan Gaspar.
  2. // Copyright 2013 Paul A. Bristow. Added some Quickbook snippet markers.
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See the accompanying file LICENSE_1_0.txt
  5. // or a copy at <http://www.boost.org/LICENSE_1_0.txt>.)
  6. //[circular_buffer_bound_example_1
  7. /*`
  8. This example shows how the `circular_buffer` can be utilized
  9. as an underlying container of the bounded buffer.
  10. */
  11. #include <boost/circular_buffer.hpp>
  12. #include <boost/thread/mutex.hpp>
  13. #include <boost/thread/condition.hpp>
  14. #include <boost/thread/thread.hpp>
  15. #include <boost/call_traits.hpp>
  16. #include <boost/bind.hpp>
  17. #include <boost/timer/timer.hpp> // for auto_cpu_timer
  18. #include <iostream>
  19. template <class T>
  20. class bounded_buffer
  21. {
  22. public:
  23. typedef boost::circular_buffer<T> container_type;
  24. typedef typename container_type::size_type size_type;
  25. typedef typename container_type::value_type value_type;
  26. typedef typename boost::call_traits<value_type>::param_type param_type;
  27. explicit bounded_buffer(size_type capacity) : m_unread(0), m_container(capacity) {}
  28. void push_front(typename boost::call_traits<value_type>::param_type item)
  29. { // `param_type` represents the "best" way to pass a parameter of type `value_type` to a method.
  30. boost::mutex::scoped_lock lock(m_mutex);
  31. m_not_full.wait(lock, boost::bind(&bounded_buffer<value_type>::is_not_full, this));
  32. m_container.push_front(item);
  33. ++m_unread;
  34. lock.unlock();
  35. m_not_empty.notify_one();
  36. }
  37. void pop_back(value_type* pItem) {
  38. boost::mutex::scoped_lock lock(m_mutex);
  39. m_not_empty.wait(lock, boost::bind(&bounded_buffer<value_type>::is_not_empty, this));
  40. *pItem = m_container[--m_unread];
  41. lock.unlock();
  42. m_not_full.notify_one();
  43. }
  44. private:
  45. bounded_buffer(const bounded_buffer&); // Disabled copy constructor.
  46. bounded_buffer& operator = (const bounded_buffer&); // Disabled assign operator.
  47. bool is_not_empty() const { return m_unread > 0; }
  48. bool is_not_full() const { return m_unread < m_container.capacity(); }
  49. size_type m_unread;
  50. container_type m_container;
  51. boost::mutex m_mutex;
  52. boost::condition m_not_empty;
  53. boost::condition m_not_full;
  54. }; //
  55. //] [/circular_buffer_bound_example_1]
  56. const unsigned long queue_size = 1000L;
  57. const unsigned long total_elements = queue_size * 1000L;
  58. //[circular_buffer_bound_example_2]
  59. /*`To demonstrate, create two classes to exercise the buffer.
  60. The producer class fills the buffer with elements.
  61. The consumer class consumes the buffer contents.
  62. */
  63. template<class Buffer>
  64. class Producer
  65. {
  66. typedef typename Buffer::value_type value_type;
  67. Buffer* m_container;
  68. public:
  69. Producer(Buffer* buffer) : m_container(buffer)
  70. {}
  71. void operator()()
  72. {
  73. for (unsigned long i = 0L; i < total_elements; ++i)
  74. {
  75. m_container->push_front(value_type());
  76. }
  77. }
  78. };
  79. template<class Buffer>
  80. class Consumer
  81. {
  82. typedef typename Buffer::value_type value_type;
  83. Buffer* m_container;
  84. value_type m_item;
  85. public:
  86. Consumer(Buffer* buffer) : m_container(buffer)
  87. {}
  88. void operator()()
  89. {
  90. for (unsigned long i = 0L; i < total_elements; ++i)
  91. {
  92. m_container->pop_back(&m_item);
  93. }
  94. }
  95. };
  96. /*`Create a first-int first-out test of the bound_buffer.
  97. Include a call to boost::progress_timer
  98. [@http://www.boost.org/doc/libs/1_53_0/libs/timer/doc/cpu_timers.html CPU timer]
  99. */
  100. template<class Buffer>
  101. void fifo_test(Buffer* buffer)
  102. {
  103. // Start of timing.
  104. boost::timer::auto_cpu_timer progress;
  105. // Initialize the buffer with some values before launching producer and consumer threads.
  106. for (unsigned long i = queue_size / 2L; i > 0; --i)
  107. {
  108. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x581))
  109. buffer->push_front(Buffer::value_type());
  110. #else
  111. buffer->push_front(BOOST_DEDUCED_TYPENAME Buffer::value_type());
  112. #endif
  113. }
  114. // Construct the threads.
  115. Consumer<Buffer> consumer(buffer);
  116. Producer<Buffer> producer(buffer);
  117. // Start the threads.
  118. boost::thread consume(consumer);
  119. boost::thread produce(producer);
  120. // Wait for completion.
  121. consume.join();
  122. produce.join();
  123. // End of timing.
  124. // destructor of boost::timer::auto_cpu_timer will output the time to std::cout.
  125. }
  126. //] [/circular_buffer_bound_example_2]
  127. int main()
  128. {
  129. //[circular_buffer_bound_example_3]
  130. //`Construct a bounded_buffer to hold the chosen type, here int.
  131. bounded_buffer<int> bb_int(queue_size);
  132. std::cout << "Testing bounded_buffer<int> ";
  133. //`Start the fifo test.
  134. fifo_test(&bb_int);
  135. //` destructor of boost::timer::auto_cpu_timer will output the time to std::cout
  136. //] [/circular_buffer_bound_example_3]
  137. return 0;
  138. } // int main()
  139. /*
  140. //[circular_buffer_bound_output
  141. Description: Autorun "J:\Cpp\Misc\Debug\circular_buffer_bound_example.exe"
  142. Testing bounded_buffer<int> 15.010692s wall, 9.188459s user + 7.207246s system = 16.395705s CPU (109.2%)
  143. //] [/circular_buffer_bound_output]
  144. */