0

I'd line to use pthread_setname_np function if it's available. Code from the manpage:

   #define _GNU_SOURCE             /* See feature_test_macros(7) */
   #include <pthread.h>
   int pthread_setname_np(pthread_t thread, const char *name);
   int pthread_getname_np(pthread_t thread,
                          char *name, size_t len);

Is it safe to just do like that (no includes, no defines):

#ifdef _GNU_SOURCE
    pthread_setname_np(pthread_self(), "mythread");
#endif

Includes are not needed because the C++ #include <thread> that I use seems to pull pthreads. The _GNU_SOURCE is always enabled in libstdc++, and if it gets disabled - the code will still compile.

Am I missing something?

Velkan
  • 7,067
  • 6
  • 43
  • 87
  • 1
    Did you read [feature_test_macros(7)](http://man7.org/linux/man-pages/man7/feature_test_macros.7.html) ? – Basile Starynkevitch Oct 25 '16 at 11:41
  • @BasileStarynkevitch, yes. It's about controlling what the standard library provides. I'm interested in activating of a feature of my program depending on how the build was set up. – Velkan Oct 25 '16 at 11:46
  • It seems this question is tag'd as `c` but the actual question discussed `c++`. These are different languages. Please pick one. – user3629249 Oct 26 '16 at 22:37
  • @user3629249, it's about `glibc`'s feature test macros. I wouldn't tag it with `c` or `c++` at all, but I figured that C pogrammers are more familiar with glibc and Linux (and there are more of C programmers in general). – Velkan Oct 27 '16 at 06:36

1 Answers1

4

Is it safe to just do like that (no includes, no defines):

Yes. It's safe as long as you include <pthread.h> which provides the prototype for pthread_setname_np.

Includes are not needed because the C++ #include that I use seems to pull pthreads.

libc++ uses pthreads library underneath. So, it works with pthread.h. But that doesn't mean pthread.h isn't needed if you include <thread>. What if libc++ changes the underlying mechanism to implement threads? So, it's not safe to by pass pthread.h just because it happens to be available by other means.

P.P
  • 117,907
  • 20
  • 175
  • 238