From 5378f321f41d035689d6d0b02242b69f5bef3256 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Sun, 24 Aug 2025 22:29:59 +0300 Subject: [PATCH] utils: add set_thread_name With HAVE_PTHREAD_SETNAME_NP --- lib/utils/mu-utils.cc | 8 ++++++++ lib/utils/mu-utils.hh | 10 ++++++++++ 2 files changed, 18 insertions(+) diff --git a/lib/utils/mu-utils.cc b/lib/utils/mu-utils.cc index deb966f3..f08e6ea0 100644 --- a/lib/utils/mu-utils.cc +++ b/lib/utils/mu-utils.cc @@ -711,3 +711,11 @@ Mu::fputs_encoded (const std::string& str, FILE *stream) return (rv == EOF) ? false: true; } + +void +Mu::set_thread_name(const std::string& name) +{ +#ifdef HAVE_PTHREAD_SETNAME_NP + pthread_setname_np(pthread_self(), name.c_str()); +#endif /*HAVE_PTHREAD_SETNAME_NP*/ +} diff --git a/lib/utils/mu-utils.hh b/lib/utils/mu-utils.hh index c84d4b3c..ecdc2f24 100644 --- a/lib/utils/mu-utils.hh +++ b/lib/utils/mu-utils.hh @@ -427,6 +427,16 @@ std::string summarize(const std::string& str, size_t max_lines); */ std::string quote(const std::string& str); +/** + * Set the name of the current thread + * + * This depends on pthread_setname_np() being available; + * otherwise, does nothing. + * + * @param name thread name + */ +void set_thread_name(const std::string& name); + /** * Convert any ostreamable<< value to a string