]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/lockfree/examples/spsc_queue.cpp
1 // Copyright (C) 2009 Tim Blechmann
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)
8 #include <boost/thread/thread.hpp>
9 #include <boost/lockfree/spsc_queue.hpp>
12 #include <boost/atomic.hpp>
14 int producer_count
= 0;
15 boost::atomic_int
consumer_count (0);
17 boost::lockfree::spsc_queue
<int, boost::lockfree::capacity
<1024> > spsc_queue
;
19 const int iterations
= 10000000;
23 for (int i
= 0; i
!= iterations
; ++i
) {
24 int value
= ++producer_count
;
25 while (!spsc_queue
.push(value
))
30 boost::atomic
<bool> done (false);
36 while (spsc_queue
.pop(value
))
40 while (spsc_queue
.pop(value
))
44 int main(int argc
, char* argv
[])
47 cout
<< "boost::lockfree::queue is ";
48 if (!spsc_queue
.is_lock_free())
50 cout
<< "lockfree" << endl
;
52 boost::thread
producer_thread(producer
);
53 boost::thread
consumer_thread(consumer
);
55 producer_thread
.join();
57 consumer_thread
.join();
59 cout
<< "produced " << producer_count
<< " objects." << endl
;
60 cout
<< "consumed " << consumer_count
<< " objects." << endl
;