From 842560d214649fc0077838e5b02cc35e4af12526 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Tue, 15 Nov 2022 09:19:43 -0800 Subject: Use AnyInvocable in internal thread_pool PiperOrigin-RevId: 488676817 Change-Id: I13f15bb93ab6dda4c56caf969be3c14f84ada6a0 --- absl/synchronization/internal/thread_pool.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'absl/synchronization/internal') diff --git a/absl/synchronization/internal/thread_pool.h b/absl/synchronization/internal/thread_pool.h index 0cb96dac..5eb0bb60 100644 --- a/absl/synchronization/internal/thread_pool.h +++ b/absl/synchronization/internal/thread_pool.h @@ -20,9 +20,11 @@ #include #include #include // NOLINT(build/c++11) +#include #include #include "absl/base/thread_annotations.h" +#include "absl/functional/any_invocable.h" #include "absl/synchronization/mutex.h" namespace absl { @@ -33,6 +35,7 @@ namespace synchronization_internal { class ThreadPool { public: explicit ThreadPool(int num_threads) { + threads_.reserve(num_threads); for (int i = 0; i < num_threads; ++i) { threads_.push_back(std::thread(&ThreadPool::WorkLoop, this)); } @@ -54,7 +57,7 @@ class ThreadPool { } // Schedule a function to be run on a ThreadPool thread immediately. - void Schedule(std::function func) { + void Schedule(absl::AnyInvocable func) { assert(func != nullptr); absl::MutexLock l(&mu_); queue_.push(std::move(func)); @@ -67,7 +70,7 @@ class ThreadPool { void WorkLoop() { while (true) { - std::function func; + absl::AnyInvocable func; { absl::MutexLock l(&mu_); mu_.Await(absl::Condition(this, &ThreadPool::WorkAvailable)); @@ -82,7 +85,7 @@ class ThreadPool { } absl::Mutex mu_; - std::queue> queue_ ABSL_GUARDED_BY(mu_); + std::queue> queue_ ABSL_GUARDED_BY(mu_); std::vector threads_; }; -- cgit v1.2.3