Go to the documentation of this file.
65 #ifndef XSENS_THREADPOOL_H
66 #define XSENS_THREADPOOL_H
85 virtual bool exec() = 0;
89 virtual void onError() {}
91 unsigned int taskId()
const;
111 std::deque<std::shared_ptr<PooledTask>>
m_tasks;
128 unsigned int count();
134 void suspend(
bool wait = false) noexcept;
138 unsigned int failedCount(
unsigned int thread) const;
150 virtual unsigned int needToWaitFor();
155 void addWaitId(
unsigned int id);
167 template <
typename T>
192 template <
typename T>
A generic task implementation for the thread pool.
bool isCanceling() const
Returns true if the task has been told to cancel itself.
virtual ~ThreadPoolTask()
void suspend(bool wait=false) noexcept
Suspend execution of tasks, any currently executing tasks will run to completion, but queued tasks wi...
ThreadPool()
Construct a threadpool with a number of threads equal to the number of cores on the PC.
unsigned int completedCount(unsigned int thread) const
Return the number of tasks completed by the given thread.
XsThreadId taskThreadId(TaskId id)
Find an XsThread with the specified id.
bool doesTaskExist(TaskId id)
Check if a task with the supplied id exists.
std::map< TaskId, std::shared_ptr< PooledTask > > m_tasksSearch
bool exec() override
deletes the managed object
A class that contains a thread that runs in a ThreadPool to execute tasks.
std::set< PooledThread * > m_threads
virtual unsigned int needToWaitFor()
This function gets called by PooledThread when the exec() function returns false to determine if we s...
void setPoolSize(unsigned int poolsize)
Set the number of threads in the ThreadPool.
A class that allows multiple-dependency waiting.
volatile std::atomic_bool m_terminating
Task that will delete its object parameter.
static ThreadPool::TaskId deleteThreaded(T *obj)
Delete obj in the ThreadPool.
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.
void reportTaskPaused(std::shared_ptr< PooledTask >)
Called by PooledThread to notify the ThreadPool that its running task has to wait for something.
unsigned int TaskId
A type definition of a task ID.
std::map< TaskId, std::shared_ptr< PooledTask > > m_executing
unsigned int count()
Return the number of tasks that are currently in the queue or being executed.
std::shared_ptr< PooledTask > getNextTask()
Return the next task that should be run and mark it as executing.
int processorCount()
Returns the number of processor cores in the current system.
static void destroy()
Destroy the global thread pool object.
std::list< unsigned int > m_waitList
ThreadPoolObjectDeleter(T *obj)
Constructor, creates the task to delete object a obj, but doesn't schedule it.
TaskId addTask(ThreadPoolTask *task, TaskId afterId=0)
Add a task to be executed by the threadpool.
std::deque< std::shared_ptr< PooledTask > > m_tasks
void resume()
Resume execution of tasks.
unsigned int taskId() const
Returns the task ID of the task or 0 if it doesn't have a proper ID (yet)
void waitForCompletion(TaskId id)
Wait for the task with the given ID to complete.
unsigned int failedCount(unsigned int thread) const
Return the number of tasks that failed to execute in the given thread.
static ThreadPool * instance() noexcept
Return the global thread pool object, it will be created if it did not yet exist.
static void setPool(ThreadPool *pool)
Set the threadpool to use.
std::shared_ptr< PooledTask > findTask(TaskId id)
Find a task with the supplied id.
This class creates and maintains a number of threads that can execute finite-length tasks.
void reportTaskComplete(std::shared_ptr< PooledTask >)
Called by PooledThread to notify the ThreadPool that a task was completed.
A class that contains a task and some administrative stuff.
unsigned int poolSize() const
Return the number of threads in the pool.
~ThreadPool()
Destructor, clears any pending tasks and destroys the threads.
unsigned int executedCount(unsigned int thread) const
Return the number of tasks executed (including paused) by the given thread.
std::map< TaskId, std::shared_ptr< PooledTask > > m_delaying