thread_pool.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef Y_ABSL_SYNCHRONIZATION_INTERNAL_THREAD_POOL_H_
  15. #define Y_ABSL_SYNCHRONIZATION_INTERNAL_THREAD_POOL_H_
  16. #include <cassert>
  17. #include <cstddef>
  18. #include <functional>
  19. #include <queue>
  20. #include <thread> // NOLINT(build/c++11)
  21. #include <utility>
  22. #include <vector>
  23. #include "y_absl/base/thread_annotations.h"
  24. #include "y_absl/functional/any_invocable.h"
  25. #include "y_absl/synchronization/mutex.h"
  26. namespace y_absl {
  27. Y_ABSL_NAMESPACE_BEGIN
  28. namespace synchronization_internal {
  29. // A simple ThreadPool implementation for tests.
  30. class ThreadPool {
  31. public:
  32. explicit ThreadPool(int num_threads) {
  33. threads_.reserve(num_threads);
  34. for (int i = 0; i < num_threads; ++i) {
  35. threads_.push_back(std::thread(&ThreadPool::WorkLoop, this));
  36. }
  37. }
  38. ThreadPool(const ThreadPool &) = delete;
  39. ThreadPool &operator=(const ThreadPool &) = delete;
  40. ~ThreadPool() {
  41. {
  42. y_absl::MutexLock l(&mu_);
  43. for (size_t i = 0; i < threads_.size(); i++) {
  44. queue_.push(nullptr); // Shutdown signal.
  45. }
  46. }
  47. for (auto &t : threads_) {
  48. t.join();
  49. }
  50. }
  51. // Schedule a function to be run on a ThreadPool thread immediately.
  52. void Schedule(y_absl::AnyInvocable<void()> func) {
  53. assert(func != nullptr);
  54. y_absl::MutexLock l(&mu_);
  55. queue_.push(std::move(func));
  56. }
  57. private:
  58. bool WorkAvailable() const Y_ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu_) {
  59. return !queue_.empty();
  60. }
  61. void WorkLoop() {
  62. while (true) {
  63. y_absl::AnyInvocable<void()> func;
  64. {
  65. y_absl::MutexLock l(&mu_);
  66. mu_.Await(y_absl::Condition(this, &ThreadPool::WorkAvailable));
  67. func = std::move(queue_.front());
  68. queue_.pop();
  69. }
  70. if (func == nullptr) { // Shutdown signal.
  71. break;
  72. }
  73. func();
  74. }
  75. }
  76. y_absl::Mutex mu_;
  77. std::queue<y_absl::AnyInvocable<void()>> queue_ Y_ABSL_GUARDED_BY(mu_);
  78. std::vector<std::thread> threads_;
  79. };
  80. } // namespace synchronization_internal
  81. Y_ABSL_NAMESPACE_END
  82. } // namespace y_absl
  83. #endif // Y_ABSL_SYNCHRONIZATION_INTERNAL_THREAD_POOL_H_