If you use C++ threads with the GCC compiler (or perhaps more accurately, with the libstdc++ C++ standard library which ships with GCC) on Linux, you may need to include the -pthread
option in your build process to get things to compile and link properly.
What I'm curious about is which library headers invoke that requirement? #include <thread>
is an obvious one, but are there other standard library headers which implicitly have a pthread dependency for libstdc++?
It's not so much header files, but specific functionality. Anything that creates threads under the hood will need or greatly benefit from linking with -lpthread
, such as std::async
from <future>
.
<thread> <mutex> <condition_variable> <future> <shared_mutex> <stop_token>
all use Pthreads types and functions.
The Networking TS headers such as <experimental/net>
and <experimental/executor>
use <mutex>
and <future>
so they also depend on Pthreads.
We'll soon be adding more concurrency headers such as <latch> <barrier> <semaphore>
.
Basically anything related to concurrency and synchronization, except <atomic>
which notably does not depend on anything from Pthreads.
On Solaris prior to version 10, errno
was a global variable in a single-threaded build, but a macro expanding into a function call in a multi-threaded build. That was a source of problems when people linked multi-threaded apps with single-threaded libraries unwittingly. See Compiling Multithreaded Code for more details.
On Linux, errno
is always a macro expanding into a function call that reads a thread-specific errno
regardless whether -pthread
is specified.
Compiling with -pthread
defines macro _REENTRANT
, but none of the GNU C and C++ standard library headers use that macro. Boost library does use it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.