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