Skip to content

Commit 6c55ed2

Browse files
committed
Use pthread_setname_np; set names in the debugger view
1 parent 24df60f commit 6c55ed2

File tree

2 files changed

+6
-15
lines changed

2 files changed

+6
-15
lines changed

testing/int/loguruthreadnames.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
// clazy:excludeall=non-pod-global-static
77

88
TEST_CASE("loguru_thread_local_storage", "[threading]") {
9-
char name[2] = "0";
9+
char name[16] = "0";
1010
loguru::set_thread_name("1");
11-
std::thread([&](){
11+
std::thread([&]() {
1212
loguru::set_thread_name("2");
13-
loguru::get_thread_name(name,2,false);
13+
loguru::get_thread_name(name, sizeof(name), false);
1414
}).join();
1515
REQUIRE(name[0] == '2');
16-
loguru::get_thread_name(name,2,false);
16+
loguru::get_thread_name(name, sizeof(name), false);
1717
REQUIRE(name[0] == '1');
1818
}

thirdparty/loguru/loguru.cpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,6 @@
109109
#elif defined(__OpenBSD__)
110110
#include <pthread_np.h>
111111
#endif
112-
113-
#ifdef __linux__
114-
/* On Linux, the default thread name is the same as the name of the binary.
115-
Additionally, all new threads inherit the name of the thread it got forked from.
116-
For this reason, Loguru use the pthread Thread Local Storage
117-
for storing thread names on Linux. */
118-
#ifndef LOGURU_PTLS_NAMES
119-
#define LOGURU_PTLS_NAMES 1
120-
#endif
121-
#endif
122112
#endif
123113

124114
#if LOGURU_WINTHREADS
@@ -1066,7 +1056,8 @@ namespace loguru
10661056
// Ask the OS about the thread name.
10671057
// This is what we *want* to do on all platforms, but
10681058
// only some platforms support it (currently).
1069-
pthread_getname_np(pthread_self(), buffer, length);
1059+
if(pthread_getname_np(pthread_self(), buffer, length) != 0)
1060+
buffer[0] = 0;
10701061
#elif LOGURU_WINTHREADS
10711062
snprintf(buffer, static_cast<size_t>(length), "%s", thread_name_buffer());
10721063
#else

0 commit comments

Comments
 (0)