1 #include <boost/lockfree/ringbuffer.hpp>
4 #define BOOST_TEST_MODULE lockfree_tests
5 #include <boost/test/included/unit_test.hpp>
8 #include <boost/thread.hpp>
13 #include "test_helpers.hpp"
15 using namespace boost
;
16 using namespace boost::lockfree
;
20 BOOST_AUTO_TEST_CASE( simple_ringbuffer_test
)
22 ringbuffer
<int, 64> f
;
24 BOOST_REQUIRE(f
.empty());
30 BOOST_REQUIRE(f
.dequeue(&i1
));
31 BOOST_REQUIRE_EQUAL(i1
, 1);
33 BOOST_REQUIRE(f
.dequeue(&i2
));
34 BOOST_REQUIRE_EQUAL(i2
, 2);
35 BOOST_REQUIRE(f
.empty());
39 struct ringbuffer_tester
41 ringbuffer
<int, 128> sf
;
43 atomic
<long> ringbuffer_cnt
, received_nodes
;
45 static_hashed_set
<int, 1<<16 > working_set
;
47 static const uint nodes_per_thread
= 20000000;
49 static const int reader_threads
= 1;
50 static const int writer_threads
= 1;
52 ringbuffer_tester(void):
53 ringbuffer_cnt(0), received_nodes(0)
58 for (uint i
= 0; i
!= nodes_per_thread
; ++i
)
60 while(ringbuffer_cnt
> 10000)
63 int id
= generate_id
<int>();
65 working_set
.insert(id
);
67 while (sf
.enqueue(id
) == false)
76 bool get_element(void)
80 bool success
= sf
.dequeue(&data
);
86 bool erased
= working_set
.erase(data
);
94 volatile bool running
;
100 bool success
= get_element();
101 if (not running
and not success
)
115 BOOST_REQUIRE(sf
.empty());
116 for (int i
= 0; i
!= reader_threads
; ++i
)
117 reader
.create_thread(boost::bind(&ringbuffer_tester::get
, this));
119 for (int i
= 0; i
!= writer_threads
; ++i
)
120 writer
.create_thread(boost::bind(&ringbuffer_tester::add
, this));
121 cout
<< "reader and writer threads created" << endl
;
124 cout
<< "writer threads joined. waiting for readers to finish" << endl
;
129 BOOST_REQUIRE_EQUAL(received_nodes
, writer_threads
* nodes_per_thread
);
130 BOOST_REQUIRE_EQUAL(ringbuffer_cnt
, 0);
131 BOOST_REQUIRE(sf
.empty());
132 BOOST_REQUIRE(working_set
.count_nodes() == 0);
138 BOOST_AUTO_TEST_CASE( ringbuffer_test_caching
)
140 ringbuffer_tester test1
;