1 #include <pthread.h>
2 #include <signal.h>
3 
set_thread_name(const char * name)4 void set_thread_name(const char *name) {
5 #if defined(__APPLE__)
6   ::pthread_setname_np(name);
7 #elif defined(__FreeBSD__) || defined(__linux__)
8   ::pthread_setname_np(::pthread_self(), name);
9 #elif defined(__NetBSD__)
10   ::pthread_setname_np(::pthread_self(), "%s", const_cast<char *>(name));
11 #endif
12 }
13 
main()14 int main() {
15   set_thread_name("hello world");
16   raise(SIGINT);
17   set_thread_name("goodbye world");
18   raise(SIGINT);
19   return 0;
20 }
21