#include <prediction_thread_pool.h>
|
| 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) |
|
◆ BaseThreadPool()
apollo::prediction::BaseThreadPool::BaseThreadPool |
( |
int |
thread_num, |
|
|
int |
next_thread_pool_level |
|
) |
| |
在文件 prediction_thread_pool.cc 第 25 行定义.
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] {
34 while (!stopped_) {
35 std::function<void()> task;
37 task();
38 }
39 }
40 });
41 }
42}
bool WaitDequeue(T *element)
static thread_local int s_thread_pool_level
◆ ~BaseThreadPool()
apollo::prediction::BaseThreadPool::~BaseThreadPool |
( |
| ) |
|
在文件 prediction_thread_pool.cc 第 52 行定义.
52 {
53 if (stopped_.exchange(true)) {
54 return;
55 }
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 |
f |
|
) |
| |
|
inline |
在文件 prediction_thread_pool.h 第 43 行定义.
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 }
std::future< typename std::result_of< FuncType()>::type > Post(FuncType &&func)
◆ Post()
template<typename FuncType >
std::future< typename std::result_of< FuncType()>::type > apollo::prediction::BaseThreadPool::Post |
( |
FuncType && |
func | ) |
|
|
inline |
在文件 prediction_thread_pool.h 第 59 行定义.
59 {
60 typedef typename std::result_of<FuncType()>::type ReturnType;
61 typedef typename std::packaged_task<ReturnType()>
TaskType;
62
63
64
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
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 |
( |
| ) |
|
◆ THREAD_POOL_CAPACITY
std::vector< int > apollo::prediction::BaseThreadPool::THREAD_POOL_CAPACITY = {20, 20, 20} |
|
static |
该类的文档由以下文件生成: