From 5dfed4deec3470ee6a6cd7f30d6001fee1d7e4e8 Mon Sep 17 00:00:00 2001 From: Christian Mazakas Date: Thu, 20 Apr 2023 14:34:01 -0700 Subject: [PATCH] Update thread_runner to block on hand-rolled barrier, add yield points to hash, key_equal --- test/cfoa/helpers.hpp | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/test/cfoa/helpers.hpp b/test/cfoa/helpers.hpp index a830c032..e630cd9c 100644 --- a/test/cfoa/helpers.hpp +++ b/test/cfoa/helpers.hpp @@ -24,6 +24,7 @@ struct transp_hash template std::size_t operator()(T const& t) const noexcept { + std::this_thread::yield(); return boost::hash()(t); } }; @@ -34,6 +35,7 @@ struct transp_key_equal template bool operator()(T const& lhs, U const& rhs) const { + std::this_thread::yield(); return lhs == rhs; } }; @@ -57,6 +59,7 @@ struct stateful_hash { std::size_t h = static_cast(x_); boost::hash_combine(h, t); + std::this_thread::yield(); return h; } @@ -86,6 +89,7 @@ struct stateful_key_equal template bool operator()(T const& t, U const& u) const { + std::this_thread::yield(); return t == u; } @@ -276,16 +280,22 @@ template void thread_runner(std::vector& values, F f) { std::mutex m; std::condition_variable cv; - bool ready = false; + std::size_t c = 0; std::vector threads; auto subslices = split(values, num_threads); for (std::size_t i = 0; i < num_threads; ++i) { - threads.emplace_back([&f, &subslices, i, &m, &cv, &ready] { + threads.emplace_back([&f, &subslices, i, &m, &cv, &c] { { std::unique_lock lk(m); - cv.wait(lk, [&] { return ready; }); + ++c; + if (c == num_threads) { + lk.unlock(); + cv.notify_all(); + } else { + cv.wait(lk, [&] { return c == num_threads; }); + } } auto s = subslices[i]; @@ -293,12 +303,6 @@ template void thread_runner(std::vector& values, F f) }); } - { - std::lock_guard guard(m); - ready = true; - } - cv.notify_all(); - for (auto& t : threads) { t.join(); }