sptk2 logo
SPTK Home Page
Public Member Functions | Protected Member Functions | List of all members
sptk::WorkerThread Class Reference

#include <WorkerThread.h>

Inheritance diagram for sptk::WorkerThread:
sptk::Thread

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 Stringname () const
 
virtual bool sleep_for (std::chrono::milliseconds interval)
 
virtual bool sleep_until (const DateTime &timestamp)
 

Protected Member Functions

void threadFunction () override
 

Additional Inherited Members

- Public Types inherited from sptk::Thread
using Id = std::thread::id
 

Detailed Description

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).

Constructor & Destructor Documentation

◆ WorkerThread()

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.

Parameters
threadManagerThread manager
queueTask queue
threadEventOptional thread event interface
maxIdleTimeMaximum time the thread is idle, seconds

◆ ~WorkerThread()

sptk::WorkerThread::~WorkerThread ( )
overridedefaultnoexcept

Destructor

Member Function Documentation

◆ execute()

void sptk::WorkerThread::execute ( const SRunable &  task)

Execute runable task

Parameters
taskTask to execute in the worker thread

◆ terminate()

void sptk::WorkerThread::terminate ( )
overridevirtual

Requests to terminate the thread

Reimplemented from sptk::Thread.

◆ threadFunction()

void sptk::WorkerThread::threadFunction ( )
overrideprotectedvirtual

Thread function

Implements sptk::Thread.


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

Fri Oct 14 2022 09:58:47: SPTK 5.4.1