Let dynamic threads wait for work instead of exiting immediately

Previously, dynamic threads exited as soon as their current task
completed and the queue was empty. This caused excessive thread
creation/destruction under bursty or long-lived workloads (e.g., SSE
streaming), degrading tail latency. Now dynamic threads loop back and
wait for CPPHTTPLIB_THREAD_POOL_IDLE_TIMEOUT (3s) before exiting,
allowing them to be reused for subsequent tasks.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
yhirose
2026-04-12 11:50:38 -04:00
co-authored by Claude Opus 4.6
parent 09d00c099c
commit ee5d15c842
-9
View File
@@ -10136,15 +10136,6 @@ inline void ThreadPool::worker(bool is_dynamic) {
assert(true == static_cast<bool>(fn));
fn();
// Dynamic thread: exit if queue is empty after task completion
if (is_dynamic) {
std::unique_lock<std::mutex> lock(mutex_);
if (jobs_.empty()) {
move_to_finished(std::this_thread::get_id());
break;
}
}
}
#if defined(CPPHTTPLIB_OPENSSL_SUPPORT) && !defined(OPENSSL_IS_BORINGSSL) && \