@@ -441,7 +441,7 @@ class thread_pool_generic : public thread_pool
441441 disarm ();
442442 }
443443 };
444- timer_generic m_maintenance_timer;
444+ timer_generic* m_maintenance_timer= nullptr ;
445445 timer* create_timer (callback_func func, void *data) override
446446 {
447447 return new timer_generic (func, data, this );
@@ -751,9 +751,16 @@ bool thread_pool_generic::add_thread()
751751 reset the flag in thread_pool_generic::worker_main in new thread created. The
752752 flag must be reset back in case we fail to create the thread. If this flag is
753753 not reset all future attempt to create thread for this pool would not work as
754- we would return from here. */
755- if (m_thread_creation_pending.test_and_set ())
756- return false ;
754+ we would return from here.
755+
756+ Do not use this flag for pool of fixed size.
757+ (since they lack maintenence that would rectify the pool size, if it is too small)
758+ */
759+ if (m_min_threads != m_max_threads)
760+ {
761+ if (m_thread_creation_pending.test_and_set ())
762+ return false ;
763+ }
757764
758765 worker_data *thread_data = m_thread_data_cache.get ();
759766 m_active_threads.push_back (thread_data);
@@ -820,13 +827,16 @@ thread_pool_generic::thread_pool_generic(int min_threads, int max_threads) :
820827 m_min_threads(min_threads),
821828 m_max_threads(max_threads),
822829 m_last_thread_count(),
823- m_last_activity(),
824- m_maintenance_timer(thread_pool_generic::maintenance_func, this , nullptr )
830+ m_last_activity()
825831{
826832 set_concurrency ();
827833
828834 // start the timer
829- m_maintenance_timer.set_time (0 , (int )m_timer_interval.count ());
835+ if (m_min_threads != m_max_threads)
836+ {
837+ m_maintenance_timer= new timer_generic (thread_pool_generic::maintenance_func, this , nullptr );
838+ m_maintenance_timer->set_time (0 , (int )m_timer_interval.count ());
839+ }
830840}
831841
832842
@@ -933,7 +943,8 @@ void thread_pool_generic::switch_timer(timer_state_t state)
933943 long long period= (state == timer_state_t ::OFF) ?
934944 m_timer_interval.count ()*10 : m_timer_interval.count ();
935945
936- m_maintenance_timer.set_period ((int )period);
946+ if (m_maintenance_timer)
947+ m_maintenance_timer->set_period ((int )period);
937948}
938949
939950
@@ -951,7 +962,8 @@ thread_pool_generic::~thread_pool_generic()
951962 m_aio.reset ();
952963
953964 /* Also stop the maintanence task early. */
954- m_maintenance_timer.disarm ();
965+ if (m_maintenance_timer)
966+ m_maintenance_timer->disarm ();
955967
956968 std::unique_lock<std::mutex> lk (m_mtx);
957969 m_in_shutdown= true ;
@@ -967,6 +979,7 @@ thread_pool_generic::~thread_pool_generic()
967979 }
968980
969981 lk.unlock ();
982+ delete m_maintenance_timer;
970983}
971984
972985thread_pool *create_thread_pool_generic (int min_threads, int max_threads)
0 commit comments