TEC
A lightweight C++ library enabling safe, efficient execution in multithreaded and concurrent systems.
Loading...
Searching...
No Matches
tec::SocketThreadPool Class Reference

Specialized thread pool that maintains one pre-allocated fixed-size buffer per worker thread — intended mainly for network/socket I/O patterns. More...

#include <tec_socket_thread_pool.hpp>

Inheritance diagram for tec::SocketThreadPool:
tec::ThreadPool

Public Member Functions

 SocketThreadPool (size_t buffer_size, size_t num_threads)
 Constructs a socket-oriented thread pool with per-thread buffers.
 
charget_buffer (size_t idx) const
 Returns a pointer to the buffer belonging to the worker at index idx
 
constexpr size_t get_buffer_size ()
 Returns the size (in bytes) of each per-thread buffer.
 
size_t get_next_worker_index ()
 Atomically selects the next worker index in round-robin fashion.
 
virtual ~SocketThreadPool ()
 Destructor – releases all per-thread buffers.
 
- Public Member Functions inherited from tec::ThreadPool
 ThreadPool (size_t num_threads)
 Constructs a thread pool with the specified number of worker threads.
 
constexpr size_t get_num_threads ()
 Returns the number of worker threads in this pool.
 
virtual ~ThreadPool ()
 Destructor – gracefully shuts down the thread pool.
 
template<class F >
void enqueue (F &&task)
 Enqueues a new task to be executed by one of the worker threads.
 

Additional Inherited Members

- Public Types inherited from tec::ThreadPool
using TaskFunc = std::function< void()>
 Type alias for the task function objects stored and executed by the pool.
 
- Protected Attributes inherited from tec::ThreadPool
size_t num_threads_
 Number of worker threads in the pool (set during construction)
 
std::vector< std::thread > workers_
 Container holding all worker std::thread objects.
 
std::queue< TaskFunctasks_
 Thread-safe queue of pending tasks (protected by queue_mutex_)
 
std::mutex queue_mutex_
 Mutex protecting access to the tasks_ queue.
 
std::condition_variable condition_
 Condition variable used to wake up sleeping worker threads when tasks arrive.
 
std::atomic< boolstop_
 Atomic flag used to signal all worker threads to terminate.
 

Detailed Description

Specialized thread pool that maintains one pre-allocated fixed-size buffer per worker thread — intended mainly for network/socket I/O patterns.

Each worker thread gets its own dedicated buffer that lives for the entire lifetime of the pool. This avoids frequent small allocations during high-frequency socket receive/send operations and reduces pressure on the general-purpose allocator.

Buffer access is index-based and uses simple round-robin distribution via an atomic counter.

Note
This class does not own sockets or manage connections — it only provides per-thread scratch buffers and inherits task scheduling from ThreadPool.

Constructor & Destructor Documentation

◆ SocketThreadPool()

tec::SocketThreadPool::SocketThreadPool ( size_t  buffer_size,
size_t  num_threads 
)
inlineexplicit

Constructs a socket-oriented thread pool with per-thread buffers.

Parameters
buffer_sizeSize in bytes of each worker's private buffer
num_threadsNumber of worker threads (passed to base ThreadPool)

Allocates exactly num_threads buffers of size buffer_size. Buffers are allocated with plain new[] and must be POD-compatible.

◆ ~SocketThreadPool()

virtual tec::SocketThreadPool::~SocketThreadPool ( )
inlinevirtual

Destructor – releases all per-thread buffers.

Deletes every buffer previously allocated with new[]. Base class (ThreadPool) destructor is called afterwards and joins all threads.

Member Function Documentation

◆ get_buffer()

char * tec::SocketThreadPool::get_buffer ( size_t  idx) const
inline

Returns a pointer to the buffer belonging to the worker at index idx

Parameters
idxWorker index (any integral value is accepted)
Returns
Pointer to the beginning of the corresponding buffer

Uses modulo arithmetic so any large idx value maps safely into the valid range. Intended usage: pool.get_buffer(pool.get_next_worker_index())

◆ get_buffer_size()

constexpr size_t tec::SocketThreadPool::get_buffer_size ( )
inlineconstexpr

Returns the size (in bytes) of each per-thread buffer.

Returns
Buffer size passed at construction

◆ get_next_worker_index()

size_t tec::SocketThreadPool::get_next_worker_index ( )
inline

Atomically selects the next worker index in round-robin fashion.

Returns
Index (0 … num_threads-1) of the next worker to be used

Uses relaxed memory order — sufficient for simple round-robin distribution when no additional synchronization is required between consecutive calls.

Typical pattern:

size_t idx = pool.get_next_worker_index();
char* buf = pool.get_buffer(idx);
// use buf for this socket operation...

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