@@ -57,6 +57,7 @@ class ThreadPoolExecutor : public Executor {
5757 if (S.UseJobserver )
5858 TheJobserver = JobserverClient::getInstance ();
5959
60+ ThreadsCreatedFuture = ThreadsCreated.get_future ();
6061 ThreadCount = S.compute_thread_count ();
6162 // Spawn all but one of the threads in another thread as spawning threads
6263 // can take a while.
@@ -84,24 +85,28 @@ class ThreadPoolExecutor : public Executor {
8485 void stop () {
8586 {
8687 std::lock_guard<std::mutex> Lock (Mutex);
87- if (Stop)
88- return ;
8988 Stop = true ;
9089 }
90+
91+ ThreadsCreatedFuture.wait ();
9192 Cond.notify_all ();
92- ThreadsCreated.get_future ().wait ();
93- }
9493
95- ~ThreadPoolExecutor () override {
96- stop ();
94+ std::vector<std::thread> ThreadsToJoin;
95+ {
96+ std::lock_guard<std::mutex> Lock (Mutex);
97+ ThreadsToJoin.swap (Threads);
98+ }
99+
97100 std::thread::id CurrentThreadId = std::this_thread::get_id ();
98- for (std::thread &T : Threads )
101+ for (std::thread &T : ThreadsToJoin )
99102 if (T.get_id () == CurrentThreadId)
100103 T.detach ();
101104 else
102105 T.join ();
103106 }
104107
108+ ~ThreadPoolExecutor () override { stop (); }
109+
105110 struct Creator {
106111 static void *call () { return new ThreadPoolExecutor (strategy); }
107112 };
@@ -187,6 +192,7 @@ class ThreadPoolExecutor : public Executor {
187192 std::mutex Mutex;
188193 std::condition_variable Cond;
189194 std::promise<void > ThreadsCreated;
195+ std::future<void > ThreadsCreatedFuture;
190196 std::vector<std::thread> Threads;
191197 unsigned ThreadCount;
192198
0 commit comments