Skip to content

Commit 4062f00

Browse files
committed
optimize thread pool code
test=develop
1 parent fe4cd50 commit 4062f00

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

paddle/fluid/framework/threadpool.cc

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,23 +70,25 @@ ThreadPool::~ThreadPool() {
7070

7171
void ThreadPool::TaskLoop() {
7272
while (true) {
73-
std::unique_lock<std::mutex> lock(mutex_);
73+
Task task;
7474

75-
scheduled_.wait(
76-
lock, [this] { return !this->tasks_.empty() || !this->running_; });
75+
{
76+
std::unique_lock<std::mutex> lock(mutex_);
77+
scheduled_.wait(
78+
lock, [this] { return !this->tasks_.empty() || !this->running_; });
7779

78-
if (!running_ && tasks_.empty()) {
79-
return;
80-
}
80+
if (!running_ && tasks_.empty()) {
81+
return;
82+
}
8183

82-
if (tasks_.empty()) {
83-
PADDLE_THROW("This thread has no task to Run");
84-
}
84+
if (tasks_.empty()) {
85+
PADDLE_THROW("This thread has no task to Run");
86+
}
8587

86-
// pop a task from the task queue
87-
auto task = std::move(tasks_.front());
88-
tasks_.pop();
89-
lock.unlock();
88+
// pop a task from the task queue
89+
task = std::move(tasks_.front());
90+
tasks_.pop();
91+
}
9092

9193
// run the task
9294
task();

0 commit comments

Comments
 (0)