Significance of -pthread flag when compiling
Solution 1
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.
Solution 2
From man gcc
:
-pthread Adds support for multithreading with the pthreads library. This option sets flags for both the preprocessor and linker.
Related videos on Youtube
leeeroy
Updated on September 01, 2020Comments
-
leeeroy over 3 years
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. -
Pascal Cuoq over 14 yearsIt 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. -
newpxsn over 14 yearsI 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.
-
C. K. Young over 14 years@Andy: I just did a grep for
_REENTRANT
in/usr/include
; I'm sure there are other examples of its use. -
C. K. Young over 14 years@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.
-
Tom over 14 years@Andy - your version of gcc may be built to provide
-D_REENTRANT
or-pthread
automatically. Run your build withg++ -v
and it will dump a lot of output about what parameters the compiler front-end is actually passing tocc1plus
andld
. -
newpxsn over 14 yearsI 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.
-
natenho almost 9 yearsThere'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 ?
-
C. K. Young almost 9 years@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. -
Kaz almost 7 yearsThe 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 thelibpthread
library. No code that includes library headers needs to be recompiled. -
Paris Qian Sen almost 3 yearsI see some people put
-lpthread
instead, what's the difference? Should we prefer-pthread
? -
Victor Paléologue over 2 years
-pthread
implies-lpthread
, but not the other way round.-pthread
is therefore a more complete solution. -
hexchain over 2 yearsOn 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.