2
2
3
3
/* *
4
4
* @file RouterThreadPool.h
5
- * @brief A generic thread pool for parallel task execution.
6
- *
7
- * This class manages a pool of threads and allows tasks to be executed in parallel.
5
+ * @brief A generic thread pool for parallel task execution
8
6
*/
9
7
10
8
#include < thread>
19
17
#include " vtr_log.h"
20
18
#include " vtr_time.h"
21
19
22
- class RouterThreadPool {
20
+ namespace vtr {
21
+
22
+ class thread_pool {
23
23
private:
24
24
struct ThreadData {
25
25
std::thread thread;
@@ -42,16 +42,16 @@ class RouterThreadPool {
42
42
std::condition_variable completion_cv;
43
43
44
44
public:
45
- RouterThreadPool (size_t thread_count) {
45
+ thread_pool (size_t thread_count) {
46
46
// VTR_LOG("Creating thread pool with %zu threads\n", thread_count);
47
47
threads.reserve (thread_count);
48
-
48
+
49
49
for (size_t i = 0 ; i < thread_count; i++) {
50
50
auto thread_data = std::make_unique<ThreadData>(i);
51
-
52
- thread_data->thread = std::thread ([this , td = thread_data.get ()]() {
51
+
52
+ thread_data->thread = std::thread ([td = thread_data.get ()]() {
53
53
// VTR_LOG("Thread %zu started\n", td->thread_id);
54
-
54
+
55
55
while (true ) {
56
56
std::function<void ()> task;
57
57
{
@@ -93,14 +93,14 @@ class RouterThreadPool {
93
93
template <typename F>
94
94
void schedule_work (F&& f) {
95
95
active_tasks++;
96
-
96
+
97
97
// Round-robin thread assignment
98
98
size_t thread_idx = (next_thread++) % threads.size ();
99
99
auto thread_data = threads[thread_idx].get ();
100
100
size_t task_id = ++total_tasks_queued;
101
-
101
+
102
102
// VTR_LOG("Scheduling task %zu to thread %zu\n", task_id, thread_data->thread_id);
103
-
103
+
104
104
// Wrap the work with task completion tracking
105
105
auto task = [this , f = std::forward<F>(f), thread_id = thread_data->thread_id , task_id]() {
106
106
vtr::Timer task_timer;
@@ -126,7 +126,7 @@ class RouterThreadPool {
126
126
completion_cv.notify_all ();
127
127
}
128
128
};
129
-
129
+
130
130
// Queue the task
131
131
{
132
132
std::lock_guard<std::mutex> lock (thread_data->queue_mutex );
@@ -142,7 +142,7 @@ class RouterThreadPool {
142
142
completion_cv.wait (lock, [this ]() { return active_tasks == 0 ; });
143
143
}
144
144
145
- ~RouterThreadPool () {
145
+ ~thread_pool () {
146
146
// VTR_LOG("Shutting down thread pool after %.3f seconds, processed %zu total tasks\n",
147
147
// pool_timer.elapsed_sec(), total_tasks_queued.load());
148
148
@@ -156,7 +156,7 @@ class RouterThreadPool {
156
156
}
157
157
thread_data->cv .notify_one ();
158
158
}
159
-
159
+
160
160
// Join all threads
161
161
for (auto & thread_data : threads) {
162
162
if (thread_data->thread .joinable ()) {
@@ -167,3 +167,5 @@ class RouterThreadPool {
167
167
}
168
168
}
169
169
};
170
+
171
+ } // namespace vtr
0 commit comments