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 <thread>
23 #include <vector>
24 
26 
27 namespace cartographer {
28 namespace common {
29 
30 // A fixed number of threads working on a work queue of work items. Adding a
31 // new work item does not block, and will be executed by a background thread
32 // eventually. The queue must be empty before calling the destructor. The thread
33 // pool will then wait for the currently executing work items to finish and then
34 // destroy the threads.
35 class ThreadPool {
36  public:
37  explicit ThreadPool(int num_threads);
38  ~ThreadPool();
39 
40  ThreadPool(const ThreadPool&) = delete;
41  ThreadPool& operator=(const ThreadPool&) = delete;
42 
43  void Schedule(std::function<void()> work_item);
44 
45  private:
46  void DoWork();
47 
48  Mutex mutex_;
49  bool running_ GUARDED_BY(mutex_) = true;
50  std::vector<std::thread> pool_ GUARDED_BY(mutex_);
51  std::deque<std::function<void()>> work_queue_ GUARDED_BY(mutex_);
52 };
53 
54 } // namespace common
55 } // namespace cartographer
56 
57 #endif // CARTOGRAPHER_COMMON_THREAD_POOL_H_
void Schedule(std::function< void()> work_item)
Definition: thread_pool.cc:48
bool running_ GUARDED_BY(mutex_)
ThreadPool & operator=(const ThreadPool &)=delete


cartographer
Author(s):
autogenerated on Mon Jun 10 2019 12:51:39