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