| 74 | } |
| 75 | |
| 76 | void ThreadPool::TaskLoop() { |
| 77 | while (true) { |
| 78 | Task task; |
| 79 | |
| 80 | { |
| 81 | std::unique_lock<std::mutex> lock(mutex_); |
| 82 | scheduled_.wait( |
| 83 | lock, [this] { return !this->tasks_.empty() || !this->running_; }); |
| 84 | |
| 85 | if (!running_ && tasks_.empty()) { |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | if (tasks_.empty()) { |
| 90 | PADDLE_THROW( |
| 91 | common::errors::Unavailable("Current thread has no task to Run.")); |
| 92 | } |
| 93 | |
| 94 | // pop a task from the task queue |
| 95 | task = std::move(tasks_.front()); |
| 96 | tasks_.pop(); |
| 97 | } |
| 98 | // run the task |
| 99 | task(); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | std::unique_ptr<ThreadPool> ThreadPoolIO::io_threadpool_(nullptr); |
| 104 | std::once_flag ThreadPoolIO::io_init_flag_; |