Skip to content

Commit 550d2ba

Browse files
committed
feat(thread_pool): name worker threads tpool-svc-N
Sets each worker's thread name via capy::set_current_thread_name so they show up in debuggers and system tools as tpool-svc-1, tpool-svc-2, etc. Format fits Linux's 15-char pthread name limit for indices up to 9999.
1 parent cd19108 commit 550d2ba

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

include/boost/corosio/detail/thread_pool.hpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
#include <boost/corosio/detail/config.hpp>
1414
#include <boost/corosio/detail/intrusive.hpp>
1515
#include <boost/capy/ex/execution_context.hpp>
16+
#include <boost/capy/test/thread_name.hpp>
1617

1718
#include <condition_variable>
19+
#include <cstdio>
1820
#include <mutex>
1921
#include <stdexcept>
2022
#include <thread>
@@ -81,7 +83,7 @@ class thread_pool final : public capy::execution_context::service
8183
std::vector<std::thread> threads_;
8284
bool shutdown_ = false;
8385

84-
void worker_loop();
86+
void worker_loop(unsigned index);
8587

8688
public:
8789
using key_type = thread_pool;
@@ -110,7 +112,7 @@ class thread_pool final : public capy::execution_context::service
110112
try
111113
{
112114
for (unsigned i = 0; i < num_threads; ++i)
113-
threads_.emplace_back([this] { worker_loop(); });
115+
threads_.emplace_back([this, i] { worker_loop(i + 1); });
114116
}
115117
catch (...)
116118
{
@@ -145,8 +147,14 @@ class thread_pool final : public capy::execution_context::service
145147
};
146148

147149
inline void
148-
thread_pool::worker_loop()
150+
thread_pool::worker_loop(unsigned index)
149151
{
152+
// Name format chosen to fit Linux's 15-char pthread limit:
153+
// "tpool-svc-" (10) + up to 4 digit index leaves "tpool-svc-9999".
154+
char name[16];
155+
std::snprintf(name, sizeof(name), "tpool-svc-%u", index);
156+
capy::set_current_thread_name(name);
157+
150158
for (;;)
151159
{
152160
pool_work_item* w;

0 commit comments

Comments
 (0)