@@ -1541,7 +1541,9 @@ class TaskQueue {
15411541
15421542class ThreadPool final : public TaskQueue {
15431543public:
1544- explicit ThreadPool(size_t n, size_t max_n = 0, size_t mqr = 0);
1544+ explicit ThreadPool(
1545+ size_t n, size_t max_n = 0, size_t mqr = 0,
1546+ time_t idle_timeout_sec = CPPHTTPLIB_THREAD_POOL_IDLE_TIMEOUT);
15451547 ThreadPool(const ThreadPool &) = delete;
15461548 ~ThreadPool() override = default;
15471549
@@ -1556,6 +1558,7 @@ class ThreadPool final : public TaskQueue {
15561558 size_t base_thread_count_;
15571559 size_t max_thread_count_;
15581560 size_t max_queued_requests_;
1561+ time_t idle_timeout_sec_;
15591562 size_t idle_thread_count_;
15601563
15611564 bool shutdown_;
@@ -10304,8 +10307,10 @@ inline ssize_t detail::BodyReader::read(char *buf, size_t len) {
1030410307}
1030510308
1030610309// ThreadPool implementation
10307- inline ThreadPool::ThreadPool(size_t n, size_t max_n, size_t mqr)
10308- : base_thread_count_(n), max_queued_requests_(mqr), idle_thread_count_(0),
10310+ inline ThreadPool::ThreadPool(size_t n, size_t max_n, size_t mqr,
10311+ time_t idle_timeout_sec)
10312+ : base_thread_count_(n), max_queued_requests_(mqr),
10313+ idle_timeout_sec_(idle_timeout_sec), idle_thread_count_(0),
1030910314 shutdown_(false) {
1031010315#ifndef CPPHTTPLIB_NO_EXCEPTIONS
1031110316 if (max_n != 0 && max_n < n) {
@@ -10415,9 +10420,9 @@ inline void ThreadPool::worker(bool is_dynamic) {
1041510420 idle_thread_count_++;
1041610421
1041710422 if (is_dynamic) {
10418- auto has_work = cond_.wait_for(
10419- lock, std::chrono::seconds(CPPHTTPLIB_THREAD_POOL_IDLE_TIMEOUT ),
10420- [&] { return !jobs_.empty() || shutdown_; });
10423+ auto has_work =
10424+ cond_.wait_for( lock, std::chrono::seconds(idle_timeout_sec_ ),
10425+ [&] { return !jobs_.empty() || shutdown_; });
1042110426 if (!has_work) {
1042210427 // Timed out with no work - exit this dynamic thread
1042310428 idle_thread_count_--;
0 commit comments