Clamp initial thread count to ScalingConfig bounds in constructor

When auto-scaling is enabled, the constructor did not enforce that the
initial thread_count falls within [min_threads, max_threads]. Starting
with fewer threads than min_threads meant the monitor had to
immediately grow the pool, and starting above max_threads created
threads that the monitor would then immediately try to shrink.

Clamp the count before spawning so the pool starts within its
configured operating range.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Antonio S. Musumeci 2026-04-09 22:00:25 -04:00
parent d166418675
commit 111bedf2c9

View file

@ -205,14 +205,24 @@ ThreadPool::ThreadPool(const unsigned thread_count_,
if(scaling_config_)
_scaling_config = *scaling_config_;
// Clamp initial thread count to ScalingConfig bounds when auto-scaling.
std::size_t count = thread_count_;
if(_autoscale_enabled)
{
if(count < _scaling_config.min_threads)
count = _scaling_config.min_threads;
if(count > _scaling_config.max_threads)
count = _scaling_config.max_threads;
}
sigset_t oldset;
sigset_t newset;
sigfillset(&newset);
pthread_sigmask(SIG_BLOCK,&newset,&oldset);
_threads.reserve(thread_count_);
for(std::size_t i = 0; i < thread_count_; ++i)
_threads.reserve(count);
for(std::size_t i = 0; i < count; ++i)
{
int rv;
pthread_t t;