third_party/abseil-cpp/absl/synchronization/internal/thread_pool.h
Go to the documentation of this file.
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 
15 #ifndef ABSL_SYNCHRONIZATION_INTERNAL_THREAD_POOL_H_
16 #define ABSL_SYNCHRONIZATION_INTERNAL_THREAD_POOL_H_
17 
18 #include <cassert>
19 #include <cstddef>
20 #include <functional>
21 #include <queue>
22 #include <thread> // NOLINT(build/c++11)
23 #include <vector>
24 
25 #include "absl/base/thread_annotations.h"
26 #include "absl/synchronization/mutex.h"
27 
28 namespace absl {
30 namespace synchronization_internal {
31 
32 // A simple ThreadPool implementation for tests.
33 class ThreadPool {
34  public:
35  explicit ThreadPool(int num_threads) {
36  for (int i = 0; i < num_threads; ++i) {
37  threads_.push_back(std::thread(&ThreadPool::WorkLoop, this));
38  }
39  }
40 
41  ThreadPool(const ThreadPool &) = delete;
42  ThreadPool &operator=(const ThreadPool &) = delete;
43 
45  {
46  absl::MutexLock l(&mu_);
47  for (size_t i = 0; i < threads_.size(); i++) {
48  queue_.push(nullptr); // Shutdown signal.
49  }
50  }
51  for (auto &t : threads_) {
52  t.join();
53  }
54  }
55 
56  // Schedule a function to be run on a ThreadPool thread immediately.
57  void Schedule(std::function<void()> func) {
58  assert(func != nullptr);
59  absl::MutexLock l(&mu_);
60  queue_.push(std::move(func));
61  }
62 
63  private:
65  return !queue_.empty();
66  }
67 
68  void WorkLoop() {
69  while (true) {
70  std::function<void()> func;
71  {
72  absl::MutexLock l(&mu_);
74  func = std::move(queue_.front());
75  queue_.pop();
76  }
77  if (func == nullptr) { // Shutdown signal.
78  break;
79  }
80  func();
81  }
82  }
83 
86  std::vector<std::thread> threads_;
87 };
88 
89 } // namespace synchronization_internal
91 } // namespace absl
92 
93 #endif // ABSL_SYNCHRONIZATION_INTERNAL_THREAD_POOL_H_
absl::synchronization_internal::ThreadPool::WorkAvailable
bool WorkAvailable() const ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu_)
Definition: third_party/abseil-cpp/absl/synchronization/internal/thread_pool.h:64
const
#define const
Definition: bloaty/third_party/zlib/zconf.h:230
absl::synchronization_internal::ThreadPool::~ThreadPool
~ThreadPool()
Definition: third_party/abseil-cpp/absl/synchronization/internal/thread_pool.h:44
absl::synchronization_internal::ThreadPool
Definition: third_party/abseil-cpp/absl/synchronization/internal/thread_pool.h:33
absl::Mutex
Definition: abseil-cpp/absl/synchronization/mutex.h:131
absl::Mutex::Await
void Await(const Condition &cond)
Definition: abseil-cpp/absl/synchronization/mutex.cc:1548
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition: third_party/abseil-cpp/absl/base/config.h:171
absl::MutexLock
Definition: abseil-cpp/absl/synchronization/mutex.h:525
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
ABSL_EXCLUSIVE_LOCKS_REQUIRED
#define ABSL_EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: abseil-cpp/absl/base/thread_annotations.h:145
absl::synchronization_internal::ThreadPool::threads_
std::vector< std::thread > threads_
Definition: third_party/abseil-cpp/absl/synchronization/internal/thread_pool.h:86
absl::Condition
Definition: abseil-cpp/absl/synchronization/mutex.h:663
queue
struct queue queue
func
const EVP_CIPHER *(* func)(void)
Definition: cipher_extra.c:73
num_threads
static volatile int num_threads
Definition: benchmark-thread.c:30
absl::synchronization_internal::ThreadPool::ThreadPool
ThreadPool(int num_threads)
Definition: third_party/abseil-cpp/absl/synchronization/internal/thread_pool.h:35
absl::synchronization_internal::ThreadPool::WorkLoop
void WorkLoop()
Definition: third_party/abseil-cpp/absl/synchronization/internal/thread_pool.h:68
absl::synchronization_internal::ThreadPool::ABSL_GUARDED_BY
std::queue< std::function< void()> > queue_ ABSL_GUARDED_BY(mu_)
absl::synchronization_internal::ThreadPool::operator=
ThreadPool & operator=(const ThreadPool &)=delete
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
function
std::function< bool(GrpcTool *, int, const char **, const CliCredentials &, GrpcToolOutputCallback)> function
Definition: grpc_tool.cc:250
absl::synchronization_internal::ThreadPool::mu_
absl::Mutex mu_
Definition: third_party/abseil-cpp/absl/synchronization/internal/thread_pool.h:84
thread
static uv_thread_t thread
Definition: test-async-null-cb.c:29
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
absl::synchronization_internal::ThreadPool::Schedule
void Schedule(std::function< void()> func)
Definition: third_party/abseil-cpp/absl/synchronization/internal/thread_pool.h:57


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:36