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