Can I set the thread name in pthreads / Linux?

Is there a way to set the stream name in Linux?

My main goal is that it would be useful when debugging, and also nice if this name was opened through, for example, /proc/$PID/task/$TID/...

+52
c linux pthreads
Mar 03 '10 at 8:36
source share
3 answers

Use the prctl(2) function with the PR_SET_NAME option (see documents ).

Please note that the documents are a bit confusing. They say

Set the process name for the calling process

but since threads are lightweight processes (LWPs) on Linux, one thread is one process in this case.

You can see the stream name using ps -o cmd or in /proc/$PID/stat between () :

 4223 (kjournald) S 1 1 1 0... 
+24
Mar 03 '10 at 8:43
source share

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:

 #include <pthread.h> // or <pthread_np.h> ? // Linux, NetBSD: int pthread_getname_np(pthread_t th, char *buf, size_t len); // some implementations don't have a safe buffer (see MKS/IBM below) int pthread_getname_np(pthread_t thread, const char **name); int pthread_getname_np(pthread_t thread, char *name); // FreeBSD & OpenBSD: dont' seem to have getname/get_name equivalent? // but I'd imagine there some other mechanism to read it directly for say gdb // Mac OS X: int pthread_getname_np(pthread_t, char*, size_t); 

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:

+87
Nov 03 2018-11-11T00:
source share

You can do this yourself by creating a dictionary mapping pthread_t to std::string , and then associate the result of pthread_self () with the name that you want to assign to the current thread. Note that if you do this, you will need to use a mutex or other synchronization primitive to prevent multiple threads from changing in the dictionary at the same time (unless your implementation of the dictionary already does this for you). You can also use thread dependent variables (see pthread_key_create , pthread_setspecific , pthread_getspecific and pthread_key_delete ) to keep the name of the current thread; however, you cannot access the names of other streams if you do this (whereas with the dictionary you can iterate over all the id / name pairs of a stream from any stream).

+5
Mar 03 '10 at 9:14
source share



All Articles