When the thread pool has no work to do, and we have enough free workers,
other worker threads will terminate.
Without this change, when using large number of worker threads, the
thread pool grows to max_worker_threads workers, and never shrink down.
With this change, the pool quickly shrinks down, but we always have
enough free worker threads for serving new requests.
Signed-off-by: Nir Soffer <nsoffer(a)redhat.com>
---
src/main.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/src/main.c b/src/main.c
index 63d8cff..622dc8e 100644
--- a/src/main.c
+++ b/src/main.c
@@ -932,6 +932,9 @@ static void *thread_pool_worker(void *data)
while (1) {
while (!pool.quit && list_empty(&pool.work_data)) {
+ if (pool.free_workers >= DEFAULT_MIN_WORKER_THREADS)
+ goto out;
+
pool.free_workers++;
pthread_cond_wait(&pool.cond, &pool.mutex);
pool.free_workers--;
@@ -952,6 +955,7 @@ static void *thread_pool_worker(void *data)
break;
}
+out:
pool.num_workers--;
if (!pool.num_workers)
pthread_cond_signal(&pool.quit_wait);
--
2.26.2