As in glibc v2.12, you can use pthread_setname_np and pthread_getname_np to set / get the name of the thread.
These interfaces are available on several other POSIX systems (BSD, QNX, Mac) in various slightly different formats.
The name setting will look something like this:
#include <pthread.h> // or maybe <pthread_np.h> for some OSes // Linux int pthread_setname_np(pthread_t thread, const char *name); // NetBSD: name + arg work like printf(name, arg) int pthread_setname_np(pthread_t thread, const char *name, void *arg); // FreeBSD & OpenBSD: function name is slightly different, and has no return value void pthread_set_name_np(pthread_t tid, const char *name); // Mac OS X: must be set from within the thread (can't specify thread ID) int pthread_setname_np(const char*);
And you can return the name:
As you can see, it is not fully portable between POSIX systems, but as far as I can tell from Linux, it should be consistent. Besides Mac OS X (where you can only do this from a stream), others are at least easily adaptable for cross-platform code.
Sources:
drfrogsplat Nov 03 2018-11-11T00: 00Z
source share