ONE - On-device Neural Engine
Loading...
Searching...
No Matches
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 40 of file BatchThreadPool.h.

Constructor & Destructor Documentation

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

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 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 // Push job in the assigned queue
69 std::lock_guard<std::mutex> lock(_m_job_queue);
70
71 // Push job
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: