Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
threadpool.h
Go to the documentation of this file.
1// Copyright 2010-2024 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14#ifndef OR_TOOLS_BASE_THREADPOOL_H_
15#define OR_TOOLS_BASE_THREADPOOL_H_
16
17#include <condition_variable> // NOLINT
18#include <functional>
19#include <list>
20#include <mutex> // NOLINT
21#include <string>
22#include <thread> // NOLINT
23#include <vector>
24
25#include "absl/strings/string_view.h"
26
27namespace operations_research {
29 public:
30 explicit ThreadPool(int num_threads);
31 ThreadPool(absl::string_view prefix, int num_threads);
33
34 void StartWorkers();
35 void Schedule(std::function<void()> closure);
36 std::function<void()> GetNextTask();
37 void SetQueueCapacity(int capacity);
38
39 private:
40 const int num_workers_;
41 std::list<std::function<void()>> tasks_;
42 std::mutex mutex_;
43 std::condition_variable condition_;
44 std::condition_variable capacity_condition_;
45 bool waiting_to_finish_ = false;
46 bool waiting_for_capacity_ = false;
47 bool started_ = false;
48 int queue_capacity_ = 2e9;
49 std::vector<std::thread> all_workers_;
50};
51} // namespace operations_research
52#endif // OR_TOOLS_BASE_THREADPOOL_H_
void Schedule(std::function< void()> closure)
Definition threadpool.cc:83
std::function< void()> GetNextTask()
Definition threadpool.cc:62
void SetQueueCapacity(int capacity)
Definition threadpool.cc:49
In SWIG mode, we don't want anything besides these top-level includes.