Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | Friends | List of all members
xsens::ThreadPool Class Reference

This class creates and maintains a number of threads that can execute finite-length tasks. More...

#include <xsens_threadpool.h>

Public Types

typedef unsigned int TaskId
 A type definition of a task ID. More...
 

Public Member Functions

TaskId addTask (ThreadPoolTask *task, TaskId afterId=0)
 Add a task to be executed by the threadpool. More...
 
void cancelTask (TaskId id, bool wait=true) noexcept
 Remove the task with the supplied id if it exists, waits for the task to be finished. More...
 
unsigned int completedCount (unsigned int thread) const
 Return the number of tasks completed by the given thread. More...
 
unsigned int count ()
 Return the number of tasks that are currently in the queue or being executed. More...
 
bool doesTaskExist (TaskId id)
 Check if a task with the supplied id exists. More...
 
unsigned int executedCount (unsigned int thread) const
 Return the number of tasks executed (including paused) by the given thread. More...
 
unsigned int failedCount (unsigned int thread) const
 Return the number of tasks that failed to execute in the given thread. More...
 
unsigned int poolSize () const
 Return the number of threads in the pool. More...
 
void resume ()
 Resume execution of tasks. More...
 
void setPoolSize (unsigned int poolsize)
 Set the number of threads in the ThreadPool. More...
 
void suspend (bool wait=false) noexcept
 Suspend execution of tasks, any currently executing tasks will run to completion, but queued tasks will not be started. More...
 
XsThreadId taskThreadId (TaskId id)
 Find an XsThread with the specified id. More...
 
void waitForCompletion (TaskId id)
 Wait for the task with the given ID to complete. More...
 

Static Public Member Functions

static void destroy ()
 Destroy the global thread pool object. More...
 
static ThreadPoolinstance () noexcept
 Return the global thread pool object, it will be created if it did not yet exist. More...
 
static void setPool (ThreadPool *pool)
 Set the threadpool to use. More...
 

Protected Member Functions

 ThreadPool ()
 Construct a threadpool with a number of threads equal to the number of cores on the PC. More...
 
 ~ThreadPool ()
 Destructor, clears any pending tasks and destroys the threads. More...
 

Private Member Functions

std::shared_ptr< PooledTaskfindTask (TaskId id)
 Find a task with the supplied id. More...
 
std::shared_ptr< PooledTaskgetNextTask ()
 Return the next task that should be run and mark it as executing. More...
 
void reportTaskComplete (std::shared_ptr< PooledTask >)
 Called by PooledThread to notify the ThreadPool that a task was completed. More...
 
void reportTaskPaused (std::shared_ptr< PooledTask >)
 Called by PooledThread to notify the ThreadPool that its running task has to wait for something. More...
 

Private Attributes

std::map< TaskId, std::shared_ptr< PooledTask > > m_delaying
 
std::map< TaskId, std::shared_ptr< PooledTask > > m_executing
 
TaskId m_nextId
 
Mutex m_safe
 
bool m_suspended
 
std::deque< std::shared_ptr< PooledTask > > m_tasks
 
std::map< TaskId, std::shared_ptr< PooledTask > > m_tasksSearch
 
volatile std::atomic_bool m_terminating
 
std::set< PooledThread * > m_threads
 

Friends

class PooledThread
 

Detailed Description

This class creates and maintains a number of threads that can execute finite-length tasks.

Note
See the test cases for examples on how to use the class

Definition at line 99 of file xsens_threadpool.h.

Member Typedef Documentation

◆ TaskId

typedef unsigned int xsens::ThreadPool::TaskId

A type definition of a task ID.

Definition at line 102 of file xsens_threadpool.h.

Constructor & Destructor Documentation

◆ ThreadPool()

xsens::ThreadPool::ThreadPool ( )
protected

Construct a threadpool with a number of threads equal to the number of cores on the PC.

Definition at line 361 of file xsens_threadpool.cpp.

◆ ~ThreadPool()

xsens::ThreadPool::~ThreadPool ( )
protected

Destructor, clears any pending tasks and destroys the threads.

Definition at line 371 of file xsens_threadpool.cpp.

Member Function Documentation

◆ addTask()

ThreadPool::TaskId xsens::ThreadPool::addTask ( ThreadPoolTask task,
ThreadPool::TaskId  afterId = 0 
)

Add a task to be executed by the threadpool.

Parameters
taskThe task object whose exec() function will be called
afterIdWhen not 0, the task will not be started until the task with the given id has completed
Returns
The id of the task that was added or 0 if the task could not be added for some reason.

Definition at line 399 of file xsens_threadpool.cpp.

◆ cancelTask()

void xsens::ThreadPool::cancelTask ( ThreadPool::TaskId  id,
bool  wait = true 
)
noexcept

Remove the task with the supplied id if it exists, waits for the task to be finished.

Definition at line 546 of file xsens_threadpool.cpp.

◆ completedCount()

unsigned int xsens::ThreadPool::completedCount ( unsigned int  thread) const

Return the number of tasks completed by the given thread.

Definition at line 713 of file xsens_threadpool.cpp.

◆ count()

unsigned int xsens::ThreadPool::count ( )

Return the number of tasks that are currently in the queue or being executed.

Definition at line 436 of file xsens_threadpool.cpp.

◆ destroy()

void xsens::ThreadPool::destroy ( )
static

Destroy the global thread pool object.

If the pool was not created by this library, the current library will only stop using the pool, but will leave the object intact. The next call to instance() will create a new pool.

See also
instance

Definition at line 760 of file xsens_threadpool.cpp.

◆ doesTaskExist()

bool xsens::ThreadPool::doesTaskExist ( ThreadPool::TaskId  id)

Check if a task with the supplied id exists.

