STTNet
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Public Member Functions | List of all members
stt::system::WorkerPool Class Reference

Fixed-size worker thread pool. More...

#include <sttnet_English.h>

Public Member Functions

 WorkerPool (size_t n)
 Constructor, creates a specified number of worker threads. More...
 
 ~WorkerPool ()
 destructor More...
 
void submit (Task task)
 Submit a task to the thread pool. More...
 
void stop ()
 Stop the thread pool and wait for all threads to exit. More...
 

Detailed Description

Fixed-size worker thread pool.

WorkerPool Internally, it maintains a task queue and several worker threads. Each worker thread takes a task from the queue and executes it in a loop.

characteristic

Thread safety instructions

Usage example

WorkerPool pool(4);
pool.submit([] {
// perform tasks
});
pool.stop();

Constructor & Destructor Documentation

stt::system::WorkerPool::WorkerPool ( size_t  n)
inlineexplicit

Constructor, creates a specified number of worker threads.

Parameters
nNumber of worker threads

Once constructed, all threads start immediately and enter a waiting state.

stt::system::WorkerPool::~WorkerPool ( )
inline

destructor

stop() is automatically called during destruction. Ensure all threads exit and are correctly joined.

Member Function Documentation

void stt::system::WorkerPool::stop ( )
inline

Stop the thread pool and wait for all threads to exit.

After calling:

  • No new tasks should be submitted.
  • Submitted but not yet executed tasks will be completed.
  • All worker threads will exit and join.

This function can be called repeatedly safely.

void stt::system::WorkerPool::submit ( Task  task)
inline

Submit a task to the thread pool.

Parameters
taskCallable objects, with the function signature void()

The task is placed in an internal queue and executed asynchronously by a worker thread.


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