You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

thread_pool.h 3.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 ABSL_SYNCHRONIZATION_INTERNAL_THREAD_POOL_H_
  15. #define 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 <vector>
  22. #include "absl/base/thread_annotations.h"
  23. #include "absl/synchronization/mutex.h"
  24. namespace absl
  25. {
  26. ABSL_NAMESPACE_BEGIN
  27. namespace synchronization_internal
  28. {
  29. // A simple ThreadPool implementation for tests.
  30. class ThreadPool
  31. {
  32. public:
  33. explicit ThreadPool(int num_threads)
  34. {
  35. for (int i = 0; i < num_threads; ++i)
  36. {
  37. threads_.push_back(std::thread(&ThreadPool::WorkLoop, this));
  38. }
  39. }
  40. ThreadPool(const ThreadPool&) = delete;
  41. ThreadPool& operator=(const ThreadPool&) = delete;
  42. ~ThreadPool()
  43. {
  44. {
  45. absl::MutexLock l(&mu_);
  46. for (size_t i = 0; i < threads_.size(); i++)
  47. {
  48. queue_.push(nullptr); // Shutdown signal.
  49. }
  50. }
  51. for (auto& t : threads_)
  52. {
  53. t.join();
  54. }
  55. }
  56. // Schedule a function to be run on a ThreadPool thread immediately.
  57. void Schedule(std::function<void()> func)
  58. {
  59. assert(func != nullptr);
  60. absl::MutexLock l(&mu_);
  61. queue_.push(std::move(func));
  62. }
  63. private:
  64. bool WorkAvailable() const ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu_)
  65. {
  66. return !queue_.empty();
  67. }
  68. void WorkLoop()
  69. {
  70. while (true)
  71. {
  72. std::function<void()> func;
  73. {
  74. absl::MutexLock l(&mu_);
  75. mu_.Await(absl::Condition(this, &ThreadPool::WorkAvailable));
  76. func = std::move(queue_.front());
  77. queue_.pop();
  78. }
  79. if (func == nullptr)
  80. { // Shutdown signal.
  81. break;
  82. }
  83. func();
  84. }
  85. }
  86. absl::Mutex mu_;
  87. std::queue<std::function<void()>> queue_ ABSL_GUARDED_BY(mu_);
  88. std::vector<std::thread> threads_;
  89. };
  90. } // namespace synchronization_internal
  91. ABSL_NAMESPACE_END
  92. } // namespace absl
  93. #endif // ABSL_SYNCHRONIZATION_INTERNAL_THREAD_POOL_H_