Shaka Packager SDK
thread_pool.h
1 // Copyright 2022 Google LLC. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #ifndef PACKAGER_FILE_THREAD_POOL_H_
8 #define PACKAGER_FILE_THREAD_POOL_H_
9 
10 #include <functional>
11 #include <queue>
12 
13 #include <absl/base/thread_annotations.h>
14 #include <absl/synchronization/mutex.h>
15 
16 #include <packager/macros/classes.h>
17 
18 namespace shaka {
19 
24 class ThreadPool {
25  public:
26  typedef std::function<void()> Task;
27 
28  ThreadPool();
29  ~ThreadPool();
30 
33  void PostTask(const Task& task);
34 
35  static ThreadPool instance;
36 
37  private:
40  void Terminate();
41 
42  Task WaitForTask();
43  void ThreadMain();
44 
45  absl::Mutex mutex_;
46  absl::CondVar tasks_available_ ABSL_GUARDED_BY(mutex_);
47  std::queue<Task> tasks_ ABSL_GUARDED_BY(mutex_);
48  size_t num_idle_threads_ ABSL_GUARDED_BY(mutex_);
49  bool terminated_ ABSL_GUARDED_BY(mutex_);
50 
51  DISALLOW_COPY_AND_ASSIGN(ThreadPool);
52 };
53 
54 } // namespace shaka
55 
56 #endif // PACKAGER_FILE_THREAD_POOL_H_
void PostTask(const Task &task)
Definition: thread_pool.cc:32
All the methods that are virtual are virtual for mocking.
Definition: crypto_flags.cc:66