aboutsummaryrefslogtreecommitdiff
path: root/absl/synchronization/internal/thread_pool.h
diff options
context:
space:
mode:
authorBenjamin Barenblat <bbaren@google.com>2023-05-08 12:51:08 -0400
committerBenjamin Barenblat <bbaren@google.com>2023-05-08 12:51:08 -0400
commitc15cec707b1c7d847e59d2db4d0b82b711a7ee6d (patch)
tree79f448d5bbc8cf52917b0b091f0e1ab60a419c85 /absl/synchronization/internal/thread_pool.h
parentf4f2c1da90c4e6a0683c4e66c0268baa1b79cdf3 (diff)
parentc2435f8342c2d0ed8101cb43adfd605fdc52dca2 (diff)
downloadabseil-c15cec707b1c7d847e59d2db4d0b82b711a7ee6d.tar.gz
abseil-c15cec707b1c7d847e59d2db4d0b82b711a7ee6d.tar.bz2
abseil-c15cec707b1c7d847e59d2db4d0b82b711a7ee6d.zip
Merge new upstream LTS 20230125.3
Diffstat (limited to 'absl/synchronization/internal/thread_pool.h')
-rw-r--r--absl/synchronization/internal/thread_pool.h9
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_;
};