168

In various multi threaded C and C++ projects I've seen the -pthread flag applied to both the compiling and linking stage while others don't use it at all and just pass -lpthread to the linking stage.

Is there any danger not compiling and linking with the -pthread flag - i.e. what does -pthread actually do ? I'm primarily interested in Linux platforms.

jww
  • 97,681
  • 90
  • 411
  • 885
leeeroy
  • 11,216
  • 17
  • 52
  • 54

2 Answers2

124

Try:

gcc -dumpspecs | grep pthread

and look for anything that starts with %{pthread:.

On my computer, this causes files to be compiled with -D_REENTRANT, and linked with -lpthread. On other platforms, this could differ. Use -pthread for most portability.

Using _REENTRANT, on GNU libc, changes the way some libc headers work. As a specific example, it makes errno call a function returning a thread-local location.

C. K. Young
  • 219,335
  • 46
  • 382
  • 435
  • 3
    It may not just be `errno` and preprocessing in general. I'm not sure how relevant the article http://www.hpl.hp.com/techreports/2004/HPL-2004-209.pdf is in practice for gcc optimizations, but I sure was impressed by the depth of the review there. – Pascal Cuoq Jan 24 '10 at 16:53
  • 2
    I don't think the errno example is correct. Even without a -pthread flag or _REENTRANT define, my errno.h (glibc 2.10.1) and gcc (4.4.1 on amd64) generates a dynamic call for errno handling and doesn't link against the symbol address. – Andy Ross Jan 24 '10 at 16:56
  • @Andy: I just did a grep for `_REENTRANT` in `/usr/include`; I'm sure there are other examples of its use. – C. K. Young Jan 24 '10 at 17:39
  • 1
    @Pascal: Thanks for the link. It goes a bit above my head at the moment, but it seems that the central point is that threading cannot just be "tacked on", but instead must be designed in as part of the memory model. I completely agree with that. – C. K. Young Jan 24 '10 at 17:43
  • 2
    @Andy - your version of gcc may be built to provide `-D_REENTRANT` or `-pthread` automatically. Run your build with `g++ -v` and it will dump a lot of output about what parameters the compiler front-end is actually passing to `cc1plus` and `ld`. – Tom Jan 24 '10 at 18:51
  • 1
    I know how it can be made to work. I was just quibbling with the example, which is not true on (at least) Ubuntu 9.10, as verified by examining the generated assembly. As far as I can tell, a typical glibc build treats -pthread as synonymous wiht -lpthread. – Andy Ross Jan 25 '10 at 05:27
  • 4
    There's still a question not answered here: Is there any danger not compiling and linking with the -pthread flag - i.e. what does -pthread actually do ? – natenho Jul 14 '15 at 15:43
  • 2
    @natenho `-pthread` does different things on different platforms, but all of them, in some way, enable pthreads to work. So, omitting `-pthread` may cause pthreads not to work (or not work reliably), or it may do nothing for platforms that already has pthreads support enabled out-of-the-box. – C. K. Young Jul 14 '15 at 15:46
  • 1
    The GNU C library has not needed `-D_REENTRANT` and therefore `-pthread` since, I think, at least as far back as year 2000. All of the necessary switching between single and multi-threaded operation in glibc is hinged on the presence or absence of the `libpthread` library. No code that includes library headers needs to be recompiled. – Kaz Aug 10 '17 at 00:13
  • 1
    On RISCV64, the current version of gcc (11.1.0) cannot generate proper atomic operation instructions for `__atomic_*` builtins, and relies on software implementations in libatomic. `libpthread` requires atomic operations and `-pthread` automatically pulls in `-latomic`, avoiding linking failure. – hexchain Dec 03 '21 at 15:18
47

From man gcc:

-pthread Adds support for multithreading with the pthreads library. This option sets flags for both the preprocessor and linker.

Dmitry
  • 6,590
  • 2
  • 26
  • 19