Apollo 11.0
自动驾驶开放平台
prediction_thread_pool.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2019 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
18
19namespace apollo {
20namespace prediction {
21
23std::vector<int> BaseThreadPool::THREAD_POOL_CAPACITY = {20, 20, 20};
24
25BaseThreadPool::BaseThreadPool(int thread_num, int next_thread_pool_level)
26 : stopped_(false) {
27 if (!task_queue_.Init(thread_num,
29 throw std::runtime_error("Task queue init failed.");
30 }
31 for (int i = 0; i < thread_num; ++i) {
32 workers_.emplace_back([this, next_thread_pool_level, i] {
33 PredictionThreadPool::s_thread_pool_level = next_thread_pool_level;
34 while (!stopped_) {
35 std::function<void()> task;
36 if (task_queue_.WaitDequeue(&task)) {
37 task();
38 }
39 }
40 });
41 }
42}
43
45 task_queue_.BreakAllWait();
46 for (std::thread& worker : workers_) {
47 worker.join();
48 }
49 stopped_ = true;
50}
51
53 if (stopped_.exchange(true)) {
54 return;
55 }
56 task_queue_.BreakAllWait();
57 for (std::thread& worker : workers_) {
58 worker.join();
59 }
60}
61
63 int pool_level = s_thread_pool_level;
64 int max_thread_pool_level =
65 static_cast<int>(BaseThreadPool::THREAD_POOL_CAPACITY.size());
66 CHECK_LT(pool_level, max_thread_pool_level);
67 int index = s_thread_pool_level;
68 switch (index) {
69 case 0: {
71 }
72 case 1: {
74 }
75 case 2: {
77 }
78 }
79 AERROR << "Should not hit here";
81}
82
83} // namespace prediction
84} // namespace apollo
BaseThreadPool(int thread_num, int next_thread_pool_level)
static std::vector< int > THREAD_POOL_CAPACITY
#define AERROR
Definition log.h:44
class register implement
Definition arena_queue.h:37