Reputation: 11431
i have a requirment that i have to get thread name from thread id or if that is not possible how to get thread name. Here i am not creating the thread. I am creating the library and this library is used by application and library code will be running in application thread context, i want to print thread name in console log to know that log is comming this particular thread. This should be achieved using posix api's so that it should be portable.
Thanks in advance all for your inputs
Upvotes: 0
Views: 3526
Reputation: 5881
I have seem pthread_getname_np()
and pthread_setname_np()
in pthread.h
, to obtain and set the display name of a thread, respectively. However, it is surrounded by conditionals, suggesting it is a nonstandard GNU extension.
You might want to examine the pthread.h
you are compiling against and see if the functions are defined. If your code needs to be portable, be sure to enclose these calls in conditionals.
Upvotes: 0
Reputation: 93410
There's no such thing. But there are informations besides the thread id that might be useful to add to your log, such as the time of the call, the filename of source code and the line number where the call was executed.
Upvotes: 2
Reputation: 14392
As far as I know, posix threads don't have names, but you might use TLS to store a name. See here for an example (not with strings).
Upvotes: 1
Reputation: 136208
There is no POSIX api to attach a name to a thread.
On Linux you can do prctl(PR_SET_NAME, ...)
. See man prctl
.
Upvotes: 0