void run(void) { BOOST_WARN(stk.is_lock_free()); running.store(true); thread_group writer; thread_group reader; BOOST_REQUIRE(stk.empty()); for (int i = 0; i != reader_threads; ++i) reader.create_thread(boost::bind(&stack_tester::get_items, this)); for (int i = 0; i != writer_threads; ++i) writer.create_thread(boost::bind(&stack_tester::add_items, this)); using namespace std; cout << "threads created" << endl; writer.join_all(); cout << "writer threads joined, waiting for readers" << endl; running = false; reader.join_all(); cout << "reader threads joined" << endl; BOOST_REQUIRE_EQUAL(data.count_nodes(), 0); BOOST_REQUIRE(stk.empty()); BOOST_REQUIRE_EQUAL(push_count, pop_count); BOOST_REQUIRE_EQUAL(push_count, writer_threads * node_count); }
void add_items(void) { for (long i = 0; i != node_count; ++i) { long id = generate_id<long>(); bool inserted = data.insert(id); assert(inserted); while(stk.push(id) == false) thread::yield(); ++push_count; } }
void add_items(void) { unsigned long count = 1000000; for (long i = 0; i != count; ++i) { thread::yield(); long id = generate_id<long>(); bool inserted = data.insert(id); assert(inserted); stk.push(id); } }
void get_items(void) { for (;;) { long id; bool got = stk.pop(&id); if (got) { bool erased = data.erase(id); assert(erased); ++pop_count; } else if (not running) return; } }
void get_items(void) { for (;;) { thread::yield(); long id; bool got = stk.pop(&id); if (got) { bool erased = data.erase(id); assert(erased); } else if (not running) return; } }
stack_tester(void): push_count(0), pop_count(0) { stk.reserve(128); }