Class that has a threadpool for batch-by-batch multi-threading.
More...
#include <BatchThreadPool.h>
Class that has a threadpool for batch-by-batch multi-threading.
Definition at line 36 of file BatchThreadPool.h.
◆ BatchThreadPool()
onert::backend::trix::BatchThreadPool::BatchThreadPool |
( |
size_t |
num_threads | ) |
|
Definition at line 22 of file BatchThreadPool.cc.
22 : _num_threads(num_threads), _stop_all(false)
23{
24 _worker_threads.reserve(_num_threads);
25 for (uint32_t thread_num = 0; thread_num < _num_threads; ++thread_num)
26 {
27 _worker_threads.emplace_back([this, thread_num]() { this->worker(thread_num); });
28 }
29}
◆ ~BatchThreadPool()
onert::backend::trix::BatchThreadPool::~BatchThreadPool |
( |
| ) |
|
Definition at line 52 of file BatchThreadPool.cc.
53{
54 _stop_all = true;
55 _cv_job_queue.notify_all();
56
57 for (auto &&t : _worker_threads)
58 {
59 t.join();
60 }
61}
◆ enqueueJob()
template<class F , class... Args>
std::future< typename std::result_of< F(uint32_t, Args...)>::type > onert::backend::trix::BatchThreadPool::enqueueJob |
( |
F && |
f, |
|
|
Args &&... |
args |
|
) |
| |
|
inline |
- Template Parameters
-
F | Type of the function for job |
Args | Type of arguments of job |
- Parameters
-
f | Function for job |
args | Arguments of job |
- Returns
- std::future<typename std::result_of<F(uint32_t, Args...)>::type>
Definition at line 52 of file BatchThreadPool.h.
53 {
54 if (_stop_all)
55 {
56 throw std::runtime_error("Stop all threads in BatchThreadPool");
57 }
58
59 using return_type = typename std::result_of<F(uint32_t, Args...)>::type;
60 auto job = std::make_shared<std::packaged_task<return_type(uint32_t)>>(
61 std::bind(std::forward<F>(f), std::placeholders::_1, std::forward<Args>(args)...));
62 std::future<return_type> job_result_future = job->get_future();
63 {
64
65 std::lock_guard<std::mutex> lock(_m_job_queue);
66
67
68 _job_queue.push([job](uint32_t thread_num) { (*job)(thread_num); });
69 }
70 _cv_job_queue.notify_one();
71
72 return job_result_future;
73 }
The documentation for this class was generated from the following files: