Commit 842560d2 by Abseil Team Committed by Copybara-Service

Use AnyInvocable in internal thread_pool

PiperOrigin-RevId: 488676817
Change-Id: I13f15bb93ab6dda4c56caf969be3c14f84ada6a0
parent d6fa16c8
...@@ -200,6 +200,7 @@ cc_library( ...@@ -200,6 +200,7 @@ cc_library(
deps = [ deps = [
":synchronization", ":synchronization",
"//absl/base:core_headers", "//absl/base:core_headers",
"//absl/functional:any_invocable",
], ],
) )
......
...@@ -136,8 +136,9 @@ absl_cc_library( ...@@ -136,8 +136,9 @@ absl_cc_library(
COPTS COPTS
${ABSL_DEFAULT_COPTS} ${ABSL_DEFAULT_COPTS}
DEPS DEPS
absl::synchronization absl::any_invocable
absl::core_headers absl::core_headers
absl::synchronization
TESTONLY TESTONLY
) )
......
...@@ -20,9 +20,11 @@ ...@@ -20,9 +20,11 @@
#include <functional> #include <functional>
#include <queue> #include <queue>
#include <thread> // NOLINT(build/c++11) #include <thread> // NOLINT(build/c++11)
#include <utility>
#include <vector> #include <vector>
#include "absl/base/thread_annotations.h" #include "absl/base/thread_annotations.h"
#include "absl/functional/any_invocable.h"
#include "absl/synchronization/mutex.h" #include "absl/synchronization/mutex.h"
namespace absl { namespace absl {
...@@ -33,6 +35,7 @@ namespace synchronization_internal { ...@@ -33,6 +35,7 @@ namespace synchronization_internal {
class ThreadPool { class ThreadPool {
public: public:
explicit ThreadPool(int num_threads) { explicit ThreadPool(int num_threads) {
threads_.reserve(num_threads);
for (int i = 0; i < num_threads; ++i) { for (int i = 0; i < num_threads; ++i) {
threads_.push_back(std::thread(&ThreadPool::WorkLoop, this)); threads_.push_back(std::thread(&ThreadPool::WorkLoop, this));
} }
...@@ -54,7 +57,7 @@ class ThreadPool { ...@@ -54,7 +57,7 @@ class ThreadPool {
} }
// Schedule a function to be run on a ThreadPool thread immediately. // 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); assert(func != nullptr);
absl::MutexLock l(&mu_); absl::MutexLock l(&mu_);
queue_.push(std::move(func)); queue_.push(std::move(func));
...@@ -67,7 +70,7 @@ class ThreadPool { ...@@ -67,7 +70,7 @@ class ThreadPool {
void WorkLoop() { void WorkLoop() {
while (true) { while (true) {
std::function<void()> func; absl::AnyInvocable<void()> func;
{ {
absl::MutexLock l(&mu_); absl::MutexLock l(&mu_);
mu_.Await(absl::Condition(this, &ThreadPool::WorkAvailable)); mu_.Await(absl::Condition(this, &ThreadPool::WorkAvailable));
...@@ -82,7 +85,7 @@ class ThreadPool { ...@@ -82,7 +85,7 @@ class ThreadPool {
} }
absl::Mutex mu_; 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_; std::vector<std::thread> threads_;
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment