MCPcopy Create free account
hub / github.com/bshoshany/thread-pool / set_os_thread_name

Method set_os_thread_name

include/BS_thread_pool.hpp:1117–1136  ·  view source on GitHub ↗

* @brief Set the name of the current thread using the current platform's native API. This should work on Windows, Linux, and macOS. Note that on Linux thread names are limited to 16 characters, including the null terminator. * * @param name The name to set. * @return `true` if the name was set successfully, `false` otherwise. */

Source from the content-addressed store, hash-verified

1115 * @return `true` if the name was set successfully, `false` otherwise.
1116 */
1117 static bool set_os_thread_name(const std::string& name)
1118 {
1119 #if defined(_WIN32)
1120 // On Windows thread names are wide strings, so we need to convert them from normal strings.
1121 const int size = MultiByteToWideChar(CP_UTF8, 0, name.data(), -1, nullptr, 0);
1122 if (size == 0)
1123 return false;
1124 std::wstring wide(static_cast<std::size_t>(size), 0);
1125 if (MultiByteToWideChar(CP_UTF8, 0, name.data(), -1, wide.data(), size) == 0)
1126 return false;
1127 const HRESULT hr = SetThreadDescription(GetCurrentThread(), wide.data());
1128 return SUCCEEDED(hr);
1129 #elif defined(__linux__)
1130 // On Linux this is straightforward.
1131 return pthread_setname_np(pthread_self(), name.data()) == 0;
1132 #elif defined(__APPLE__)
1133 // On macOS, unlike Linux, a thread can only set a name for itself, so the signature is different.
1134 return pthread_setname_np(name.data()) == 0;
1135 #endif
1136 }
1137
1138 /**
1139 * @brief Get the priority of the current thread using the current platform's native API. This should work on Windows, Linux, and macOS.

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected