mirror of
https://github.com/boostorg/unordered.git
synced 2025-07-31 03:47:16 +02:00
Update tests to use condition variables for work synchronization instead of sleeps
This commit is contained in:
@ -297,11 +297,20 @@ namespace {
|
|||||||
boost::unordered_flat_map<raii, raii>(values.begin(), values.end());
|
boost::unordered_flat_map<raii, raii>(values.begin(), values.end());
|
||||||
raii::reset_counts();
|
raii::reset_counts();
|
||||||
|
|
||||||
|
std::mutex m;
|
||||||
|
std::condition_variable cv;
|
||||||
|
bool ready = false;
|
||||||
|
|
||||||
{
|
{
|
||||||
map_type x(0, hasher(1), key_equal(2), allocator_type{});
|
map_type x(0, hasher(1), key_equal(2), allocator_type{});
|
||||||
|
|
||||||
auto f = [&x, &values] {
|
auto f = [&x, &values, &m, &cv, &ready] {
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(75));
|
{
|
||||||
|
std::lock_guard<std::mutex> guard(m);
|
||||||
|
ready = true;
|
||||||
|
}
|
||||||
|
cv.notify_all();
|
||||||
|
|
||||||
for (auto const& val : values) {
|
for (auto const& val : values) {
|
||||||
x.insert(val);
|
x.insert(val);
|
||||||
}
|
}
|
||||||
@ -311,9 +320,15 @@ namespace {
|
|||||||
std::thread t2(f);
|
std::thread t2(f);
|
||||||
|
|
||||||
thread_runner(
|
thread_runner(
|
||||||
values, [&x, &reference_map, &values, rg](
|
values, [&x, &reference_map, &values, rg, &m, &cv, &ready](
|
||||||
boost::span<span_value_type<decltype(values)> > s) {
|
boost::span<span_value_type<decltype(values)> > s) {
|
||||||
(void)s;
|
(void)s;
|
||||||
|
|
||||||
|
{
|
||||||
|
std::unique_lock<std::mutex> lk(m);
|
||||||
|
cv.wait(lk, [&] { return ready; });
|
||||||
|
}
|
||||||
|
|
||||||
map_type y(x);
|
map_type y(x);
|
||||||
|
|
||||||
BOOST_TEST_LE(y.size(), values.size());
|
BOOST_TEST_LE(y.size(), values.size());
|
||||||
@ -483,12 +498,12 @@ namespace {
|
|||||||
BOOST_TEST_EQ(y.hash_function(), hasher(1));
|
BOOST_TEST_EQ(y.hash_function(), hasher(1));
|
||||||
BOOST_TEST_EQ(y.key_eq(), key_equal(2));
|
BOOST_TEST_EQ(y.key_eq(), key_equal(2));
|
||||||
} else {
|
} else {
|
||||||
BOOST_TEST_EQ(y.size(), 0);
|
BOOST_TEST_EQ(y.size(), 0u);
|
||||||
BOOST_TEST_EQ(y.hash_function(), hasher());
|
BOOST_TEST_EQ(y.hash_function(), hasher());
|
||||||
BOOST_TEST_EQ(y.key_eq(), key_equal());
|
BOOST_TEST_EQ(y.key_eq(), key_equal());
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_TEST_EQ(x.size(), 0);
|
BOOST_TEST_EQ(x.size(), 0u);
|
||||||
BOOST_TEST_EQ(x.hash_function(), hasher());
|
BOOST_TEST_EQ(x.hash_function(), hasher());
|
||||||
BOOST_TEST_EQ(x.key_eq(), key_equal());
|
BOOST_TEST_EQ(x.key_eq(), key_equal());
|
||||||
|
|
||||||
@ -511,11 +526,20 @@ namespace {
|
|||||||
boost::unordered_flat_map<raii, raii>(values.begin(), values.end());
|
boost::unordered_flat_map<raii, raii>(values.begin(), values.end());
|
||||||
raii::reset_counts();
|
raii::reset_counts();
|
||||||
|
|
||||||
|
std::mutex m;
|
||||||
|
std::condition_variable cv;
|
||||||
|
bool ready = false;
|
||||||
|
|
||||||
{
|
{
|
||||||
map_type x(0, hasher(1), key_equal(2), allocator_type{});
|
map_type x(0, hasher(1), key_equal(2), allocator_type{});
|
||||||
|
|
||||||
auto f = [&x, &values] {
|
auto f = [&x, &values, &m, &cv, &ready] {
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(25));
|
{
|
||||||
|
std::lock_guard<std::mutex> guard(m);
|
||||||
|
ready = true;
|
||||||
|
}
|
||||||
|
cv.notify_all();
|
||||||
|
|
||||||
for (auto const& val : values) {
|
for (auto const& val : values) {
|
||||||
x.insert(val);
|
x.insert(val);
|
||||||
}
|
}
|
||||||
@ -527,10 +551,15 @@ namespace {
|
|||||||
std::thread t2(f);
|
std::thread t2(f);
|
||||||
|
|
||||||
thread_runner(
|
thread_runner(
|
||||||
values, [&x, &reference_map, &num_transfers, rg](
|
values, [&x, &reference_map, &num_transfers, rg, &m, &ready, &cv](
|
||||||
boost::span<span_value_type<decltype(values)> > s) {
|
boost::span<span_value_type<decltype(values)> > s) {
|
||||||
(void)s;
|
(void)s;
|
||||||
|
|
||||||
|
{
|
||||||
|
std::unique_lock<std::mutex> lk(m);
|
||||||
|
cv.wait(lk, [&] { return ready; });
|
||||||
|
}
|
||||||
|
|
||||||
map_type y(std::move(x));
|
map_type y(std::move(x));
|
||||||
|
|
||||||
if (!y.empty()) {
|
if (!y.empty()) {
|
||||||
|
@ -9,8 +9,10 @@
|
|||||||
#include <boost/unordered/unordered_flat_map.hpp>
|
#include <boost/unordered/unordered_flat_map.hpp>
|
||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
|
#include <condition_variable>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <mutex>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@ -272,16 +274,31 @@ std::vector<boost::span<T> > split(
|
|||||||
|
|
||||||
template <class T, class F> void thread_runner(std::vector<T>& values, F f)
|
template <class T, class F> void thread_runner(std::vector<T>& values, F f)
|
||||||
{
|
{
|
||||||
|
std::mutex m;
|
||||||
|
std::condition_variable cv;
|
||||||
|
bool ready = false;
|
||||||
|
|
||||||
std::vector<std::thread> threads;
|
std::vector<std::thread> threads;
|
||||||
auto subslices = split<T>(values, num_threads);
|
auto subslices = split<T>(values, num_threads);
|
||||||
|
|
||||||
for (std::size_t i = 0; i < num_threads; ++i) {
|
for (std::size_t i = 0; i < num_threads; ++i) {
|
||||||
threads.emplace_back([&f, &subslices, i] {
|
threads.emplace_back([&f, &subslices, i, &m, &cv, &ready] {
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
{
|
||||||
|
std::unique_lock<std::mutex> lk(m);
|
||||||
|
cv.wait(lk, [&] { return ready; });
|
||||||
|
}
|
||||||
|
|
||||||
auto s = subslices[i];
|
auto s = subslices[i];
|
||||||
f(s);
|
f(s);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> guard(m);
|
||||||
|
ready = true;
|
||||||
|
}
|
||||||
|
cv.notify_all();
|
||||||
|
|
||||||
for (auto& t : threads) {
|
for (auto& t : threads) {
|
||||||
t.join();
|
t.join();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user