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

#include <ThreadPool.h>

Inheritance diagram for sptk::ThreadPool:
sptk::ThreadEvent sptk::TCPServer sptk::WSListener

Public Member Functions

 ThreadPool (uint32_t threadLimit, std::chrono::milliseconds threadIdleSeconds, const String &threadName, LogEngine *logEngine)
 
virtual void execute (const SRunable &task)
 
void threadEvent (Thread *thread, ThreadEvent::Type eventType, SRunable runable) override
 
virtual void stop ()
 
size_t size () const
 
- Public Member Functions inherited from sptk::ThreadEvent
virtual void threadEvent (Thread *thread, Type eventType, SRunable runable)=0
 Thread event callback function. More...
 
virtual ~ThreadEvent ()=default
 Destructor.
 

Additional Inherited Members

- Public Types inherited from sptk::ThreadEvent
enum class  Type : uint8_t {
  THREAD_STARTED , THREAD_FINISHED , RUNABLE_STARTED , RUNABLE_FINISHED ,
  IDLE_TIMEOUT
}
 Thread event type. More...
 

Detailed Description

Controls creation and execution of the threads.

When a thread is requested from the thread pool, it ether creates a new thread or returns one from the thread pool. If a thread is idle for the period longer than defined in constructor, it's automatically terminated.

Constructor & Destructor Documentation

◆ ThreadPool()

sptk::ThreadPool::ThreadPool ( uint32_t  threadLimit,
std::chrono::milliseconds  threadIdleSeconds,
const String threadName,
LogEngine logEngine 
)

Constructor

Parameters
threadLimitMaximum number of threads in this pool
threadIdleSecondsMaximum period of inactivity (seconds) for thread in the pool before thread is terminated
threadNameThread pool own threadName

Member Function Documentation

◆ execute()

virtual void sptk::ThreadPool::execute ( const SRunable &  task)
virtual

Executes task

Parameters
taskTask to execute

◆ size()

size_t sptk::ThreadPool::size ( ) const

Number of active threads in the pool

◆ stop()

virtual void sptk::ThreadPool::stop ( )
virtual

Sends terminate() message to all worker threads, and sets shutdown state

After thread pool is stopped, it no longer accepts tasks for execution.

Reimplemented in sptk::TCPServer.

◆ threadEvent()

void sptk::ThreadPool::threadEvent ( Thread thread,
ThreadEvent::Type  eventType,
SRunable  runable 
)
overridevirtual

Thread event callback function

Receives events that occur in the threads

Parameters
threadThread where event occured
eventTypeThread event type
runableRelated runable (if any)

Implements sptk::ThreadEvent.


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

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