Helios++
Helios software for LiDAR simulations
MDThreadPool< MDType, TaskArgs > Class Template Referenceabstract

Abstract class extending basic thread pool implementation to provide a basis layer to handle tasks with associated data. More...

#include <MDThreadPool.h>

Inheritance diagram for MDThreadPool< MDType, TaskArgs >:
Collaboration diagram for MDThreadPool< MDType, TaskArgs >:

Public Member Functions

 MDThreadPool (std::size_t const _pool_size)
 Multiple data thread pool constructor. More...
 
template<typename Task >
void run_md_task (Task task, MDType *data)
 Run a task with associated data without considering limit for max pending tasks.
 
template<typename Task >
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, the task will be run asynchronously and true will be returned once it has been posted to the asynchronous execution service. If the max limit has been reached, then the task will not be posted to the asynchronous execution service and false will be returned. More...
 
void join () override
 Lock until all pending threads have finished.
 
virtual size_t getPendingTasks ()
 Obtain the number of pending tasks to be computed. More...
 
virtual size_t safeGetPendingTasks ()
 Obtain the number of pending tasks to be computed in a thread safe way. It is, with proper handling of concurrent reading. More...
 
virtual void setPendingTasks (size_t const pendingTasks)
 Set the number of pending tasks to be computed. More...
 
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 current writting. More...
 
virtual void subtractPendingTasks (size_t const amount)
 Subtract specified amount of pending tasks. More...
 
virtual void safeSubtractPendingTasks (size_t const amount)
 Subtract pending tasks in a thread safe way. It is, with proper handling of concurrency. More...
 
virtual void increasePendingTasks ()
 Unitary increase the number of pending tasks to be computed.
 
virtual void decreasePendingTasks ()
 Unitary decrease the number of pending tasks to be computed.
 
- Public Member Functions inherited from SimpleThreadPool< TaskArgs ... >
 SimpleThreadPool (std::size_t const _pool_size)
 Simple thread pool constructor. More...
 
void run_task (Task task)
 Run a task when there is an available thread for it.
 
virtual void notifyOne ()
 Notify the conditional variable so if there is a waiting thread it will wake up.
 
- Public Member Functions inherited from ThreadPool
 ThreadPool (std::size_t const _pool_size)
 Thread pool constructor. More...
 
virtual std::size_t getPoolSize () const
 Obtain the thread pool size. More...
 

Protected Member Functions

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 completed. More...
 
void do_task (boost::function< void(TaskArgs ...)> &task) override
 Throw exception when calling non data do_task. More...
 
virtual void do_md_task (boost::function< void(TaskArgs ...)> &task, MDType *data)=0
 Invoke task with associated data. More...
 
- Protected Member Functions inherited from SimpleThreadPool< TaskArgs ... >
virtual void wrap_task (boost::function< void(TaskArgs ...)> &task)
 Wrap a task so that available threads count can be increased once provided task has been completed. More...
 

Additional Inherited Members

- Protected Attributes inherited from SimpleThreadPool< TaskArgs ... >
std::size_t available_
 Number of available threads, those which are not currently performing a task.
 
boost::mutex mutex_
 Mutex to handle concurrent tasks.
 
boost::condition_variable cond_
 Condition variable to handle tasks dispatching depending on available threads.
 
boost::asio::io_service io_service_
 Instance of boost input/output service for asynchronous data processing.
 
std::size_t pool_size
 Size of thread pool (number of threads)
 
- Protected Attributes inherited from ThreadPool
boost::asio::io_service io_service_
 Instance of boost input/output service for asynchronous data processing.
 
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. More...
 
std::size_t pool_size
 Size of thread pool (number of threads)
 
boost::thread_group threads_
 Group of threads.
 

Detailed Description

template<typename MDType, typename ... TaskArgs>
class MDThreadPool< MDType, TaskArgs >

Abstract class extending basic thread pool implementation to provide a basis layer to handle tasks with associated data.

Author
Alberto M. Esmoris Pena
Version
1.0

NOTICE available variable from ThreadPool has a different usage in MDThreadPool. Instead of counting available threads to deal with tasks, it counts pending tasks to be finished

See also
ThreadPool

Constructor & Destructor Documentation

