diff options
author | Abseil Team <absl-team@google.com> | 2022-11-15 09:19:43 -0800 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2022-11-15 09:20:36 -0800 |
commit | 842560d214649fc0077838e5b02cc35e4af12526 (patch) | |
tree | a8de03a6098fb52a73f356bc180129e1e0f8af2d /absl/synchronization/internal | |
parent | d6fa16c8c5a067382ba991fe9765772ee2698088 (diff) | |
download | abseil-842560d214649fc0077838e5b02cc35e4af12526.tar.gz abseil-842560d214649fc0077838e5b02cc35e4af12526.tar.bz2 abseil-842560d214649fc0077838e5b02cc35e4af12526.zip |
Use AnyInvocable in internal thread_pool
PiperOrigin-RevId: 488676817
Change-Id: I13f15bb93ab6dda4c56caf969be3c14f84ada6a0
Diffstat (limited to 'absl/synchronization/internal')
-rw-r--r-- | absl/synchronization/internal/thread_pool.h | 9 |
1 files changed, 6 insertions, 3 deletions
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 <functional> #include <queue> #include <thread> // NOLINT(build/c++11) +#include <utility> #include <vector> #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<void()> func) { + void Schedule(absl::AnyInvocable<void()> 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<void()> func; + absl::AnyInvocable<void()> func; { absl::MutexLock l(&mu_); mu_.Await(absl::Condition(this, &ThreadPool::WorkAvailable)); @@ -82,7 +85,7 @@ class ThreadPool { } absl::Mutex mu_; - std::queue<std::function<void()>> queue_ ABSL_GUARDED_BY(mu_); + std::queue<absl::AnyInvocable<void()>> queue_ ABSL_GUARDED_BY(mu_); std::vector<std::thread> threads_; }; |