Significance of -pthread flag when compiling
Asked Answered
G

2

169

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.

Gelsenkirchen answered 24/1, 2010 at 16:33 Comment(0)
P
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.

Parboil answered 24/1, 2010 at 16:37 Comment(10)
It may not just be errno and preprocessing in general. I'm not sure how relevant the article 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.Implicative
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.Noisette
@Andy: I just did a grep for _REENTRANT in /usr/include; I'm sure there are other examples of its use.Parboil
@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.Parboil
@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.Ozonolysis
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.Noisette
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 ?Afoot
@Afoot -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.Parboil
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.Curlpaper
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.Collision
B
47

From man gcc:

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

Boris answered 24/1, 2010 at 16:38 Comment(2)
I see some people put -lpthread instead, what's the difference? Should we prefer -pthread?Turbidimeter
-pthread implies -lpthread, but not the other way round. -pthread is therefore a more complete solution.Enzymology

© 2022 - 2024 — McMap. All rights reserved.