#include <WorkerThread.h>
Public Member Functions | |
WorkerThread (const SThreadManager &threadManager, SynchronizedQueue< SRunable > &queue, ThreadEvent *threadEvent=nullptr, std::chrono::milliseconds maxIdleTime=std::chrono::seconds(3600)) | |
~WorkerThread () noexcept override=default | |
void | execute (const SRunable &task) |
void | terminate () override |
Public Member Functions inherited from sptk::Thread | |
Thread (const String &name, const std::shared_ptr< ThreadManager > &threadManager=nullptr) | |
virtual | ~Thread () |
virtual void | run () |
bool | running () const |
virtual void | threadFunction ()=0 |
virtual void | terminate () |
virtual void | onThreadExit () |
virtual bool | terminated () |
virtual void | join () |
Id | id () const |
const String & | name () const |
virtual bool | sleep_for (std::chrono::milliseconds interval) |
virtual bool | sleep_until (const DateTime ×tamp) |
Protected Member Functions | |
void | threadFunction () override |
Additional Inherited Members | |
Public Types inherited from sptk::Thread | |
using | Id = std::thread::id |
Worker thread for thread manager
Worker threads are created by thread manager. They are designed to read tasks from internal or external queue. Executed tasks are objects derived from Runable. If a thread event object is defined, worker thread may report events such as thread start, task start, etc. Worker thread automatically terminates if it's idle for the period longer than defined maxIdleSec (seconds).
sptk::WorkerThread::WorkerThread | ( | const SThreadManager & | threadManager, |
SynchronizedQueue< SRunable > & | queue, | ||
ThreadEvent * | threadEvent = nullptr , |
||
std::chrono::milliseconds | maxIdleTime = std::chrono::seconds(3600) |
||
) |
Constructor
If queue is NULL then worker thread uses internal task queue. Otherwise, external (shared) task queue is used. If maxIdleSec is defined and thread is idle (not executing any tasks) for a period longer than maxIdleSec then it terminates automatically.
threadManager | Thread manager |
queue | Task queue |
threadEvent | Optional thread event interface |
maxIdleTime | Maximum time the thread is idle, seconds |
|
overridedefaultnoexcept |
Destructor
void sptk::WorkerThread::execute | ( | const SRunable & | task | ) |
Execute runable task
task | Task to execute in the worker thread |
|
overridevirtual |
Requests to terminate the thread
Reimplemented from sptk::Thread.
|
overrideprotectedvirtual |
Thread function
Implements sptk::Thread.