Apollo 10.0
自动驾驶开放平台
apollo::cyber::base::ThreadPool类 参考

#include <thread_pool.h>

apollo::cyber::base::ThreadPool 的协作图:

Public 成员函数

 ThreadPool (std::size_t thread_num, std::size_t max_task_num=1000)
 
template<typename F , typename... Args>
auto Enqueue (F &&f, Args &&... args) -> std::future< typename std::result_of< F(Args...)>::type >
 
 ~ThreadPool ()
 

详细描述

在文件 thread_pool.h36 行定义.

构造及析构函数说明

◆ ThreadPool()

apollo::cyber::base::ThreadPool::ThreadPool ( std::size_t  thread_num,
std::size_t  max_task_num = 1000 
)
inlineexplicit

在文件 thread_pool.h52 行定义.

53 : stop_(false) {
54 if (!task_queue_.Init(max_task_num, new BlockWaitStrategy())) {
55 throw std::runtime_error("Task queue init failed.");
56 }
57 workers_.reserve(threads);
58 for (size_t i = 0; i < threads; ++i) {
59 workers_.emplace_back([this] {
60 while (!stop_) {
61 std::function<void()> task;
62 if (task_queue_.WaitDequeue(&task)) {
63 task();
64 }
65 }
66 });
67 }
68}

◆ ~ThreadPool()

apollo::cyber::base::ThreadPool::~ThreadPool ( )
inline

在文件 thread_pool.h90 行定义.

90 {
91 if (stop_.exchange(true)) {
92 return;
93 }
94 task_queue_.BreakAllWait();
95 for (std::thread& worker : workers_) {
96 worker.join();
97 }
98}

成员函数说明

◆ Enqueue()

template<typename F , typename... Args>
auto apollo::cyber::base::ThreadPool::Enqueue ( F &&  f,
Args &&...  args 
) -> std::future<typename std::result_of<F(Args...)>::type>

在文件 thread_pool.h72 行定义.

73 {
74 using return_type = typename std::result_of<F(Args...)>::type;
75
76 auto task = std::make_shared<std::packaged_task<return_type()>>(
77 std::bind(std::forward<F>(f), std::forward<Args>(args)...));
78
79 std::future<return_type> res = task->get_future();
80
81 // don't allow enqueueing after stopping the pool
82 if (stop_) {
83 return std::future<return_type>();
84 }
85 task_queue_.Enqueue([task]() { (*task)(); });
86 return res;
87};
double f
bool Enqueue(const T &element)

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