Apollo 10.0
自动驾驶开放平台
apollo::prediction::BaseThreadPool类 参考

#include <prediction_thread_pool.h>

类 apollo::prediction::BaseThreadPool 继承关系图:
apollo::prediction::BaseThreadPool 的协作图:

Public 成员函数

 BaseThreadPool (int thread_num, int next_thread_pool_level)
 
void Stop ()
 
 ~BaseThreadPool ()
 
template<typename InputIter , typename F >
void ForEach (InputIter begin, InputIter end, F f)
 
template<typename FuncType >
std::future< typename std::result_of< FuncType()>::type > Post (FuncType &&func)
 

静态 Public 属性

static std::vector< int > THREAD_POOL_CAPACITY = {20, 20, 20}
 

详细描述

在文件 prediction_thread_pool.h34 行定义.

构造及析构函数说明

◆ BaseThreadPool()

apollo::prediction::BaseThreadPool::BaseThreadPool ( int  thread_num,
int  next_thread_pool_level 
)

在文件 prediction_thread_pool.cc25 行定义.

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}

◆ ~BaseThreadPool()

apollo::prediction::BaseThreadPool::~BaseThreadPool ( )

在文件 prediction_thread_pool.cc52 行定义.

52 {
53 if (stopped_.exchange(true)) {
54 return;
55 }
56 task_queue_.BreakAllWait();
57 for (std::thread& worker : workers_) {
58 worker.join();
59 }
60}

成员函数说明

◆ ForEach()

template<typename InputIter , typename F >
void apollo::prediction::BaseThreadPool::ForEach ( InputIter  begin,
InputIter  end,
f 
)
inline

在文件 prediction_thread_pool.h43 行定义.

43 {
44 std::vector<std::future<void>> futures;
45 for (auto iter = begin; iter != end; ++iter) {
46 auto& elem = *iter;
47 futures.emplace_back(this->Post([&] { f(elem); }));
48 }
49 for (auto& future : futures) {
50 if (future.valid()) {
51 future.get();
52 } else {
53 AERROR << "Future is invalid.";
54 }
55 }
56 }
double f
std::future< typename std::result_of< FuncType()>::type > Post(FuncType &&func)
#define AERROR
Definition log.h:44

◆ Post()

template<typename FuncType >
std::future< typename std::result_of< FuncType()>::type > apollo::prediction::BaseThreadPool::Post ( FuncType &&  func)
inline

在文件 prediction_thread_pool.h59 行定义.

59 {
60 typedef typename std::result_of<FuncType()>::type ReturnType;
61 typedef typename std::packaged_task<ReturnType()> TaskType;
62 // Post requires that the functions in it are copy-constructible.
63 // We used a shared pointer for the packaged_task,
64 // Since it's only movable and non-copyable
65 std::shared_ptr<TaskType> task =
66 std::make_shared<TaskType>(std::move(func));
67 std::future<ReturnType> returned_future = task->get_future();
68
69 // Note: variables eg. `task` must be copied here because of the lifetime
70 if (stopped_) {
71 return std::future<ReturnType>();
72 }
73 task_queue_.Enqueue([task]() { (*task)(); });
74 return returned_future;
75 }
bool Enqueue(const T &element)

◆ Stop()

void apollo::prediction::BaseThreadPool::Stop ( )

在文件 prediction_thread_pool.cc44 行定义.

44 {
45 task_queue_.BreakAllWait();
46 for (std::thread& worker : workers_) {
47 worker.join();
48 }
49 stopped_ = true;
50}

类成员变量说明

◆ THREAD_POOL_CAPACITY

std::vector< int > apollo::prediction::BaseThreadPool::THREAD_POOL_CAPACITY = {20, 20, 20}
static

在文件 prediction_thread_pool.h77 行定义.


该类的文档由以下文件生成: