ONE - On-device Neural Engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
onert::backend::trix::BatchThreadPool Class Reference

Class that has a threadpool for batch-by-batch multi-threading. More...

#include <BatchThreadPool.h>

Public Member Functions

 BatchThreadPool (size_t num_threads)
 
 ~BatchThreadPool ()
 
template<class F , class... Args>
std::future< typename std::result_of< F(uint32_t, Args...)>::type > enqueueJob (F &&f, Args &&...args)
 

Detailed Description

Class that has a threadpool for batch-by-batch multi-threading.

Definition at line 36 of file BatchThreadPool.h.

Constructor & Destructor Documentation

◆ 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}

Member Function Documentation

◆ 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
FType of the function for job
ArgsType of arguments of job
Parameters
fFunction for job
argsArguments 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 // Push job in the assigned queue
65 std::lock_guard<std::mutex> lock(_m_job_queue);
66
67 // Push job
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: