TEC
A lightweight C++ library enabling safe, efficient execution in multithreaded and concurrent systems.
Loading...
Searching...
No Matches
tec_thread_pool.hpp
Go to the documentation of this file.
1// Time-stamp: <Last changed 2026-02-20 16:04:44 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 <thread>
30#include <atomic>
31#include <functional>
32#include <mutex>
33#include <condition_variable>
34#include <vector>
35#include <queue>
36
37#include "tec/tec_def.hpp" // IWYU pragma: keep
38#include "tec/tec_trace.hpp"
39
40
41namespace tec {
42
54public:
56 using TaskFunc = std::function<void()>;
57
58protected:
59 size_t num_threads_;
60 std::vector<std::thread> workers_;
61 std::queue<TaskFunc> tasks_;
62 std::mutex queue_mutex_;
63 std::condition_variable condition_;
64 std::atomic<bool> stop_;
65
66public:
74 explicit ThreadPool(size_t num_threads)
75 : num_threads_(num_threads)
76 , stop_(false)
77 {
78 TEC_ENTER("ThreadPool::ThreadPool");
79 //
80 // Register thread workers.
81 //
82 for (size_t i = 0; i < num_threads; ++i) {
83 workers_.emplace_back([this] {
84 while (true) {
85 TaskFunc task;
86 {
87 std::unique_lock<std::mutex> lock(queue_mutex_);
88 condition_.wait(lock, [this] {
89 return stop_ || !tasks_.empty();
90 });
91
92 if (stop_ && tasks_.empty())
93 return;
94
95 task = std::move(tasks_.front());
96 tasks_.pop();
97 }
98
99 if (task) {
100 task();
101 }
102 }
103 });
104 }
105 TEC_TRACE("Thread pool created with {} workers.", num_threads_);
106 }
107
112 constexpr size_t get_num_threads() {
113 return num_threads_;
114 }
115
128 virtual ~ThreadPool() {
129 {
130 std::unique_lock<std::mutex> lock(queue_mutex_);
131 stop_ = true;
132 }
133 condition_.notify_all();
134 for (std::thread& worker : workers_) {
135 if (worker.joinable())
136 worker.join();
137 }
138 }
139
153 template<class F>
154 void enqueue(F&& task) {
155 {
156 std::unique_lock<std::mutex> lock(queue_mutex_);
157 tasks_.emplace(std::forward<F>(task));
158 }
159 condition_.notify_one();
160 }
161};
162
163} // namespace tec
Simple, non-stealing thread pool implementation using a single shared task queue.
Definition tec_thread_pool.hpp:53
std::function< void()> TaskFunc
Type alias for the task function objects stored and executed by the pool.
Definition tec_thread_pool.hpp:56
std::queue< TaskFunc > tasks_
Thread-safe queue of pending tasks (protected by queue_mutex_)
Definition tec_thread_pool.hpp:61
constexpr size_t get_num_threads()
Returns the number of worker threads in this pool.
Definition tec_thread_pool.hpp:112
size_t num_threads_
Number of worker threads in the pool (set during construction)
Definition tec_thread_pool.hpp:59
std::atomic< bool > stop_
Atomic flag used to signal all worker threads to terminate.
Definition tec_thread_pool.hpp:64
ThreadPool(size_t num_threads)
Constructs a thread pool with the specified number of worker threads.
Definition tec_thread_pool.hpp:74
std::condition_variable condition_
Condition variable used to wake up sleeping worker threads when tasks arrive.
Definition tec_thread_pool.hpp:63
virtual ~ThreadPool()
Destructor – gracefully shuts down the thread pool.
Definition tec_thread_pool.hpp:128
std::mutex queue_mutex_
Mutex protecting access to the tasks_ queue.
Definition tec_thread_pool.hpp:62
std::vector< std::thread > workers_
Container holding all worker std::thread objects.
Definition tec_thread_pool.hpp:60
void enqueue(F &&task)
Enqueues a new task to be executed by one of the worker threads.
Definition tec_thread_pool.hpp:154
#define TEC_ENTER(name)
Logs an entry message for a named context (e.g., function).
Definition tec_trace.hpp:211
#define TEC_TRACE(...)
Logs a formatted trace message.
Definition tec_trace.hpp:222
Common definitions and utilities for the tec namespace.
Provides a thread-safe tracing utility for debugging in the tec namespace.