-
Notifications
You must be signed in to change notification settings - Fork 26
Feature: set_thread_name #107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mvandeberg
merged 5 commits into
cppalliance:develop
from
mvandeberg:feature/set_thread_name
Jan 23, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fbab0d6
Add thread naming support to thread_pool for debugging visibility
mvandeberg 5f7bd72
Fix Windows thread naming for long UTF-8 names
mvandeberg 45599d0
Add implementation overview blocks to thread_name.cpp and thread_pool…
mvandeberg 5ae1607
Fix overview block placement
mvandeberg 23c66ca
Squelch 'name' unused warning on incompatible platforms
mvandeberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| // | ||
| // Copyright (c) 2026 Michael Vandeberg | ||
| // | ||
| // Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
| // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
| // | ||
| // Official repository: https://github.com/cppalliance/capy | ||
| // | ||
|
|
||
| #ifndef BOOST_CAPY_DETAIL_THREAD_NAME_HPP | ||
| #define BOOST_CAPY_DETAIL_THREAD_NAME_HPP | ||
|
|
||
| #include <boost/capy/detail/config.hpp> | ||
|
|
||
| /* | ||
| Thread naming abstraction for debugging purposes. | ||
|
|
||
| Sets the current thread's name which appears in debuggers | ||
| (GDB, LLDB, Visual Studio) and system tools (htop, Process Explorer). | ||
|
|
||
| Platform support: | ||
| - Windows: SetThreadDescription (Windows 10 1607+) | ||
| - macOS: pthread_setname_np (truncated to 63 chars) | ||
| - Linux/FreeBSD/NetBSD: pthread_setname_np (truncated to 15 chars) | ||
| - Other platforms: no-op | ||
| */ | ||
|
|
||
| namespace boost { | ||
| namespace capy { | ||
| namespace detail { | ||
|
|
||
| /** Set the name of the current thread for debugging purposes. | ||
|
|
||
| The name may be truncated to platform limits: | ||
| - Linux/FreeBSD/NetBSD: 15 characters | ||
| - macOS: 63 characters | ||
| - Windows: no practical limit | ||
|
|
||
| @param name The thread name to set (UTF-8 encoded on Windows). | ||
| */ | ||
| BOOST_CAPY_DECL | ||
| void | ||
| set_current_thread_name(char const* name) noexcept; | ||
|
|
||
| } // detail | ||
| } // capy | ||
| } // boost | ||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| // | ||
| // Copyright (c) 2026 Michael Vandeberg | ||
| // | ||
| // Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
| // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
| // | ||
| // Official repository: https://github.com/cppalliance/capy | ||
| // | ||
|
|
||
| #include <boost/capy/detail/thread_name.hpp> | ||
|
|
||
| #if defined(_WIN32) | ||
|
|
||
| #ifndef NOMINMAX | ||
| #define NOMINMAX | ||
| #endif | ||
| #include <windows.h> | ||
| #include <string> | ||
|
|
||
| #elif defined(__APPLE__) | ||
|
|
||
| #include <pthread.h> | ||
| #include <cstring> | ||
|
|
||
| #elif defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) | ||
|
|
||
| #include <pthread.h> | ||
| #include <cstring> | ||
|
|
||
| #endif | ||
|
|
||
| /* | ||
| Platform-specific thread naming implementation. | ||
|
|
||
| Each platform has a different API and name length limit: | ||
| - Windows: SetThreadDescription with UTF-8 to UTF-16 conversion (no limit) | ||
| - macOS: pthread_setname_np(name) with 63-char limit | ||
| - Linux/BSD: pthread_setname_np(thread, name) with 15-char limit | ||
|
|
||
| All operations are best-effort and silently fail on error, since thread | ||
| naming is purely for debugging visibility and should never affect program | ||
| correctness. The noexcept guarantee is maintained by catching exceptions | ||
| from std::wstring allocation on Windows. | ||
| */ | ||
|
|
||
| namespace boost { | ||
| namespace capy { | ||
| namespace detail { | ||
|
|
||
| void | ||
| set_current_thread_name(char const* name) noexcept | ||
| { | ||
| #if defined(_WIN32) | ||
| // SetThreadDescription requires Windows 10 1607+. Older Windows versions | ||
| // are unsupported; the program may fail to link on those systems. | ||
|
|
||
| // Query required buffer size for UTF-8 to wide conversion. | ||
| int required = MultiByteToWideChar(CP_UTF8, 0, name, -1, nullptr, 0); | ||
| if(required <= 0) | ||
| return; | ||
|
|
||
| // Allocate and convert; catch exceptions to maintain noexcept. | ||
| std::wstring wname; | ||
| try | ||
| { | ||
| wname.resize(static_cast<std::size_t>(required)); | ||
| } | ||
| catch(...) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| if(MultiByteToWideChar(CP_UTF8, 0, name, -1, wname.data(), required) <= 0) | ||
| return; | ||
|
|
||
| // Ignore return value: thread naming is best-effort for debugging. | ||
| (void)SetThreadDescription(GetCurrentThread(), wname.c_str()); | ||
| #elif defined(__APPLE__) | ||
| // macOS pthread_setname_np takes only the name (no thread handle) | ||
| // and has a 64 char limit (63 + null terminator) | ||
| char truncated[64]; | ||
| std::strncpy(truncated, name, 63); | ||
| truncated[63] = '\0'; | ||
|
|
||
| // Ignore return value: thread naming is best-effort for debugging. | ||
| (void)pthread_setname_np(truncated); | ||
| #elif defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) | ||
| // pthread_setname_np has 16 char limit (15 + null terminator) | ||
| char truncated[16]; | ||
| std::strncpy(truncated, name, 15); | ||
| truncated[15] = '\0'; | ||
|
|
||
| // Ignore return value: thread naming is best-effort for debugging. | ||
| (void)pthread_setname_np(pthread_self(), truncated); | ||
| #else | ||
| (void)name; | ||
| #endif | ||
| } | ||
|
|
||
| } // detail | ||
| } // capy | ||
| } // boost | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.