Horizon Official Technical Documentation
WorkerThreadPool Class Reference

#include <WorkerThreadPool.hpp>

+ Collaboration diagram for WorkerThreadPool:

Public Member Functions

 WorkerThreadPool (unsigned const thread_count=std::thread::hardware_concurrency())
 
 ~WorkerThreadPool ()
 
template<typename FunctionType >
std::future< typename std::invoke_result< FunctionType()>::type > submit (FunctionType f)
 

Private Member Functions

void worker_thread ()
 

Private Attributes

std::vector< std::thread > _threads
 
std::atomic_bool _done
 
ThreadSafeQueue< FunctionWrapper_work_queue
 

Constructor & Destructor Documentation

◆ WorkerThreadPool()

WorkerThreadPool::WorkerThreadPool ( unsigned const  thread_count = std::thread::hardware_concurrency())
inline
83 : _done(false)
84 {
85 try {
86 for (unsigned i = 0; i < thread_count; ++i)
87 _threads.push_back(std::thread(&WorkerThreadPool::worker_thread, this));
88 } catch (...) {
89 _done = true;
90 throw;
91 }
92 }
std::atomic_bool _done
Definition: WorkerThreadPool.hpp:128
std::vector< std::thread > _threads
Definition: WorkerThreadPool.hpp:127
void worker_thread()
Definition: WorkerThreadPool.hpp:116

References _done, _threads, and worker_thread().

+ Here is the call graph for this function:

◆ ~WorkerThreadPool()

WorkerThreadPool::~WorkerThreadPool ( )
inline
95 {
96 _done = true;
97
98 for (unsigned long i = 0; i < _threads.size(); i++)
99 if (_threads.at(i).joinable())
100 _threads.at(i).join();
101 }

References _done, and _threads.

Member Function Documentation

◆ submit()

template<typename FunctionType >
std::future< typename std::invoke_result< FunctionType()>::type > WorkerThreadPool::submit ( FunctionType  f)
inline
106 {
107 typedef typename std::invoke_result<FunctionType()>::type result_type;
108
109 std::packaged_task<result_type()> task(std::move(f));
110 std::future<result_type> res(task.get_future());
111 _work_queue.push(std::move(task));
112 return res;
113 }
void push(T &&new_value)
Definition: ThreadSafeQueue.hpp:90
ThreadSafeQueue< FunctionWrapper > _work_queue
Definition: WorkerThreadPool.hpp:129

References _work_queue, and ThreadSafeQueue< T >::push().

+ Here is the call graph for this function:

◆ worker_thread()

void WorkerThreadPool::worker_thread ( )
inlineprivate
117 {
118 while (!_done) {
119 std::shared_ptr<FunctionWrapper> task;
120 if ((task = _work_queue.try_pop()))
121 task->call();
122 else
123 std::this_thread::yield();
124 }
125 }
std::shared_ptr< T > try_pop()
Definition: ThreadSafeQueue.hpp:84

References _done, _work_queue, and ThreadSafeQueue< T >::try_pop().

Referenced by WorkerThreadPool().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ _done

std::atomic_bool WorkerThreadPool::_done
private

◆ _threads

std::vector<std::thread> WorkerThreadPool::_threads
private

◆ _work_queue

ThreadSafeQueue<FunctionWrapper> WorkerThreadPool::_work_queue
private

Referenced by submit(), and worker_thread().


The documentation for this class was generated from the following file: