Apollo  6.0
Open source self driving car software
thread_pool.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2018 The Apollo Authors. All Rights Reserved.
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 #pragma once
17 
18 #include <vector>
19 
20 #include "google/protobuf/stubs/callback.h"
21 #include "google/protobuf/stubs/common.h"
22 
26 
27 namespace apollo {
28 namespace perception {
29 namespace lib {
30 
31 class ThreadPoolWorker;
32 
33 class ThreadPool {
34  public:
35  explicit ThreadPool(int num_workers);
36 
37  ~ThreadPool();
38 
39  void Start();
40 
41  void Add(google::protobuf::Closure *closure);
42  void Add(const std::vector<google::protobuf::Closure *> &closures);
43 
44  int num_workers() const { return num_workers_; }
45 
46  int num_available_workers() const { return num_available_workers_; }
47 
48  ThreadPool(const ThreadPool &) = delete;
49  ThreadPool &operator=(const ThreadPool &) = delete;
50 
51  private:
52  friend class ThreadPoolWorker;
53 
54  std::vector<ThreadPoolWorker *> workers_;
55  int num_workers_;
56  int num_available_workers_;
57  Mutex mutex_;
58 
60  bool started_;
61 };
62 
63 class ThreadPoolWorker : public Thread {
64  public:
65  explicit ThreadPoolWorker(ThreadPool *thread_pool)
66  : Thread(true, "ThreadPoolWorker"), thread_pool_(thread_pool) {}
67 
68  virtual ~ThreadPoolWorker() {}
69 
70  ThreadPoolWorker(const ThreadPoolWorker &) = delete;
71  ThreadPoolWorker &operator=(const ThreadPoolWorker &) = delete;
72 
73  protected:
74  void Run() override;
75 
76  private:
77  ThreadPool *thread_pool_;
78 };
79 
80 } // namespace lib
81 } // namespace perception
82 } // namespace apollo
Definition: thread_pool.h:33
void Add(google::protobuf::Closure *closure)
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
Definition: mutex.h:24
virtual ~ThreadPoolWorker()
Definition: thread_pool.h:68
ThreadPoolWorker(ThreadPool *thread_pool)
Definition: thread_pool.h:65
Definition: thread.h:24
friend class ThreadPoolWorker
Definition: thread_pool.h:52
Definition: concurrent_queue.h:89
int num_available_workers() const
Definition: thread_pool.h:46
int num_workers() const
Definition: thread_pool.h:44
ThreadPool & operator=(const ThreadPool &)=delete
Definition: thread_pool.h:63