◆ MDThreadPool()

template<typename MDType , typename ... TaskArgs>
MDThreadPool< MDType, TaskArgs >::MDThreadPool ( std::size_t const  _pool_size)
inlineexplicit

Multiple data thread pool constructor.

See also
ThreadPool::ThreadPool(std::size_t const)

Member Function Documentation

◆ do_md_task()

template<typename MDType , typename ... TaskArgs>
virtual void MDThreadPool< MDType, TaskArgs >::do_md_task ( boost::function< void(TaskArgs ...)> &  task,
MDType *  data 
)
protectedpure virtual

Invoke task with associated data.

Parameters
taskTask to be invoked
dataThe data associated with the task

◆ do_task()

template<typename MDType , typename ... TaskArgs>
void MDThreadPool< MDType, TaskArgs >::do_task ( boost::function< void(TaskArgs ...)> &  task)
inlineoverrideprotectedvirtual

Throw exception when calling non data do_task.

See also
ThreadPool::do_task

Implements SimpleThreadPool< TaskArgs ... >.

◆ getPendingTasks()

template<typename MDType , typename ... TaskArgs>
virtual size_t MDThreadPool< MDType, TaskArgs >::getPendingTasks ( )
inlinevirtual

Obtain the number of pending tasks to be computed.

Returns
Number of pending tasks to be computed

◆ safeGetPendingTasks()

template<typename MDType , typename ... TaskArgs>
virtual size_t MDThreadPool< MDType, TaskArgs >::safeGetPendingTasks ( )
inlinevirtual

Obtain the number of pending tasks to be computed in a thread safe way. It is, with proper handling of concurrent reading.

Returns
Number of pending tasks to be computed

◆ safeSetPendingTasks()

template<typename MDType , typename ... TaskArgs>
virtual void MDThreadPool< MDType, TaskArgs >::safeSetPendingTasks ( size_t const  pendingTasks)
inlinevirtual

Set the number of pending tasks to be computed in a thread safe way. It is, with proper handling of current writting.

Parameters
pendingTasksNew number of pending tasks to be computed

◆ safeSubtractPendingTasks()

template<typename MDType , typename ... TaskArgs>
virtual void MDThreadPool< MDType, TaskArgs >::safeSubtractPendingTasks ( size_t const  amount)
inlinevirtual

Subtract pending tasks in a thread safe way. It is, with proper handling of concurrency.

See also
MDThreadPool::subtractPendingTasks

◆ setPendingTasks()

template<typename MDType , typename ... TaskArgs>
virtual void MDThreadPool< MDType, TaskArgs >::setPendingTasks ( size_t const  pendingTasks)
inlinevirtual

Set the number of pending tasks to be computed.

Parameters
pendingTasksNew number of pendings tasks to be computed

◆ subtractPendingTasks()

template<typename MDType , typename ... TaskArgs>
virtual void MDThreadPool< MDType, TaskArgs >::subtractPendingTasks ( size_t const  amount)
inlinevirtual

Subtract specified amount of pending tasks.

\[ p_{t+1} = p_{t} - n \]

Parameters
amountAmount of pending tasks to be subtracted

◆ try_run_md_task()

template<typename MDType , typename ... TaskArgs>
template<typename Task >
bool MDThreadPool< MDType, TaskArgs >::try_run_md_task ( Task  task,
MDType *  data 
)
inline

Try to run a task with associated data. If the max limit of pending tasks has not been reached, the task will be run asynchronously and true will be returned once it has been posted to the asynchronous execution service. If the max limit has been reached, then the task will not be posted to the asynchronous execution service and false will be returned.

Returns
True if task was posted, false otherwise

◆ wrap_md_task()

template<typename MDType , typename ... TaskArgs>
virtual void MDThreadPool< MDType, TaskArgs >::wrap_md_task ( boost::function< void(TaskArgs ...)> &  task,
MDType *  data 
)
inlineprotectedvirtual

Wrap a data task so that pending tasks count can be increased once provided data task has been completed.

Parameters
taskData task to be wrapped
dataPointer to data associated with the task. It is necessary to handle task arguments. If data must be released after computation, do it at do_md_task implementation. It is safe to do so because it will not be used later by any MDThreadPool stage

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