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