R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
server::ThreadPool Class Reference

Concrete implementation of IThreadPool. More...

#include <ThreadPool.hpp>

Inheritance diagram for server::ThreadPool:
Inheritance graph
Collaboration diagram for server::ThreadPool:
Collaboration graph

Public Member Functions

 ThreadPool (size_t threadCount)
 
 ~ThreadPool () override
 
void enqueue (Task task) override
 Enqueue a task to be executed by the thread pool.
 
void start () override
 Start all threads in the pool.
 
void stop () override
 Stop all threads and clean up resources.
 
size_t size () const override
 Get the number of threads in the pool.
 
- Public Member Functions inherited from server::IThreadPool
virtual ~IThreadPool ()=default
 

Private Member Functions

void _workerLoop (std::stop_token stopToken)
 Worker thread main loop Continuously pulls tasks from the queue and executes them.
 

Private Attributes

size_t _threadCount
 
std::vector< std::jthread > _workers
 
ThreadSafeQueue< Task_taskQueue
 

Additional Inherited Members

- Public Types inherited from server::IThreadPool
using Task = std::function< void()>
 

Detailed Description

Concrete implementation of IThreadPool.

Manages a fixed number of worker threads executing tasks concurrently. Uses a thread-safe queue to distribute tasks among workers.

Definition at line 23 of file ThreadPool.hpp.

Constructor & Destructor Documentation

◆ ThreadPool()

server::ThreadPool::ThreadPool ( size_t  threadCount)
explicit

Definition at line 13 of file ThreadPool.cpp.

◆ ~ThreadPool()

server::ThreadPool::~ThreadPool ( )
override

Definition at line 15 of file ThreadPool.cpp.

References LOG_ERROR, and stop().

Here is the call graph for this function:

Member Function Documentation

◆ _workerLoop()

void server::ThreadPool::_workerLoop ( std::stop_token  stopToken)
private

Worker thread main loop Continuously pulls tasks from the queue and executes them.

Parameters
stopTokenToken to check for stop requests

Definition at line 86 of file ThreadPool.cpp.

References _taskQueue, LOG_ERROR, and ThreadSafeQueue< T >::pop().

Referenced by start().

Here is the call graph for this function:

◆ enqueue()

void server::ThreadPool::enqueue ( Task  task)
overridevirtual

Enqueue a task to be executed by the thread pool.

Parameters
taskTask function

Implements server::IThreadPool.

Definition at line 68 of file ThreadPool.cpp.

References _taskQueue, _workers, LOG_WARNING, and ThreadSafeQueue< T >::push().

Here is the call graph for this function:

◆ size()

size_t server::ThreadPool::size ( ) const
overridevirtual

Get the number of threads in the pool.

Returns
number of threads

Implements server::IThreadPool.

Definition at line 82 of file ThreadPool.cpp.

References _threadCount.

◆ start()

void server::ThreadPool::start ( )
overridevirtual

Start all threads in the pool.

Implements server::IThreadPool.

Definition at line 24 of file ThreadPool.cpp.

References _threadCount, _workerLoop(), _workers, LOG_DEBUG, LOG_INFO, and LOG_WARNING.

Here is the call graph for this function:

◆ stop()

void server::ThreadPool::stop ( )
overridevirtual

Stop all threads and clean up resources.

Implements server::IThreadPool.

Definition at line 45 of file ThreadPool.cpp.

References _taskQueue, _threadCount, _workers, LOG_INFO, and ThreadSafeQueue< T >::push().

Referenced by ~ThreadPool().

Here is the call graph for this function:

Member Data Documentation

◆ _taskQueue

ThreadSafeQueue<Task> server::ThreadPool::_taskQueue
private

Definition at line 44 of file ThreadPool.hpp.

Referenced by _workerLoop(), enqueue(), and stop().

◆ _threadCount

size_t server::ThreadPool::_threadCount
private

Definition at line 42 of file ThreadPool.hpp.

Referenced by size(), start(), and stop().

◆ _workers

std::vector<std::jthread> server::ThreadPool::_workers
private

Definition at line 43 of file ThreadPool.hpp.

Referenced by enqueue(), start(), and stop().


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