Apollo 10.0
自动驾驶开放平台
thread_pool.h
浏览该文件的文档.
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
27namespace apollo {
28namespace perception {
29namespace lib {
30
31class ThreadPoolWorker;
32
34 public:
35 explicit ThreadPool(int num_workers);
36
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
63class ThreadPoolWorker : public Thread {
64 public:
65 explicit ThreadPoolWorker(ThreadPool *thread_pool)
66 : Thread(true, "ThreadPoolWorker"), thread_pool_(thread_pool) {}
67
68 virtual ~ThreadPoolWorker() {}
69
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
ThreadPoolWorker & operator=(const ThreadPoolWorker &)=delete
ThreadPoolWorker(const ThreadPoolWorker &)=delete
ThreadPoolWorker(ThreadPool *thread_pool)
Definition thread_pool.h:65
ThreadPool(const ThreadPool &)=delete
void Add(const std::vector< google::protobuf::Closure * > &closures)
ThreadPool & operator=(const ThreadPool &)=delete
void Add(google::protobuf::Closure *closure)
class register implement
Definition arena_queue.h:37