OR-Tools  8.1
threadpool.h
Go to the documentation of this file.
1 // Copyright 2010-2018 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 namespace operations_research {
26 class ThreadPool {
27  public:
28  ThreadPool(const std::string& prefix, int num_threads);
29  ~ThreadPool();
30 
31  void StartWorkers();
32  void Schedule(std::function<void()> closure);
33  std::function<void()> GetNextTask();
34  void SetQueueCapacity(int capacity);
35 
36  private:
37  const int num_workers_;
38  std::list<std::function<void()>> tasks_;
39  std::mutex mutex_;
40  std::condition_variable condition_;
41  std::condition_variable capacity_condition_;
42  bool waiting_to_finish_ = false;
43  bool waiting_for_capacity_ = false;
44  bool started_ = false;
45  int queue_capacity_ = 2e9;
46  std::vector<std::thread> all_workers_;
47 };
48 } // namespace operations_research
49 #endif // OR_TOOLS_BASE_THREADPOOL_H_
operations_research
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
Definition: dense_doubly_linked_list.h:21
operations_research::ThreadPool::Schedule
void Schedule(std::function< void()> closure)
Definition: threadpool.cc:77
operations_research::ThreadPool::StartWorkers
void StartWorkers()
Definition: threadpool.cc:49
operations_research::ThreadPool::GetNextTask
std::function< void()> GetNextTask()
Definition: threadpool.cc:56
operations_research::ThreadPool::ThreadPool
ThreadPool(const std::string &prefix, int num_threads)
Definition: threadpool.cc:28
operations_research::ThreadPool::~ThreadPool
~ThreadPool()
Definition: threadpool.cc:31
capacity
int64 capacity
Definition: routing_flow.cc:129
operations_research::ThreadPool
Definition: threadpool.h:26
operations_research::ThreadPool::SetQueueCapacity
void SetQueueCapacity(int capacity)
Definition: threadpool.cc:43