Skip to content

Commit 81cbc42

Browse files
committed
Fix race condition in new Dynamic ThreadPool
1 parent 06f6d82 commit 81cbc42

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

httplib.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9356,7 +9356,15 @@ inline void ThreadPool::shutdown() {
93569356
for (auto &t : threads_) {
93579357
if (t.joinable()) { t.join(); }
93589358
}
9359-
for (auto &t : dynamic_threads_) {
9359+
9360+
// Move dynamic_threads_ to a local list under the lock to avoid racing
9361+
// with worker threads that call move_to_finished() concurrently.
9362+
std::list<std::thread> remaining_dynamic;
9363+
{
9364+
std::unique_lock<std::mutex> lock(mutex_);
9365+
remaining_dynamic = std::move(dynamic_threads_);
9366+
}
9367+
for (auto &t : remaining_dynamic) {
93609368
if (t.joinable()) { t.join(); }
93619369
}
93629370

0 commit comments

Comments
 (0)