1 // Copyright 2018 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef UTIL_WORKER_POOL_H_ 6 #define UTIL_WORKER_POOL_H_ 7 8 #include <condition_variable> 9 #include <functional> 10 #include <mutex> 11 #include <queue> 12 #include <thread> 13 14 #include "base/logging.h" 15 #include "base/macros.h" 16 17 class WorkerPool { 18 public: 19 WorkerPool(); 20 WorkerPool(size_t thread_count); 21 ~WorkerPool(); 22 23 void PostTask(std::function<void()> work); 24 25 private: 26 void Worker(); 27 28 std::vector<std::thread> threads_; 29 std::queue<std::function<void()>> task_queue_; 30 std::mutex queue_mutex_; 31 std::condition_variable_any pool_notifier_; 32 bool should_stop_processing_; 33 34 DISALLOW_COPY_AND_ASSIGN(WorkerPool); 35 }; 36 37 #endif // UTIL_WORKER_POOL_H_ 38