3 #include <SimpleThreadPool.h>
4 #include <HeliosException.h>
12 template <
typename ... TaskArgs>
37 for (std::size_t i = 0; i < this->
pool_size; ++i){
55 for(
size_t i = 0 ; i < this->
pool_size ; i++){
66 template <
typename Task>
68 boost::unique_lock<boost::mutex> lock(this->
mutex_);
72 this->
cond_.wait(lock);
90 boost::function<
void(TaskArgs ...)>(task),
104 template <
typename Task>
106 boost::unique_lock<boost::mutex> lock(this->
mutex_);
128 boost::function<
void(TaskArgs ...)>(task),
145 boost::function<
void(TaskArgs ...)> &task,
146 int const resourceIdx
153 catch (
const std::exception &e) {
154 std::stringstream ss;
155 ss <<
"ResThreadPool::wrap_res_task EXCEPTION: " << e.what();
156 logging::WARN(ss.str());
160 boost::unique_lock<boost::mutex> lock(this->
mutex_);
162 resourceSetAvailable[resourceIdx] =
true;
164 this->
cond_.notify_one();
172 boost::function<
void(TaskArgs ...)> &task
175 "ResThreadPool::do_task MUST NOT be invoked.\n"
176 "Please, avoid this call or override implementation"
189 boost::function<
void(TaskArgs ...)> &task,
190 int const resourceIdx
Base class for Helios exceptions.
Definition: HeliosException.h:12
Abstract class extending basic thread pool implementation to provide a basis layer to handle thread a...
Definition: ResThreadPool.h:13
void run_res_task(Task task)
Run a task with associated resources when there is an available thread for it.
Definition: ResThreadPool.h:67
bool try_run_res_task(Task task)
Run a task with associated resources. If there is not even a single available thread,...
Definition: ResThreadPool.h:105
virtual int getAvailableResourceSetIndex() const
Obtain the index of an available resource set.
Definition: ResThreadPool.h:54
bool * resourceSetAvailable
Array of flags specifying availability of resource sets.
Definition: ResThreadPool.h:21
void do_task(boost::function< void(TaskArgs ...)> &task) override
Throw exception when calling non resource do_task.
Definition: ResThreadPool.h:171
virtual void wrap_res_task(boost::function< void(TaskArgs ...)> &task, int const resourceIdx)
Wrap a task so that available threads count can be increased once provided task has been completed.
Definition: ResThreadPool.h:144
virtual void do_res_task(boost::function< void(TaskArgs ...)> &task, int const resourceIdx)=0
Invoke task with associated resources with corresponding arguments.
ResThreadPool(std::size_t const _pool_size)
Resource thread pool constructor.
Definition: ResThreadPool.h:30
Abstract class providing implementation of a simple thread pool which assigns tasks to threads.
Definition: SimpleThreadPool.h:12
std::size_t pool_size
Size of thread pool (number of threads)
Definition: ThreadPool.h:47
boost::mutex mutex_
Mutex to handle concurrent tasks.
Definition: SimpleThreadPool.h:26
boost::asio::io_service io_service_
Instance of boost input/output service for asynchronous data processing.
Definition: ThreadPool.h:36
boost::condition_variable cond_
Condition variable to handle tasks dispatching depending on available threads.
Definition: SimpleThreadPool.h:31
std::size_t available_
Number of available threads, those which are not currently performing a task.
Definition: SimpleThreadPool.h:22