venkysmarty
venkysmarty

Reputation: 11431

getting thread name from thread id using posix api

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

Answers (4)

user149408
user149408

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

karlphillip
karlphillip

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

stefaanv
stefaanv

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

Maxim Egorushkin
Maxim Egorushkin

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

Related Questions