summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Abseil Team <absl-team@google.com>2022-11-15 09:19:43 -0800
committerGravatar Copybara-Service <copybara-worker@google.com>2022-11-15 09:20:36 -0800
commit842560d214649fc0077838e5b02cc35e4af12526 (patch)
treea8de03a6098fb52a73f356bc180129e1e0f8af2d
parentd6fa16c8c5a067382ba991fe9765772ee2698088 (diff)
Use AnyInvocable in internal thread_pool
PiperOrigin-RevId: 488676817 Change-Id: I13f15bb93ab6dda4c56caf969be3c14f84ada6a0
-rw-r--r--absl/synchronization/BUILD.bazel1
-rw-r--r--absl/synchronization/CMakeLists.txt3
-rw-r--r--absl/synchronization/internal/thread_pool.h9
3 files changed, 9 insertions, 4 deletions
diff --git a/absl/synchronization/BUILD.bazel b/absl/synchronization/BUILD.bazel
index 66bd8742..078e22a6 100644
--- a/absl/synchronization/BUILD.bazel
+++ b/absl/synchronization/BUILD.bazel
@@ -200,6 +200,7 @@ cc_library(
deps = [
":synchronization",
"//absl/base:core_headers",
+ "//absl/functional:any_invocable",
],
)
diff --git a/absl/synchronization/CMakeLists.txt b/absl/synchronization/CMakeLists.txt
index 9335c264..975cb995 100644
--- a/absl/synchronization/CMakeLists.txt
+++ b/absl/synchronization/CMakeLists.txt
@@ -136,8 +136,9 @@ absl_cc_library(
COPTS
${ABSL_DEFAULT_COPTS}
DEPS
- absl::synchronization
+ absl::any_invocable
absl::core_headers
+ absl::synchronization
TESTONLY
)
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_;
};