1 // Copyright (C) 2011 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 "test_helpers.hpp"
10 #include <boost/array.hpp>
11 #include <boost/thread.hpp>
16 using namespace boost;
19 template <bool Bounded = false>
20 struct queue_stress_tester
22 static const unsigned int buckets = 1<<13;
23 #ifndef BOOST_LOCKFREE_STRESS_TEST
24 static const long node_count = 5000;
26 static const long node_count = 500000;
28 const int reader_threads;
29 const int writer_threads;
31 boost::lockfree::detail::atomic<int> writers_finished;
33 static_hashed_set<long, buckets> data;
34 static_hashed_set<long, buckets> dequeued;
35 array<std::set<long>, buckets> returned;
37 boost::lockfree::detail::atomic<int> push_count, pop_count;
39 queue_stress_tester(int reader, int writer):
40 reader_threads(reader), writer_threads(writer), push_count(0), pop_count(0)
43 template <typename queue>
44 void add_items(queue & stk)
46 for (long i = 0; i != node_count; ++i) {
47 long id = generate_id<long>();
49 bool inserted = data.insert(id);
53 while(stk.bounded_push(id) == false)
56 while(stk.push(id) == false)
60 writers_finished += 1;
63 boost::lockfree::detail::atomic<bool> running;
65 template <typename queue>
66 bool consume_element(queue & q)
74 bool erased = data.erase(id);
75 bool inserted = dequeued.insert(id);
82 template <typename queue>
83 void get_items(queue & q)
86 bool received_element = consume_element(q);
90 if ( writers_finished.load() == writer_threads )
94 while (consume_element(q));
97 template <typename queue>
100 BOOST_WARN(stk.is_lock_free());
101 writers_finished.store(0);
106 BOOST_REQUIRE(stk.empty());
108 for (int i = 0; i != reader_threads; ++i)
109 reader.create_thread(boost::bind(&queue_stress_tester::template get_items<queue>, this, boost::ref(stk)));
111 for (int i = 0; i != writer_threads; ++i)
112 writer.create_thread(boost::bind(&queue_stress_tester::template add_items<queue>, this, boost::ref(stk)));
115 cout << "threads created" << endl;
119 cout << "writer threads joined, waiting for readers" << endl;
123 cout << "reader threads joined" << endl;
125 BOOST_REQUIRE_EQUAL(data.count_nodes(), (size_t)0);
126 BOOST_REQUIRE(stk.empty());
128 BOOST_REQUIRE_EQUAL(push_count, pop_count);
129 BOOST_REQUIRE_EQUAL(push_count, writer_threads * node_count);
135 using impl::queue_stress_tester;