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