| #include <algorithm> | |
| #include <cstddef> | |
| #include <memory> | |
| #include <thread> // NOLINT(build/c++11): gBMC lacks absl threading | |
| #include <vector> | |
| #include "worker_pool.h" | |
| namespace safepower_agent { | |
| struct WorkerPool::Impl { | |
| std::vector<std::thread> workers; | |
| }; | |
| WorkerPool::WorkerPool(size_t num_workers) : impl_(std::make_unique<Impl>()) { | |
| const size_t count = std::max<size_t>(1, num_workers); | |
| impl_->workers.reserve(count); | |
| for (size_t i = 0; i < count; ++i) { | |
| impl_->workers.emplace_back([this]() { WorkerLoop(); }); | |
| } | |
| } | |
| WorkerPool::~WorkerPool() { | |
| queue_.Close(); | |
| for (std::thread& worker : impl_->workers) { | |
| if (worker.joinable()) { | |
| worker.join(); | |
| } | |
| } | |
| } | |
| } // namespace safepower_agent |