Skip to content

Commit a3a95e6

Browse files
committed
[Support] Join threads when stopping ThreadPoolExecutor
1 parent f46ac92 commit a3a95e6

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

llvm/lib/Support/Parallel.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,26 +82,26 @@ class ThreadPoolExecutor : public Executor {
8282
ThreadPoolExecutor() = delete;
8383

8484
void stop() {
85+
Stop = true;
86+
Cond.notify_all();
87+
ThreadsCreated.get_future().wait();
88+
89+
std::vector<std::thread> ThreadsToJoin;
8590
{
8691
std::lock_guard<std::mutex> Lock(Mutex);
87-
if (Stop)
88-
return;
89-
Stop = true;
92+
ThreadsToJoin.swap(Threads);
9093
}
91-
Cond.notify_all();
92-
ThreadsCreated.get_future().wait();
93-
}
9494

95-
~ThreadPoolExecutor() override {
96-
stop();
9795
std::thread::id CurrentThreadId = std::this_thread::get_id();
98-
for (std::thread &T : Threads)
96+
for (std::thread &T : ThreadsToJoin)
9997
if (T.get_id() == CurrentThreadId)
10098
T.detach();
10199
else
102100
T.join();
103101
}
104102

103+
~ThreadPoolExecutor() override { stop(); }
104+
105105
struct Creator {
106106
static void *call() { return new ThreadPoolExecutor(strategy); }
107107
};

0 commit comments

Comments
 (0)