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

optimize thread pool code

test=develop
上级 fe4cd502
...@@ -70,8 +70,10 @@ ThreadPool::~ThreadPool() { ...@@ -70,8 +70,10 @@ ThreadPool::~ThreadPool() {
void ThreadPool::TaskLoop() { void ThreadPool::TaskLoop() {
while (true) { while (true) {
std::unique_lock<std::mutex> lock(mutex_); Task task;
{
std::unique_lock<std::mutex> lock(mutex_);
scheduled_.wait( scheduled_.wait(
lock, [this] { return !this->tasks_.empty() || !this->running_; }); lock, [this] { return !this->tasks_.empty() || !this->running_; });
...@@ -84,9 +86,9 @@ void ThreadPool::TaskLoop() { ...@@ -84,9 +86,9 @@ void ThreadPool::TaskLoop() {
} }
// 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.
先完成此消息的编辑!
想要评论请 注册