|
TEC
A lightweight C++ library enabling safe, efficient execution in multithreaded and concurrent systems.
|
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>
Public Member Functions | |
| SocketThreadPool (size_t buffer_size, size_t num_threads) | |
| Constructs a socket-oriented thread pool with per-thread buffers. | |
| char * | get_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< TaskFunc > | tasks_ |
| 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< bool > | stop_ |
| Atomic flag used to signal all worker threads to terminate. | |
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.
Constructs a socket-oriented thread pool with per-thread buffers.
| buffer_size | Size in bytes of each worker's private buffer |
| num_threads | Number 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.
|
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.
Returns a pointer to the buffer belonging to the worker at index idx
| idx | Worker index (any integral value is accepted) |
Uses modulo arithmetic so any large idx value maps safely into the valid range. Intended usage: pool.get_buffer(pool.get_next_worker_index())
Returns the size (in bytes) of each per-thread buffer.
|
inline |
Atomically selects the next worker index in round-robin fashion.
Uses relaxed memory order — sufficient for simple round-robin distribution when no additional synchronization is required between consecutive calls.
Typical pattern: