g++ - Why do I have to pass "-pthread" option while using std::thread?
According to GCC's concurrency page, it's necessary to provide additional options to the compiler based on the features being used. You can verify that your version of GCC's threads rely on POSIX threads:
$ gcc -v 2>&1 | grep "Thread model"Thread model: posix
See this bug report for a justification for the behavior:
The problem is that not all targets need -pthread, or some which do need it spell it differently, and not all platforms define _REENTRANT when the option is used, so there's no reliable way to do what you're asking for.
pthread
is an industry standard over the OS specific threads, using the OS specific calls.
std::thread
is an abstraction in C++ that could be implemented using pthread
or the OS's native threads. But to make it work on as many OS's as possible fast the std-library
implementer could just implement it in posix
as they should be good to go on all compliant OS's.
There are exceptions, some windows only std-library
uses windows native threads instead.