From 10e9cd3edc9e1e4cb78ddffcf8966d1bf241deee Mon Sep 17 00:00:00 2001 From: Christopher Dykes Date: Tue, 9 Aug 2016 17:33:05 -0700 Subject: [PATCH] Wait for all threads to finish executing before leaving in an EventBase test Summary: If one of the assertions failed, it would result in any threads that are still running accessing already-disposed data, so wait for the threads to exit, even when an exception is thrown. This also slightly cleans up the handling of threads a bit further down, because the mess it was doing was completely unnecessary. Reviewed By: yfeldblum Differential Revision: D3692438 fbshipit-source-id: 9082ba248b2cf1062e46c97faf0bc062312ee9ae --- folly/io/async/test/EventBaseTest.cpp | 36 ++++++++++++++++----------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/folly/io/async/test/EventBaseTest.cpp b/folly/io/async/test/EventBaseTest.cpp index 29cf89fd..11f61187 100644 --- a/folly/io/async/test/EventBaseTest.cpp +++ b/folly/io/async/test/EventBaseTest.cpp @@ -1121,6 +1121,13 @@ TEST(EventBaseTest, RunInThread) { RunInThreadData data(numThreads, opsPerThread); deque threads; + SCOPE_EXIT { + // Wait on all of the threads. + for (auto& thread : threads) { + thread.join(); + } + }; + for (uint32_t i = 0; i < numThreads; ++i) { threads.emplace_back([i, &data] { for (int n = 0; n < data.opsPerThread; ++n) { @@ -1186,24 +1193,23 @@ TEST(EventBaseTest, RunInEventBaseThreadAndWait) { auto& atom = atoms.at(i); atom = make_unique>(0); } - vector threads(c); + vector threads; for (size_t i = 0; i < c; ++i) { - auto& atom = *atoms.at(i); - auto& th = threads.at(i); - th = thread([&atom] { - EventBase eb; - auto ebth = thread([&]{ eb.loopForever(); }); - eb.waitUntilRunning(); - eb.runInEventBaseThreadAndWait([&] { - size_t x = 0; - atom.compare_exchange_weak( - x, 1, std::memory_order_release, std::memory_order_relaxed); - }); + threads.emplace_back([&atoms, i] { + EventBase eb; + auto& atom = *atoms.at(i); + auto ebth = thread([&] { eb.loopForever(); }); + eb.waitUntilRunning(); + eb.runInEventBaseThreadAndWait([&] { size_t x = 0; atom.compare_exchange_weak( - x, 2, std::memory_order_release, std::memory_order_relaxed); - eb.terminateLoopSoon(); - ebth.join(); + x, 1, std::memory_order_release, std::memory_order_relaxed); + }); + size_t x = 0; + atom.compare_exchange_weak( + x, 2, std::memory_order_release, std::memory_order_relaxed); + eb.terminateLoopSoon(); + ebth.join(); }); } for (size_t i = 0; i < c; ++i) { -- 2.34.1