From 4062f00f2ae20bfb07b850ce5e1e21fccf07b97d Mon Sep 17 00:00:00 2001 From: Qiao Longfei Date: Tue, 6 Nov 2018 16:21:38 +0800 Subject: [PATCH] optimize thread pool code test=develop --- paddle/fluid/framework/threadpool.cc | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/paddle/fluid/framework/threadpool.cc b/paddle/fluid/framework/threadpool.cc index 21fab2cf5..fcec95536 100644 --- a/paddle/fluid/framework/threadpool.cc +++ b/paddle/fluid/framework/threadpool.cc @@ -70,23 +70,25 @@ ThreadPool::~ThreadPool() { void ThreadPool::TaskLoop() { while (true) { - std::unique_lock lock(mutex_); + Task task; - scheduled_.wait( - lock, [this] { return !this->tasks_.empty() || !this->running_; }); + { + std::unique_lock lock(mutex_); + scheduled_.wait( + lock, [this] { return !this->tasks_.empty() || !this->running_; }); - if (!running_ && tasks_.empty()) { - return; - } + if (!running_ && tasks_.empty()) { + return; + } - if (tasks_.empty()) { - PADDLE_THROW("This thread has no task to Run"); - } + if (tasks_.empty()) { + PADDLE_THROW("This thread has no task to Run"); + } - // pop a task from the task queue - auto task = std::move(tasks_.front()); - tasks_.pop(); - lock.unlock(); + // pop a task from the task queue + task = std::move(tasks_.front()); + tasks_.pop(); + } // run the task task(); -- GitLab