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 40 of file BatchThreadPool.h.
◆ BatchThreadPool()
onert::backend::trix::BatchThreadPool::BatchThreadPool |
( |
size_t |
num_threads | ) |
|
Definition at line 26 of file BatchThreadPool.cc.
26 : _num_threads(num_threads), _stop_all(false)
27{
28 _worker_threads.reserve(_num_threads);
29 for (uint32_t thread_num = 0; thread_num < _num_threads; ++thread_num)
30 {
31 _worker_threads.emplace_back([this, thread_num]() { this->worker(thread_num); });
32 }
33}
◆ ~BatchThreadPool()
onert::backend::trix::BatchThreadPool::~BatchThreadPool |
( |
| ) |
|
Definition at line 56 of file BatchThreadPool.cc.
57{
58 _stop_all = true;
59 _cv_job_queue.notify_all();
60
61 for (auto &&t : _worker_threads)
62 {
63 t.join();
64 }
65}
◆ 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 56 of file BatchThreadPool.h.
57 {
58 if (_stop_all)
59 {
60 throw std::runtime_error("Stop all threads in BatchThreadPool");
61 }
62
63 using return_type = typename std::result_of<F(uint32_t, Args...)>::type;
64 auto job = std::make_shared<std::packaged_task<return_type(uint32_t)>>(
65 std::bind(std::forward<F>(f), std::placeholders::_1, std::forward<Args>(args)...));
66 std::future<return_type> job_result_future = job->get_future();
67 {
68
69 std::lock_guard<std::mutex> lock(_m_job_queue);
70
71
72 _job_queue.push([job](uint32_t thread_num) { (*job)(thread_num); });
73 }
74 _cv_job_queue.notify_one();
75
76 return job_result_future;
77 }
The documentation for this class was generated from the following files: