2 #define BOOST_TEST_DYN_LINK
3 #define BOOST_TEST_MAIN
4 #include <boost/test/unit_test.hpp>
6 #include "test_helpers.hpp"
8 #include <boost/lockfree/stack.hpp>
10 #include <boost/thread.hpp>
14 BOOST_AUTO_TEST_CASE( simple_stack_test
)
16 boost::lockfree::stack
<long> stk
;
21 BOOST_REQUIRE(stk
.pop(out
)); BOOST_REQUIRE_EQUAL(out
, 2);
22 BOOST_REQUIRE(stk
.pop(out
)); BOOST_REQUIRE_EQUAL(out
, 1);
23 BOOST_REQUIRE(!stk
.pop(out
));
27 BOOST_REQUIRE(stk
.pop_unsafe(out
)); BOOST_REQUIRE_EQUAL(out
, 2);
28 BOOST_REQUIRE(stk
.pop_unsafe(out
)); BOOST_REQUIRE_EQUAL(out
, 1);
29 BOOST_REQUIRE(!stk
.pop_unsafe(out
));
33 using namespace boost
;
36 template <typename freelist_t
>
39 static const unsigned int buckets
= 1<<10;
40 static const long node_count
= 200000;
41 static const int reader_threads
= 4;
42 static const int writer_threads
= 4;
44 static_hashed_set
<long, buckets
> data
;
45 boost::array
<std::set
<long>, buckets
> returned
;
47 boost::lockfree::detail::atomic
<int> push_count
, pop_count
;
49 boost::lockfree::stack
<long, freelist_t
> stk
;
52 push_count(0), pop_count(0)
59 for (long i
= 0; i
!= node_count
; ++i
)
61 long id
= generate_id
<long>();
63 bool inserted
= data
.insert(id
);
66 while(stk
.push(id
) == false)
72 boost::atomic
<bool> running
;
80 bool got
= stk
.pop(id
);
83 bool erased
= data
.erase(id
);
88 if (not running
.load())
95 BOOST_WARN(stk
.is_lock_free());
102 BOOST_REQUIRE(stk
.empty());
104 for (int i
= 0; i
!= reader_threads
; ++i
)
105 reader
.create_thread(boost::bind(&stack_tester::get_items
, this));
107 for (int i
= 0; i
!= writer_threads
; ++i
)
108 writer
.create_thread(boost::bind(&stack_tester::add_items
, this));
111 cout
<< "threads created" << endl
;
115 cout
<< "writer threads joined, waiting for readers" << endl
;
120 cout
<< "reader threads joined" << endl
;
122 BOOST_REQUIRE_EQUAL(data
.count_nodes(), 0);
123 BOOST_REQUIRE(stk
.empty());
125 BOOST_REQUIRE_EQUAL(push_count
, pop_count
);
126 BOOST_REQUIRE_EQUAL(push_count
, writer_threads
* node_count
);
130 BOOST_AUTO_TEST_CASE( stack_test_caching
)
132 stack_tester
<boost::lockfree::caching_freelist_t
> tester
;
136 BOOST_AUTO_TEST_CASE( stack_test_static
)
138 stack_tester
<boost::lockfree::static_freelist_t
> tester
;