Skip to content

Commit ef0c5fe

Browse files
authored
bugfix: correct threadpool initialization logic. (jd-opensource#353)
1 parent 9cc499b commit ef0c5fe

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

xllm/core/util/threadpool.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@ ThreadPool::ThreadPool(size_t num_threads) : ThreadPool(num_threads, nullptr) {}
2222

2323
ThreadPool::ThreadPool(size_t num_threads, Runnable init_func)
2424
: queues_(num_threads) {
25+
BlockingCounter counter(num_threads);
2526
for (size_t i = 0; i < num_threads; ++i) {
26-
threads_.emplace_back(
27-
[this, i, init_func = std::move(init_func)]() mutable {
28-
internal_loop(i, std::move(init_func));
29-
});
27+
threads_.emplace_back([this,
28+
i,
29+
init_func_ptr = &init_func,
30+
counter_ptr = &counter]() mutable {
31+
internal_loop(i, init_func_ptr, counter_ptr);
32+
});
3033
}
34+
counter.wait();
3135
}
3236

3337
ThreadPool::~ThreadPool() {
@@ -66,10 +70,13 @@ void ThreadPool::schedule_with_tid(Runnable runnable, size_t tid) {
6670
queues_[tid].enqueue(std::move(runnable));
6771
}
6872

69-
void ThreadPool::internal_loop(size_t index, Runnable&& init_func) {
70-
if (init_func != nullptr) {
71-
init_func();
73+
void ThreadPool::internal_loop(size_t index,
74+
Runnable* init_func,
75+
BlockingCounter* block_counter) {
76+
if (init_func != nullptr && *init_func != nullptr) {
77+
(*init_func)();
7278
}
79+
block_counter->decrement_count();
7380

7481
while (true) {
7582
Runnable runnable;

xllm/core/util/threadpool.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ limitations under the License.
1818

1919
#include <thread>
2020

21+
#include "util/blocking_counter.h"
2122
#include "util/blockingconcurrentqueue.h"
2223

2324
namespace xllm {
@@ -56,7 +57,9 @@ class ThreadPool final {
5657
size_t size() { return threads_.size(); }
5758

5859
private:
59-
void internal_loop(size_t tid, Runnable&& init_func);
60+
void internal_loop(size_t tid,
61+
Runnable* init_func,
62+
BlockingCounter* block_counter);
6063

6164
std::vector<std::thread> threads_;
6265
std::vector<moodycamel::BlockingConcurrentQueue<Runnable>> queues_;

0 commit comments

Comments
 (0)