提交 4062f00f 编写于 作者: Q Qiao Longfei

optimize thread pool code

test=develop
上级 fe4cd502
...@@ -70,23 +70,25 @@ ThreadPool::~ThreadPool() { ...@@ -70,23 +70,25 @@ ThreadPool::~ThreadPool() {
void ThreadPool::TaskLoop() { void ThreadPool::TaskLoop() {
while (true) { while (true) {
std::unique_lock<std::mutex> lock(mutex_); Task task;
scheduled_.wait( {
lock, [this] { return !this->tasks_.empty() || !this->running_; }); std::unique_lock<std::mutex> lock(mutex_);
scheduled_.wait(
lock, [this] { return !this->tasks_.empty() || !this->running_; });
if (!running_ && tasks_.empty()) { if (!running_ && tasks_.empty()) {
return; return;
} }
if (tasks_.empty()) { if (tasks_.empty()) {
PADDLE_THROW("This thread has no task to Run"); PADDLE_THROW("This thread has no task to Run");
} }
// pop a task from the task queue // pop a task from the task queue
auto task = std::move(tasks_.front()); task = std::move(tasks_.front());
tasks_.pop(); tasks_.pop();
lock.unlock(); }
// run the task // run the task
task(); task();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册