Skip to content

Commit d688fb9

Browse files
authored
make workpool's dtor consume less cpu if there's photon env (alibaba#969)
1 parent ff3ef5f commit d688fb9

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

thread/workerpool.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class WorkPool::impl {
4545
int mode;
4646

4747
impl(size_t vcpu_num, int ev_engine, int io_engine, int mode) : mode(mode) {
48+
vcpus.reserve(vcpu_num);
4849
for (size_t i = 0; i < vcpu_num; ++i) {
4950
owned_std_threads.emplace_back(
5051
&WorkPool::impl::worker_thread_routine, this, ev_engine,
@@ -53,10 +54,14 @@ class WorkPool::impl {
5354
ready_vcpu.wait(vcpu_num);
5455
}
5556

56-
~impl() { // avoid depending on photon to make it destructible wihout photon
57+
~impl() {
5758
for (auto num = vcpus.size(); num; --num) enqueue({});
5859
for (auto &worker : owned_std_threads) worker.join();
59-
while (vcpus.size()) std::this_thread::yield();
60+
if (likely(CURRENT)) {
61+
while (vcpus.size()) thread_yield();
62+
} else {
63+
while (vcpus.size()) std::this_thread::yield();
64+
}
6065
}
6166

6267
void enqueue(Delegate<void> call, AutoContext = {}) {

0 commit comments

Comments
 (0)