• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 class WorkerPool {
17  public:
18   WorkerPool();
19   WorkerPool(size_t thread_count);
20   ~WorkerPool();
21 
22   void PostTask(std::function<void()> work);
23 
24  private:
25   void Worker();
26 
27   std::vector<std::thread> threads_;
28   std::queue<std::function<void()>> task_queue_;
29   std::mutex queue_mutex_;
30   std::condition_variable_any pool_notifier_;
31   bool should_stop_processing_;
32 
33   WorkerPool(const WorkerPool&) = delete;
34   WorkerPool& operator=(const WorkerPool&) = delete;
35 };
36 
37 #endif  // UTIL_WORKER_POOL_H_
38