CppELib 1.7.0
Loading...
Searching...
No Matches
OSWrapper::ThreadPool Class Reference

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::UncaughtExceptionHandlergetUncaughtExceptionHandler () const
 Get the UncaughtExceptionHandler for this ThreadPool.
 
const char * getThreadName () const
 Get the name of threads.
 

Static Public Member Functions

static ThreadPoolcreate (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.
 

Detailed Description

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.

Member Function Documentation

◆ create()

ThreadPool * OSWrapper::ThreadPool::create ( std::size_t  maxThreads,
std::size_t  stackSize = 0U,
int  defaultPriority = Thread::getNormalPriority(),
const char *  threadName = "" 
)
static

Create a ThreadPool object.

Parameters
maxThreadsNumber of max threads that can execute concurrently
stackSizeStack size of threads. If zero then sets default stack size of RTOS.
defaultPriorityFirst, 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.
threadNameName of threads. Threads are named the same name.
Returns
If this method succeeds then returns a pointer of ThreadPool object, else returns null pointer

◆ destroy()

void OSWrapper::ThreadPool::destroy ( ThreadPool p)
static

Destroy a ThreadPool object.

This method waits for all of the asynchronous tasks finished and destroys all of the threads.

Parameters
pPointer of ThreadPool object created by ThreadPool::create()
Note
If p is null pointer, do nothing.

◆ getThreadName()

const char * OSWrapper::ThreadPool::getThreadName ( ) const

Get the name of threads.

Returns
The name of threads

◆ getUncaughtExceptionHandler()

Thread::UncaughtExceptionHandler * OSWrapper::ThreadPool::getUncaughtExceptionHandler ( ) const

Get the UncaughtExceptionHandler for this ThreadPool.

Returns
The UncaughtExceptionHandler object

◆ setUncaughtExceptionHandler()

void OSWrapper::ThreadPool::setUncaughtExceptionHandler ( Thread::UncaughtExceptionHandler handler)

Set the UncaughtExceptionHandler for this ThreadPool.

Parameters
handlerPointer of UncaughtExceptionHandler object

◆ start()

Error OSWrapper::ThreadPool::start ( Runnable task,
WaitGuard waiter = 0,
int  priority = Thread::INHERIT_PRIORITY 
)

Start an asynchronous task.

Parameters
taskAsynchronous task
waiterPointer 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.
priorityThread priority of the asynchronous task
Return values
OKSuccess. A free thread has been allocated and started
InvalidParametertask is null pointer
CalledByNonThreadCalled from non thread context (interrupt handler, timer, etc)
Note
Same as timedStart(task, Timeout::FOREVER, waiter, priority)

◆ timedStart()

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.

Parameters
taskAsynchronous task
tmoutThe limited time
waiterPointer 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.
priorityThread priority of the asynchronous task
Return values
OKSuccess. A free thread has been allocated and started
InvalidParametertask is null pointer
TimedOutThe limited time was elapsed
CalledByNonThreadCalled from non thread context (interrupt handler, timer, etc)
Note
If tmout is Timeout::POLLING then this method does not block.
If tmout is Timeout::FOREVER then this method waits forever until a free thread is allocated.

◆ tryStart()

Error OSWrapper::ThreadPool::tryStart ( Runnable task,
WaitGuard waiter = 0,
int  priority = Thread::INHERIT_PRIORITY 
)

Try to start an asynchronous task without blocking.

Parameters
taskAsynchronous task
waiterPointer 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.
priorityThread priority of the asynchronous task
Return values
OKSuccess. A free thread has been allocated and started
InvalidParametertask is null pointer
TimedOutFailed to allocate a free thread
Note
Same as timedStart(task, Timeout::POLLING, waiter, priority)

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