This class creates and maintains a number of threads that can execute finite-length tasks.
More...
#include <xsens_threadpool.h>
|
typedef unsigned int | TaskId |
| A type definition of a task ID. More...
|
|
|
| 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...
|
|
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.
◆ TaskId
◆ 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.
◆ addTask()
Add a task to be executed by the threadpool.
- Parameters
-
task | The task object whose exec() function will be called |
afterId | When 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()
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 |
◆ 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()
◆ 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()
◆ 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()
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 |
◆ reportTaskComplete()
void xsens::ThreadPool::reportTaskComplete |
( |
std::shared_ptr< PooledTask > |
task | ) |
|
|
private |
◆ 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
-
task | The 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 |
( |
| ) |
|
◆ 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
-
pool | The 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
-
poolsize | When 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
-
wait | When 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()
◆ waitForCompletion()
◆ PooledThread
◆ m_delaying
◆ m_executing
◆ m_nextId
TaskId xsens::ThreadPool::m_nextId |
|
private |
◆ m_safe
Mutex xsens::ThreadPool::m_safe |
|
private |
◆ m_suspended
bool xsens::ThreadPool::m_suspended |
|
private |
◆ m_tasks
std::deque<std::shared_ptr<PooledTask> > xsens::ThreadPool::m_tasks |
|
private |
◆ m_tasksSearch
◆ m_terminating
volatile std::atomic_bool xsens::ThreadPool::m_terminating |
|
private |
◆ m_threads
The documentation for this class was generated from the following files: