开发者

getting thread name from thread id using posix api

开发者 https://www.devze.com 2023-02-12 09:28 出处:网络
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 us

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 a开发者_开发技巧pi's so that it should be portable.

Thanks in advance all for your inputs


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.


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).


There is no POSIX api to attach a name to a thread.

On Linux you can do prctl(PR_SET_NAME, ...). See man prctl.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消