|
|
@ -57,10 +57,10 @@ ThreadPool::ThreadPool(int num_threads) : running_(true) {
|
|
|
|
ThreadPool::~ThreadPool() {
|
|
|
|
ThreadPool::~ThreadPool() {
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// notify all threads to stop running
|
|
|
|
// notify all threads to stop running
|
|
|
|
std::lock_guard<std::mutex> l(mutex_);
|
|
|
|
std::unique_lock<std::mutex> l(mutex_);
|
|
|
|
running_ = false;
|
|
|
|
running_ = false;
|
|
|
|
scheduled_.notify_all();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
scheduled_.notify_all();
|
|
|
|
|
|
|
|
|
|
|
|
for (auto& t : threads_) {
|
|
|
|
for (auto& t : threads_) {
|
|
|
|
t->join();
|
|
|
|
t->join();
|
|
|
@ -70,19 +70,25 @@ 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_; });
|
|
|
|
|
|
|
|
|
|
|
|
if (!running_ || tasks_.empty()) {
|
|
|
|
if (!running_ && tasks_.empty()) {
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (tasks_.empty()) {
|
|
|
|
|
|
|
|
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();
|
|
|
|