19#include "absl/log/check.h"
20#include "absl/strings/string_view.h"
25 std::function<void()> work = thread_pool->
GetNextTask();
26 while (work !=
nullptr) {
35 : num_workers_(num_threads) {}
39 std::unique_lock<std::mutex> mutex_lock(mutex_);
40 waiting_to_finish_ =
true;
42 condition_.notify_all();
43 for (
int i = 0; i < num_workers_; ++i) {
44 all_workers_[i].join();
50 CHECK_GT(capacity, num_workers_);
52 queue_capacity_ = capacity;
57 for (
int i = 0; i < num_workers_; ++i) {
58 all_workers_.push_back(std::thread(&
RunWorker,
this));
63 std::unique_lock<std::mutex> lock(mutex_);
65 if (!tasks_.empty()) {
66 std::function<void()> task = tasks_.front();
68 if (tasks_.size() < queue_capacity_ && waiting_for_capacity_) {
69 waiting_for_capacity_ =
false;
70 capacity_condition_.notify_all();
74 if (waiting_to_finish_) {
77 condition_.wait(lock);
84 std::unique_lock<std::mutex> lock(mutex_);
85 while (tasks_.size() >= queue_capacity_) {
86 waiting_for_capacity_ =
true;
87 capacity_condition_.wait(lock);
89 tasks_.push_back(closure);
92 condition_.notify_all();
void Schedule(std::function< void()> closure)
std::function< void()> GetNextTask()
ThreadPool(int num_threads)
void SetQueueCapacity(int capacity)
In SWIG mode, we don't want anything besides these top-level includes.
void RunWorker(void *data)