#include <thread_pool.h>
|
| 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 () |
|
◆ ThreadPool()
apollo::cyber::base::ThreadPool::ThreadPool |
( |
std::size_t |
thread_num, |
|
|
std::size_t |
max_task_num = 1000 |
|
) |
| |
|
inlineexplicit |
在文件 thread_pool.h 第 52 行定义.
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;
63 task();
64 }
65 }
66 });
67 }
68}
bool WaitDequeue(T *element)
◆ ~ThreadPool()
apollo::cyber::base::ThreadPool::~ThreadPool |
( |
| ) |
|
|
inline |
在文件 thread_pool.h 第 90 行定义.
90 {
91 if (stop_.exchange(true)) {
92 return;
93 }
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.h 第 72 行定义.
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
82 if (stop_) {
83 return std::future<return_type>();
84 }
85 task_queue_.
Enqueue([task]() { (*task)(); });
86 return res;
87};
bool Enqueue(const T &element)
该类的文档由以下文件生成: