3 #include <SimpleThreadPool.h>
4 #include <HeliosException.h>
6 #include <unordered_map>
21 template <
typename MDType,
typename ... TaskArgs>
42 template <
typename Task>
45 boost::unique_lock<boost::mutex> lock(this->
mutex_);
58 boost::function<
void(TaskArgs ...)>(task),
73 template <
typename Task>
76 boost::unique_lock<boost::mutex> lock(this->
mutex_);
95 boost::function<
void(TaskArgs ...)>(task),
106 boost::unique_lock<boost::mutex> lock(this->
mutex_);
108 this->
cond_.wait(lock);
125 boost::function<
void(TaskArgs ...)> &task,
133 catch (
const std::exception &e) {
134 std::stringstream ss;
135 ss <<
"MDThreadPool::wrap_md_task EXCEPTION: " << e.what();
136 logging::WARN(ss.str());
140 boost::unique_lock<boost::mutex> lock(this->
mutex_);
144 this->
cond_.notify_one();
153 boost::function<
void(TaskArgs ...)> &task
156 "MDThreadPool::do_task MUST NOT be invoked.\n"
157 "Please, avoid this call or override implementation"
167 boost::function<
void(TaskArgs ...)> &task,
185 boost::unique_lock<boost::mutex> lock(this->
mutex_);
202 boost::unique_lock<boost::mutex> lock(this->
mutex_);
222 boost::unique_lock<boost::mutex> lock(this->
mutex_);
Base class for Helios exceptions.
Definition: HeliosException.h:12
Abstract class extending basic thread pool implementation to provide a basis layer to handle tasks wi...
Definition: MDThreadPool.h:22
virtual size_t safeGetPendingTasks()
Obtain the number of pending tasks to be computed in a thread safe way. It is, with proper handling o...
Definition: MDThreadPool.h:184
void join() override
Lock until all pending threads have finished.
Definition: MDThreadPool.h:105
virtual void subtractPendingTasks(size_t const amount)
Subtract specified amount of pending tasks.
Definition: MDThreadPool.h:213
virtual void increasePendingTasks()
Unitary increase the number of pending tasks to be computed.
Definition: MDThreadPool.h:229
virtual void setPendingTasks(size_t const pendingTasks)
Set the number of pending tasks to be computed.
Definition: MDThreadPool.h:194
void run_md_task(Task task, MDType *data)
Run a task with associated data without considering limit for max pending tasks.
Definition: MDThreadPool.h:43
virtual void wrap_md_task(boost::function< void(TaskArgs ...)> &task, MDType *data)
Wrap a data task so that pending tasks count can be increased once provided data task has been comple...
Definition: MDThreadPool.h:124
virtual void safeSetPendingTasks(size_t const pendingTasks)
Set the number of pending tasks to be computed in a thread safe way. It is, with proper handling of c...
Definition: MDThreadPool.h:201
virtual void do_md_task(boost::function< void(TaskArgs ...)> &task, MDType *data)=0
Invoke task with associated data.
void do_task(boost::function< void(TaskArgs ...)> &task) override
Throw exception when calling non data do_task.
Definition: MDThreadPool.h:152
virtual void safeSubtractPendingTasks(size_t const amount)
Subtract pending tasks in a thread safe way. It is, with proper handling of concurrency.
Definition: MDThreadPool.h:221
virtual void decreasePendingTasks()
Unitary decrease the number of pending tasks to be computed.
Definition: MDThreadPool.h:233
MDThreadPool(std::size_t const _pool_size)
Multiple data thread pool constructor.
Definition: MDThreadPool.h:30
virtual size_t getPendingTasks()
Obtain the number of pending tasks to be computed.
Definition: MDThreadPool.h:178
bool try_run_md_task(Task task, MDType *data)
Try to run a task with associated data. If the max limit of pending tasks has not been reached,...
Definition: MDThreadPool.h:74
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
boost::thread_group threads_
Group of threads.
Definition: ThreadPool.h:51
boost::asio::executor_work_guard< boost::asio::io_service::executor_type > work_
Instance of work guard to report the io service when it has pending tasks.
Definition: ThreadPool.h:43