Skip to content

Commit 70e434b

Browse files
committed
remove extra thread_pool logs
1 parent 0ce9b46 commit 70e434b

File tree

1 file changed

+16
-46
lines changed

1 file changed

+16
-46
lines changed

libs/libvtrutil/src/vtr_thread_pool.h

Lines changed: 16 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22

33
/**
4-
* @file RouterThreadPool.h
4+
* @file vtr_thread_pool.h
55
* @brief A generic thread pool for parallel task execution
66
*/
77

@@ -27,89 +27,70 @@ class thread_pool {
2727
std::mutex queue_mutex;
2828
std::condition_variable cv;
2929
bool stop = false;
30-
size_t thread_id; // For debugging
31-
size_t tasks_completed{0}; // Track number of completed tasks
30+
size_t thread_id;
3231

3332
ThreadData(size_t id) : thread_id(id) {}
3433
};
3534

3635
std::vector<std::unique_ptr<ThreadData>> threads;
37-
std::atomic<size_t> next_thread{0}; // For round-robin assignment
36+
std::atomic<size_t> next_thread{0};
3837
std::atomic<size_t> total_tasks_queued{0};
39-
vtr::Timer pool_timer; // Track pool lifetime
4038
std::atomic<size_t> active_tasks{0};
39+
40+
/* Condition variable for wait_for_all*/
4141
std::mutex completion_mutex;
4242
std::condition_variable completion_cv;
4343

4444
public:
4545
thread_pool(size_t thread_count) {
46-
// VTR_LOG("Creating thread pool with %zu threads\n", thread_count);
4746
threads.reserve(thread_count);
4847

4948
for (size_t i = 0; i < thread_count; i++) {
5049
auto thread_data = std::make_unique<ThreadData>(i);
5150

52-
thread_data->thread = std::thread([td = thread_data.get()]() {
53-
// VTR_LOG("Thread %zu started\n", td->thread_id);
51+
thread_data->thread = std::thread([&]() {
52+
ThreadData* td = thread_data.get();
5453

5554
while (true) {
5655
std::function<void()> task;
5756
{
5857
std::unique_lock<std::mutex> lock(td->queue_mutex);
59-
// if (!td->task_queue.empty()) {
60-
// VTR_LOG("Thread %zu has %zu tasks queued\n",
61-
// td->thread_id, td->task_queue.size());
62-
// }
63-
58+
6459
td->cv.wait(lock, [td]() {
6560
return td->stop || !td->task_queue.empty();
6661
});
67-
62+
6863
if (td->stop && td->task_queue.empty()) {
69-
// VTR_LOG("Thread %zu stopping after completing %zu tasks\n",
70-
// td->thread_id, td->tasks_completed);
7164
return;
7265
}
73-
66+
7467
task = std::move(td->task_queue.front());
7568
td->task_queue.pop();
7669
}
77-
70+
7871
vtr::Timer task_timer;
7972
task();
80-
td->tasks_completed++;
81-
// VTR_LOG("Thread %zu completed task %zu in %.3f seconds\n",
82-
// td->thread_id, td->tasks_completed, task_timer.elapsed_sec());
8373
}
8474
});
85-
75+
8676
threads.push_back(std::move(thread_data));
8777
}
88-
// VTR_LOG("Thread pool initialization completed in %.3f seconds\n",
89-
// pool_timer.elapsed_sec());
9078
}
9179

92-
// Schedule work and get future for result
9380
template<typename F>
9481
void schedule_work(F&& f) {
9582
active_tasks++;
9683

97-
// Round-robin thread assignment
84+
/* Round-robin thread assignment */
9885
size_t thread_idx = (next_thread++) % threads.size();
9986
auto thread_data = threads[thread_idx].get();
10087
size_t task_id = ++total_tasks_queued;
10188

102-
// VTR_LOG("Scheduling task %zu to thread %zu\n", task_id, thread_data->thread_id);
103-
104-
// Wrap the work with task completion tracking
10589
auto task = [this, f = std::forward<F>(f), thread_id = thread_data->thread_id, task_id]() {
10690
vtr::Timer task_timer;
107-
// VTR_LOG("Thread %zu starting task %zu\n", thread_id, task_id);
108-
91+
10992
try {
11093
f();
111-
// VTR_LOG("Thread %zu completed task %zu successfully in %.3f seconds\n",
112-
// thread_id, task_id, task_timer.elapsed_sec());
11394
} catch (const std::exception& e) {
11495
VTR_LOG_ERROR("Thread %zu failed task %zu with error: %s\n",
11596
thread_id, task_id, e.what());
@@ -120,19 +101,16 @@ class thread_pool {
120101
throw;
121102
}
122103

123-
// Track task completion
124104
size_t remaining = --active_tasks;
125105
if (remaining == 0) {
126106
completion_cv.notify_all();
127107
}
128108
};
129109

130-
// Queue the task
110+
/* Queue new task */
131111
{
132112
std::lock_guard<std::mutex> lock(thread_data->queue_mutex);
133113
thread_data->task_queue.push(std::move(task));
134-
// VTR_LOG("Task %zu queued to thread %zu (queue size: %zu)\n",
135-
// task_id, thread_data->thread_id, thread_data->task_queue.size());
136114
}
137115
thread_data->cv.notify_one();
138116
}
@@ -143,26 +121,18 @@ class thread_pool {
143121
}
144122

145123
~thread_pool() {
146-
// VTR_LOG("Shutting down thread pool after %.3f seconds, processed %zu total tasks\n",
147-
// pool_timer.elapsed_sec(), total_tasks_queued.load());
148-
149-
// Signal all threads to stop
124+
/* Stop all threads */
150125
for (auto& thread_data : threads) {
151126
{
152127
std::lock_guard<std::mutex> lock(thread_data->queue_mutex);
153128
thread_data->stop = true;
154-
// VTR_LOG("Signaling thread %zu to stop (remaining tasks: %zu)\n",
155-
// thread_data->thread_id, thread_data->task_queue.size());
156129
}
157130
thread_data->cv.notify_one();
158131
}
159132

160-
// Join all threads
161133
for (auto& thread_data : threads) {
162134
if (thread_data->thread.joinable()) {
163135
thread_data->thread.join();
164-
// VTR_LOG("Thread %zu joined after completing %zu tasks\n",
165-
// thread_data->thread_id, thread_data->tasks_completed);
166136
}
167137
}
168138
}

0 commit comments

Comments
 (0)