| 603 | } |
| 604 | |
| 605 | Status ThreadPool::Shutdown(bool wait) { |
| 606 | std::unique_lock<std::mutex> lock(state_->mutex_); |
| 607 | |
| 608 | if (state_->please_shutdown_) { |
| 609 | return Status::Invalid("Shutdown() already called"); |
| 610 | } |
| 611 | state_->please_shutdown_ = true; |
| 612 | state_->quick_shutdown_ = !wait; |
| 613 | state_->cv_.notify_all(); |
| 614 | state_->cv_shutdown_.wait(lock, [this] { return state_->workers_.empty(); }); |
| 615 | if (!state_->quick_shutdown_) { |
| 616 | DCHECK_EQ(state_->pending_tasks_.size(), 0); |
| 617 | } else { |
| 618 | std::priority_queue<QueuedTask> empty; |
| 619 | std::swap(state_->pending_tasks_, empty); |
| 620 | } |
| 621 | CollectFinishedWorkersUnlocked(); |
| 622 | return Status::OK(); |
| 623 | } |
| 624 | |
| 625 | void ThreadPool::CollectFinishedWorkersUnlocked() { |
| 626 | for (auto& thread : state_->finished_workers_) { |