TEC
A lightweight C++ library enabling safe, efficient execution in multithreaded and concurrent systems.
Loading...
Searching...
No Matches
tec_socket_thread_pool.hpp
Go to the documentation of this file.
1// Time-stamp: <Last changed 2026-02-20 16:27:10 by magnolia>
2/*----------------------------------------------------------------------
3------------------------------------------------------------------------
4Copyright (c) 2020-2026 The Emacs Cat (https://github.com/olddeuteronomy/tec).
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17------------------------------------------------------------------------
18----------------------------------------------------------------------*/
26#pragma once
27
28#include <cstddef>
29#include <atomic>
30#include <vector>
31
32#include "tec/tec_def.hpp" // IWYU pragma: keep
33#include "tec/tec_trace.hpp"
35
36
37namespace tec {
38
54private:
55 size_t buffer_size_;
56 std::vector<char> buffers_;
57 std::atomic<size_t> next_worker_index_;
58
59public:
68 explicit SocketThreadPool(size_t buffer_size, size_t num_threads)
69 : ThreadPool(num_threads)
70 , buffer_size_{buffer_size}
71 , next_worker_index_{0}
72 {
73 TEC_ENTER("SocketThreadPool::SocketThreadPool");
74 //
75 // Allocate thread buffers.
76 //
77 buffers_.resize(num_threads_ * buffer_size_);
78 }
79
88 char* get_buffer(size_t idx) const {
89 // safe even if idx is huge
90 return (char*)buffers_.data() + ((idx % num_threads_) * buffer_size_);
91 }
92
97 constexpr size_t get_buffer_size() { return buffer_size_; }
98
114 return next_worker_index_.fetch_add(1, std::memory_order_relaxed)
115 % workers_.size();
116 }
117
125 }
126
127}; // class SocketThreadPool
128
129} // namespace tec
Specialized thread pool that maintains one pre-allocated fixed-size buffer per worker thread — intend...
Definition tec_socket_thread_pool.hpp:53
constexpr size_t get_buffer_size()
Returns the size (in bytes) of each per-thread buffer.
Definition tec_socket_thread_pool.hpp:97
char * get_buffer(size_t idx) const
Returns a pointer to the buffer belonging to the worker at index idx
Definition tec_socket_thread_pool.hpp:88
virtual ~SocketThreadPool()
Destructor – releases all per-thread buffers.
Definition tec_socket_thread_pool.hpp:124
SocketThreadPool(size_t buffer_size, size_t num_threads)
Constructs a socket-oriented thread pool with per-thread buffers.
Definition tec_socket_thread_pool.hpp:68
size_t get_next_worker_index()
Atomically selects the next worker index in round-robin fashion.
Definition tec_socket_thread_pool.hpp:113
Simple, non-stealing thread pool implementation using a single shared task queue.
Definition tec_thread_pool.hpp:53
size_t num_threads_
Number of worker threads in the pool (set during construction)
Definition tec_thread_pool.hpp:59
std::vector< std::thread > workers_
Container holding all worker std::thread objects.
Definition tec_thread_pool.hpp:60
#define TEC_ENTER(name)
Logs an entry message for a named context (e.g., function).
Definition tec_trace.hpp:211
Common definitions and utilities for the tec namespace.
Simple, non-stealing thread pool implementation using a single shared task queue.
Provides a thread-safe tracing utility for debugging in the tec namespace.