[llvm] [Support] Join threads when stopping the ThreadPoolExecutor (PR #166054)
Maurice Heumann via llvm-commits
llvm-commits at lists.llvm.org
Tue Dec 2 22:39:44 PST 2025
https://github.com/momo5502 updated https://github.com/llvm/llvm-project/pull/166054
>From 9b807f34f8a7c1b3c569128e60c48de006346669 Mon Sep 17 00:00:00 2001
From: momo5502 <mauriceheumann at gmail.com>
Date: Sun, 2 Nov 2025 11:56:45 +0100
Subject: [PATCH 1/2] [Support] Join threads when stopping ThreadPoolExecutor
---
llvm/lib/Support/Parallel.cpp | 20 +++++++++++++-------
1 file changed, 13 insertions(+), 7 deletions(-)
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<std::mutex> Lock(Mutex);
- if (Stop)
- return;
Stop = true;
}
+
Cond.notify_all();
- ThreadsCreated.get_future().wait();
- }
+ ThreadsCreatedFuture.wait();
+
+ std::vector<std::thread> ThreadsToJoin;
+ {
+ std::lock_guard<std::mutex> 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<void> ThreadsCreated;
+ std::future<void> ThreadsCreatedFuture;
std::vector<std::thread> Threads;
unsigned ThreadCount;
>From 8cc0efc20d7d2e40f41fa917226478d2ca1da34b Mon Sep 17 00:00:00 2001
From: momo5502 <mauriceheumann at gmail.com>
Date: Wed, 3 Dec 2025 07:39:28 +0100
Subject: [PATCH 2/2] Use reduced implementation by @nga888
---
llvm/lib/Support/Parallel.cpp | 16 +++++-----------
1 file changed, 5 insertions(+), 11 deletions(-)
diff --git a/llvm/lib/Support/Parallel.cpp b/llvm/lib/Support/Parallel.cpp
index 2420231307196..29b28c199b45a 100644
--- a/llvm/lib/Support/Parallel.cpp
+++ b/llvm/lib/Support/Parallel.cpp
@@ -57,7 +57,6 @@ 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.
@@ -85,24 +84,20 @@ class ThreadPoolExecutor : public Executor {
void stop() {
{
std::lock_guard<std::mutex> Lock(Mutex);
+ if (Stop)
+ return;
Stop = true;
}
-
Cond.notify_all();
- ThreadsCreatedFuture.wait();
-
- std::vector<std::thread> ThreadsToJoin;
- {
- std::lock_guard<std::mutex> Lock(Mutex);
- ThreadsToJoin.swap(Threads);
- }
+ ThreadsCreated.get_future().wait();
std::thread::id CurrentThreadId = std::this_thread::get_id();
- for (std::thread &T : ThreadsToJoin)
+ for (std::thread &T : Threads)
if (T.get_id() == CurrentThreadId)
T.detach();
else
T.join();
+ Threads.clear();
}
~ThreadPoolExecutor() override { stop(); }
@@ -192,7 +187,6 @@ class ThreadPoolExecutor : public Executor {
std::mutex Mutex;
std::condition_variable Cond;
std::promise<void> ThreadsCreated;
- std::future<void> ThreadsCreatedFuture;
std::vector<std::thread> Threads;
unsigned ThreadCount;
More information about the llvm-commits
mailing list