]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/lockfree/examples/spsc_queue.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / lockfree / examples / spsc_queue.cpp
1 // Copyright (C) 2009 Tim Blechmann
2 //
3 // Distributed under the Boost Software License, Version 1.0. (See
4 // accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6
7 //[spsc_queue_example
8 #include <boost/thread/thread.hpp>
9 #include <boost/lockfree/spsc_queue.hpp>
10 #include <iostream>
11
12 #include <boost/atomic.hpp>
13
14 int producer_count = 0;
15 boost::atomic_int consumer_count (0);
16
17 boost::lockfree::spsc_queue<int, boost::lockfree::capacity<1024> > spsc_queue;
18
19 const int iterations = 10000000;
20
21 void producer(void)
22 {
23 for (int i = 0; i != iterations; ++i) {
24 int value = ++producer_count;
25 while (!spsc_queue.push(value))
26 ;
27 }
28 }
29
30 boost::atomic<bool> done (false);
31
32 void consumer(void)
33 {
34 int value;
35 while (!done) {
36 while (spsc_queue.pop(value))
37 ++consumer_count;
38 }
39
40 while (spsc_queue.pop(value))
41 ++consumer_count;
42 }
43
44 int main(int argc, char* argv[])
45 {
46 using namespace std;
47 cout << "boost::lockfree::queue is ";
48 if (!spsc_queue.is_lock_free())
49 cout << "not ";
50 cout << "lockfree" << endl;
51
52 boost::thread producer_thread(producer);
53 boost::thread consumer_thread(consumer);
54
55 producer_thread.join();
56 done = true;
57 consumer_thread.join();
58
59 cout << "produced " << producer_count << " objects." << endl;
60 cout << "consumed " << consumer_count << " objects." << endl;
61 }
62 //]