diff --git a/llvm/lib/Support/Parallel.cpp b/llvm/lib/Support/Parallel.cpp index 8e0c724accb36..2420231307196 100644 --- a/llvm/lib/Support/Parallel.cpp +++ b/llvm/lib/Support/Parallel.cpp @@ -57,6 +57,7 @@ class ThreadPoolExecutor : public Executor { if (S.UseJobserver) TheJobserver = JobserverClient::getInstance(); + ThreadsCreatedFuture = ThreadsCreated.get_future(); ThreadCount = S.compute_thread_count(); // Spawn all but one of the threads in another thread as spawning threads // can take a while. @@ -84,24 +85,28 @@ class ThreadPoolExecutor : public Executor { void stop() { { std::lock_guard Lock(Mutex); - if (Stop) - return; Stop = true; } + Cond.notify_all(); - ThreadsCreated.get_future().wait(); - } + ThreadsCreatedFuture.wait(); + + std::vector ThreadsToJoin; + { + std::lock_guard Lock(Mutex); + ThreadsToJoin.swap(Threads); + } - ~ThreadPoolExecutor() override { - stop(); std::thread::id CurrentThreadId = std::this_thread::get_id(); - for (std::thread &T : Threads) + for (std::thread &T : ThreadsToJoin) if (T.get_id() == CurrentThreadId) T.detach(); else T.join(); } + ~ThreadPoolExecutor() override { stop(); } + struct Creator { static void *call() { return new ThreadPoolExecutor(strategy); } }; @@ -187,6 +192,7 @@ class ThreadPoolExecutor : public Executor { std::mutex Mutex; std::condition_variable Cond; std::promise ThreadsCreated; + std::future ThreadsCreatedFuture; std::vector Threads; unsigned ThreadCount;