'pthread_setname_np' has not been declared in this scope

I have created multiple threads in my application. I want to assign a name to each pthread, so I used pthread_setname_np

which worked on Ubuntu but doesn't work on SUSE Linux.

I searched for it and found out that "_np" means "not portable" and this api is not available for all Linux OS versions.

So now I only want to do this if the API is available. How to determine if an api is available or not? I need something like this.

#ifdef SOME_MACRO
    pthread_setname_np(tid, "someName");
#endif

      

+3


source to share


3 answers


You can use feature_test_macro _GNU_SOURCE

to check if this feature is available:

#ifdef _GNU_SOURCE
    pthread_setname_np(tid, "someName");
#endif

      

But the manual states that pthread_setname_np

they are pthread_getname_np

introduced in glibc 2.12

. So if you are using an older glibc (say 2.5) then the definition _GNU_SOURCE

will not help.

Therefore, it is best to avoid this non-portable function and you can easily name the streams yourself as part of creating the stream, for example using a map between the stream id and the array, e.g .:



pthread_t tid[128];
char thr_names[128][256]; //each name corresponds to on thread in 'tid'

      

You can check the glibc version using:

getconf GNU_LIBC_VERSION

      

+4


source


This kind of thing - figuring out if a particular feature exists in your compilation environment - is what people use GNU Autoconf scripts for.

If your project is already using autoconf, you can add it to your config source after you have checked the pthreads compiler flags and checkboxes:



AC_CHECK_FUNCS(pthread_setname_np)

      

... and it will define the HAVE_PTHREAD_SETNAME_NP macro if the function exists.

+1


source


Since this function was introduced in glibc 2.12, you can use:

#if __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 12
    pthread_setname_np(tid, "someName");
#endif

      

+1


source







All Articles