16 #ifndef TENSORFLOW_SERVING_UTIL_NET_HTTP_INTERNAL_FIXED_THREAD_POOL_H_
17 #define TENSORFLOW_SERVING_UTIL_NET_HTTP_INTERNAL_FIXED_THREAD_POOL_H_
25 #include "absl/base/thread_annotations.h"
26 #include "absl/synchronization/mutex.h"
28 namespace tensorflow {
38 for (
int i = 0; i < num_threads; ++i) {
39 threads_.push_back(std::thread(&FixedThreadPool::WorkLoop,
this));
48 absl::MutexLock l(&mu_);
49 for (
int i = 0; i < threads_.size(); ++i) {
53 for (
auto &t : threads_) {
59 void Schedule(std::function<
void()> func) {
60 assert(func !=
nullptr);
61 absl::MutexLock l(&mu_);
62 queue_.push(std::move(func));
66 bool WorkAvailable()
const ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu_) {
67 return !queue_.empty();
72 std::function<void()> func;
74 absl::MutexLock l(&mu_);
75 mu_.Await(absl::Condition(
this, &FixedThreadPool::WorkAvailable));
76 func = std::move(queue_.front());
79 if (func ==
nullptr) {
87 std::queue<std::function<void()>> queue_ ABSL_GUARDED_BY(mu_);
88 std::vector<std::thread> threads_;