Definition at line 539 of file xsens_threadpool.cpp.

◆ executedCount()

unsigned int xsens::ThreadPool::executedCount ( unsigned int  thread) const

Return the number of tasks executed (including paused) by the given thread.

Definition at line 702 of file xsens_threadpool.cpp.

◆ failedCount()

unsigned int xsens::ThreadPool::failedCount ( unsigned int  thread) const

Return the number of tasks that failed to execute in the given thread.

Definition at line 724 of file xsens_threadpool.cpp.

◆ findTask()

std::shared_ptr< PooledTask > xsens::ThreadPool::findTask ( ThreadPool::TaskId  id)
private

Find a task with the supplied id.

Definition at line 504 of file xsens_threadpool.cpp.

◆ getNextTask()

std::shared_ptr< PooledTask > xsens::ThreadPool::getNextTask ( )
private

Return the next task that should be run and mark it as executing.

Definition at line 621 of file xsens_threadpool.cpp.

◆ instance()

ThreadPool * xsens::ThreadPool::instance ( )
staticnoexcept

Return the global thread pool object, it will be created if it did not yet exist.

Definition at line 737 of file xsens_threadpool.cpp.

◆ poolSize()

unsigned int xsens::ThreadPool::poolSize ( ) const

Return the number of threads in the pool.

Definition at line 497 of file xsens_threadpool.cpp.

◆ reportTaskComplete()

void xsens::ThreadPool::reportTaskComplete ( std::shared_ptr< PooledTask task)
private

Called by PooledThread to notify the ThreadPool that a task was completed.

Note
After this call, the supplied task is invalid

Definition at line 596 of file xsens_threadpool.cpp.

◆ reportTaskPaused()

void xsens::ThreadPool::reportTaskPaused ( std::shared_ptr< PooledTask task)
private

Called by PooledThread to notify the ThreadPool that its running task has to wait for something.

This function can be called when the task failed to run to completion. It will be rescheduled at the end of the queue and its exec function will be called again. To notify the threadpool, a task should return 'false' from its exec() function.

Parameters
taskThe task that is to be paused
Note
The task itself is responsible for maintaining its state between the exec calls
After calling this function the PooledThread should consider the supplied task as invalid since it's possible that it has been picked up, executed and deleted by another thread during the function return

Definition at line 644 of file xsens_threadpool.cpp.

◆ resume()

void xsens::ThreadPool::resume ( )

Resume execution of tasks.

See also
suspend

Definition at line 694 of file xsens_threadpool.cpp.

◆ setPool()

void xsens::ThreadPool::setPool ( ThreadPool pool)
static

Set the threadpool to use.

This function allows the user to supply his own ThreadPool object to be used by all subsequent operations. If another pool was created by this library, it will be destroyed.

Parameters
poolThe new threadpool object to use. If NULL is supplied, it is simply destroyed or stopped from being used.
See also
destroy

Definition at line 776 of file xsens_threadpool.cpp.

◆ setPoolSize()

void xsens::ThreadPool::setPoolSize ( unsigned int  poolsize)

Set the number of threads in the ThreadPool.

Parameters
poolsizeWhen 0 or less two threads will be created for each processor core in the system (with a minimum of 4 threads), otherwise the desired number of threads will be created

Definition at line 445 of file xsens_threadpool.cpp.

◆ suspend()

void xsens::ThreadPool::suspend ( bool  wait = false)
noexcept

Suspend execution of tasks, any currently executing tasks will run to completion, but queued tasks will not be started.

Parameters
waitWhen set to true the function waits for all threads to finish their current task
See also
resume

Definition at line 676 of file xsens_threadpool.cpp.

◆ taskThreadId()

XsThreadId xsens::ThreadPool::taskThreadId ( TaskId  id)

Find an XsThread with the specified id.

Definition at line 528 of file xsens_threadpool.cpp.

◆ waitForCompletion()

void xsens::ThreadPool::waitForCompletion ( ThreadPool::TaskId  id)

Wait for the task with the given ID to complete.

Definition at line 586 of file xsens_threadpool.cpp.

Friends And Related Function Documentation

◆ PooledThread

friend class PooledThread
friend

Definition at line 108 of file xsens_threadpool.h.

Member Data Documentation

◆ m_delaying

std::map<TaskId, std::shared_ptr<PooledTask> > xsens::ThreadPool::m_delaying
private

Definition at line 114 of file xsens_threadpool.h.

◆ m_executing

std::map<TaskId, std::shared_ptr<PooledTask> > xsens::ThreadPool::m_executing
private

Definition at line 113 of file xsens_threadpool.h.

◆ m_nextId

TaskId xsens::ThreadPool::m_nextId
private

Definition at line 116 of file xsens_threadpool.h.

◆ m_safe

Mutex xsens::ThreadPool::m_safe
private

Definition at line 115 of file xsens_threadpool.h.

◆ m_suspended

bool xsens::ThreadPool::m_suspended
private

Definition at line 117 of file xsens_threadpool.h.

◆ m_tasks

std::deque<std::shared_ptr<PooledTask> > xsens::ThreadPool::m_tasks
private

Definition at line 111 of file xsens_threadpool.h.

◆ m_tasksSearch

std::map<TaskId, std::shared_ptr<PooledTask> > xsens::ThreadPool::m_tasksSearch
private

Definition at line 112 of file xsens_threadpool.h.

◆ m_terminating

volatile std::atomic_bool xsens::ThreadPool::m_terminating
private

Definition at line 118 of file xsens_threadpool.h.

◆ m_threads

std::set<PooledThread*> xsens::ThreadPool::m_threads
private

Definition at line 110 of file xsens_threadpool.h.


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


xsens_mti_driver
Author(s):
autogenerated on Sun Sep 3 2023 02:43:23