CppELib 1.7.0
|
Class of thread pool pattern. More...
#include <ThreadPool.h>
Classes | |
class | WaitGuard |
Class to wait for the asynchronous task finished. More... | |
Public Member Functions | |
Error | start (Runnable *task, WaitGuard *waiter=0, int priority=Thread::INHERIT_PRIORITY) |
Start an asynchronous task. | |
Error | tryStart (Runnable *task, WaitGuard *waiter=0, int priority=Thread::INHERIT_PRIORITY) |
Try to start an asynchronous task without blocking. | |
Error | timedStart (Runnable *task, Timeout tmout, WaitGuard *waiter=0, int priority=Thread::INHERIT_PRIORITY) |
Start an asynchronous task with Timeout. | |
void | setUncaughtExceptionHandler (Thread::UncaughtExceptionHandler *handler) |
Set the UncaughtExceptionHandler for this ThreadPool. | |
Thread::UncaughtExceptionHandler * | getUncaughtExceptionHandler () const |
Get the UncaughtExceptionHandler for this ThreadPool. | |
const char * | getThreadName () const |
Get the name of threads. | |
Static Public Member Functions | |
static ThreadPool * | create (std::size_t maxThreads, std::size_t stackSize=0U, int defaultPriority=Thread::getNormalPriority(), const char *threadName="") |
Create a ThreadPool object. | |
static void | destroy (ThreadPool *p) |
Destroy a ThreadPool object. | |
Class of thread pool pattern.
ThreadPool creates some threads to call tasks asynchronously. When asynchronous tasks are finished, used threads are reused later instead of destroyed. An asynchronous task needs to be a class that implements OSWrapper::Runnable interface. You can set a thread priority of the asynchronous task when you call start().
If you choose to use a WaitGuard when you call start(), you need to wait for the asynchronous task finished. After you check the task finished, you can take data from the task object without mutex.
If you choose not to use a WaitGuard when you call start(), you don't have to wait for the task finished. But the destruction of the task object must be later than the task finished. Otherwise the access violation may occur that the thread may access to the destroyed object.
|
static |
Create a ThreadPool object.
maxThreads | Number of max threads that can execute concurrently |
stackSize | Stack size of threads. If zero then sets default stack size of RTOS. |
defaultPriority | First, the thread priority is defaultPriority when the threads are created. Then the priority is changed by the parameter of start() when an asynchronous task is started. After the asynchronous task finishes, the priority returns to defaultPriority . |
threadName | Name of threads. Threads are named the same name. |
|
static |
Destroy a ThreadPool object.
This method waits for all of the asynchronous tasks finished and destroys all of the threads.
p | Pointer of ThreadPool object created by ThreadPool::create() |
const char * OSWrapper::ThreadPool::getThreadName | ( | ) | const |
Get the name of threads.
Thread::UncaughtExceptionHandler * OSWrapper::ThreadPool::getUncaughtExceptionHandler | ( | ) | const |
Get the UncaughtExceptionHandler for this ThreadPool.
void OSWrapper::ThreadPool::setUncaughtExceptionHandler | ( | Thread::UncaughtExceptionHandler * | handler | ) |
Set the UncaughtExceptionHandler for this ThreadPool.
handler | Pointer of UncaughtExceptionHandler object |
Error OSWrapper::ThreadPool::start | ( | Runnable * | task, |
WaitGuard * | waiter = 0 , |
||
int | priority = Thread::INHERIT_PRIORITY |
||
) |
Start an asynchronous task.
task | Asynchronous task |
waiter | Pointer of variable of WaitGuard object that state is invalid. If this method succeeds, the WaitGuard object becomes valid. If you don't want to wait for the task finished, specify null pointer or omit this. |
priority | Thread priority of the asynchronous task |
OK | Success. A free thread has been allocated and started |
InvalidParameter | task is null pointer |
CalledByNonThread | Called from non thread context (interrupt handler, timer, etc) |
Error OSWrapper::ThreadPool::timedStart | ( | Runnable * | task, |
Timeout | tmout, | ||
WaitGuard * | waiter = 0 , |
||
int | priority = Thread::INHERIT_PRIORITY |
||
) |
Start an asynchronous task with Timeout.
If all thread of this ThreadPool have already started, this method blocks the current thread for the limited time until a free thread is allocated.
task | Asynchronous task |
tmout | The limited time |
waiter | Pointer of variable of WaitGuard object that state is invalid. If this method succeeds, the WaitGuard object becomes valid. If you don't want to wait for the task finished, specify null pointer or omit this. |
priority | Thread priority of the asynchronous task |
OK | Success. A free thread has been allocated and started |
InvalidParameter | task is null pointer |
TimedOut | The limited time was elapsed |
CalledByNonThread | Called from non thread context (interrupt handler, timer, etc) |
tmout
is Timeout::POLLING then this method does not block. tmout
is Timeout::FOREVER then this method waits forever until a free thread is allocated. Error OSWrapper::ThreadPool::tryStart | ( | Runnable * | task, |
WaitGuard * | waiter = 0 , |
||
int | priority = Thread::INHERIT_PRIORITY |
||
) |
Try to start an asynchronous task without blocking.
task | Asynchronous task |
waiter | Pointer of variable of WaitGuard object that state is invalid. If this method succeeds, the WaitGuard object becomes valid. If you don't want to wait for the task finished, specify null pointer or omit this. |
priority | Thread priority of the asynchronous task |
OK | Success. A free thread has been allocated and started |
InvalidParameter | task is null pointer |
TimedOut | Failed to allocate a free thread |