diff --git a/llvm/lib/Support/Parallel.cpp b/lib/Support/Parallel.cpp --- a/llvm/lib/Support/Parallel.cpp +++ b/lib/Support/Parallel.cpp @@ -11,11 +11,10 @@ #if LLVM_ENABLE_THREADS -#include "llvm/Support/Threading.h" - #include #include #include +#include namespace llvm { namespace parallel { @@ -36,16 +35,12 @@ /// in filo order. class ThreadPoolExecutor : public Executor { public: - explicit ThreadPoolExecutor(unsigned ThreadCount = hardware_concurrency()) - : Done(ThreadCount) { - // Spawn all but one of the threads in another thread as spawning threads - // can take a while. - std::thread([&, ThreadCount] { - for (size_t i = 1; i < ThreadCount; ++i) { - std::thread([=] { work(); }).detach(); - } - work(); - }).detach(); + explicit ThreadPoolExecutor( + unsigned ThreadCount = std::thread::hardware_concurrency()) { + Pool.reserve(ThreadCount); + for (unsigned i = 0; i < ThreadCount; ++i) { + Pool.push_back(std::thread([=] { work(); })); + } } ~ThreadPoolExecutor() override { @@ -53,7 +48,9 @@ Stop = true; Lock.unlock(); Cond.notify_all(); - // Wait for ~Latch. + for (auto &Thread : Pool) { + Thread.join(); + } } void add(std::function F) override { @@ -75,14 +72,13 @@ Lock.unlock(); Task(); } - Done.dec(); } std::atomic Stop{false}; std::stack> WorkStack; std::mutex Mutex; std::condition_variable Cond; - parallel::detail::Latch Done; + std::vector Pool; }; Executor *Executor::getDefaultExecutor() {