@@ -33,25 +33,24 @@ namespace milvus {
3333
3434class ThreadPool {
3535 public:
36- explicit ThreadPool (const int thread_core_coefficient, std::string name)
36+ explicit ThreadPool (const float thread_core_coefficient, std::string name)
3737 : shutdown_(false ), name_(std::move(name)) {
3838 idle_threads_size_ = 0 ;
3939 current_threads_size_ = 0 ;
40- min_threads_size_ = CPU_NUM;
41- max_threads_size_ = CPU_NUM * thread_core_coefficient;
40+ min_threads_size_ = 1 ;
41+ max_threads_size_.store (std::max (
42+ 1 ,
43+ static_cast <int >(std::round (CPU_NUM * thread_core_coefficient))));
4244
4345 // only IO pool will set large limit, but the CPU helps nothing to IO operations,
4446 // we need to limit the max thread num, each thread will download 16~64 MiB data,
4547 // according to our benchmark, 16 threads is enough to saturate the network bandwidth.
46- if (min_threads_size_ > 16 ) {
47- min_threads_size_ = 16 ;
48- }
49- if (max_threads_size_ > 16 ) {
50- max_threads_size_ = 16 ;
48+ if (max_threads_size_.load () > 16 ) {
49+ max_threads_size_.store (16 );
5150 }
5251 LOG_INFO (" Init thread pool:{}" , name_)
5352 << " with min worker num:" << min_threads_size_
54- << " and max worker num:" << max_threads_size_;
53+ << " and max worker num:" << max_threads_size_. load () ;
5554 Init ();
5655 }
5756
@@ -80,7 +79,7 @@ class ThreadPool {
8079
8180 size_t
8281 GetMaxThreadNum () {
83- return max_threads_size_;
82+ return max_threads_size_. load () ;
8483 }
8584
8685 template <typename F, typename ... Args>
@@ -100,7 +99,7 @@ class ThreadPool {
10099
101100 if (idle_threads_size_ > 0 ) {
102101 condition_lock_.notify_one ();
103- } else if (current_threads_size_ < max_threads_size_) {
102+ } else if (current_threads_size_ < max_threads_size_. load () ) {
104103 // Dynamic increase thread number
105104 std::thread t (&ThreadPool::Worker, this );
106105 assert (threads_.find (t.get_id ()) == threads_.end ());
@@ -117,11 +116,18 @@ class ThreadPool {
117116 void
118117 FinishThreads ();
119118
119+ void
120+ Resize (int new_size) {
121+ // no need to hold mutex here as we don't require
122+ // max_threads_size to take effect instantly, just guaranteed atomic
123+ max_threads_size_.store (new_size);
124+ }
125+
120126 public:
121127 int min_threads_size_;
122128 int idle_threads_size_;
123129 int current_threads_size_;
124- int max_threads_size_;
130+ std::atomic< int > max_threads_size_;
125131 bool shutdown_;
126132 static constexpr size_t WAIT_SECONDS = 2 ;
127133 SafeQueue<std::function<void ()>> work_queue_;
0 commit comments