thread_pool.h
Go to the documentation of this file.
1 /*
2  * Copyright 2016 The Cartographer Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef CARTOGRAPHER_COMMON_THREAD_POOL_H_
18 #define CARTOGRAPHER_COMMON_THREAD_POOL_H_
19 
20 #include <deque>
21 #include <functional>
22 #include <memory>
23 #include <thread>
24 #include <unordered_map>
25 #include <vector>
26 
29 
30 namespace cartographer {
31 namespace common {
32 
33 class Task;
34 
36  public:
38  virtual ~ThreadPoolInterface() {}
39  virtual std::weak_ptr<Task> Schedule(std::unique_ptr<Task> task) = 0;
40 
41  protected:
42  void Execute(Task* task);
43  void SetThreadPool(Task* task);
44 
45  private:
46  friend class Task;
47 
48  virtual void NotifyDependenciesCompleted(Task* task) = 0;
49 };
50 
51 // A fixed number of threads working on tasks. Adding a task does not block.
52 // Tasks may be added whether or not their dependencies are completed.
53 // When all dependencies of a task are completed, it is queued up for execution
54 // in a background thread. The queue must be empty before calling the
55 // destructor. The thread pool will then wait for the currently executing work
56 // items to finish and then destroy the threads.
58  public:
59  explicit ThreadPool(int num_threads);
60  ~ThreadPool();
61 
62  ThreadPool(const ThreadPool&) = delete;
63  ThreadPool& operator=(const ThreadPool&) = delete;
64 
65  // When the returned weak pointer is expired, 'task' has certainly completed,
66  // so dependants no longer need to add it as a dependency.
67  std::weak_ptr<Task> Schedule(std::unique_ptr<Task> task)
68  EXCLUDES(mutex_) override;
69 
70  private:
71  void DoWork();
72 
73  void NotifyDependenciesCompleted(Task* task) EXCLUDES(mutex_) override;
74 
75  Mutex mutex_;
76  bool running_ GUARDED_BY(mutex_) = true;
77  std::vector<std::thread> pool_ GUARDED_BY(mutex_);
78  std::deque<std::shared_ptr<Task>> task_queue_ GUARDED_BY(mutex_);
79  std::unordered_map<Task*, std::shared_ptr<Task>> tasks_not_ready_
80  GUARDED_BY(mutex_);
81 };
82 
83 } // namespace common
84 } // namespace cartographer
85 
86 #endif // CARTOGRAPHER_COMMON_THREAD_POOL_H_
#define EXCLUDES(...)
Definition: mutex.h:53
#define GUARDED_BY(x)
Definition: mutex.h:40
virtual void NotifyDependenciesCompleted(Task *task)=0
std::map< Task *, std::shared_ptr< Task > > tasks_not_ready_
Definition: task_test.cc:64
std::deque< std::shared_ptr< Task > > task_queue_
Definition: task_test.cc:63
virtual std::weak_ptr< Task > Schedule(std::unique_ptr< Task > task)=0
Mutex mutex_


cartographer
Author(s): The Cartographer Authors
autogenerated on Mon Feb 28 2022 22:00:58