'Is C++'s std::thread a wrapper for pthread on all platforms?
I'm learning the use of std::thread, I used to think std::thread is a completely different threading library independent of pthread , I use the following code to test:
void run(void) {
std::cout << "Hello World" <<endl;
}
int main(void) {
std::thread t(run);
t.join();
}
I thought it could be compiled directly via:
$ g++ main.cpp -o main
But it tells me that the "pthread_create" function is not defined:
/usr/bin/ld: /tmp/ccZpJ3EI.o: in function `std::thread::thread<void (&)(), , void>(void (&)())':
main.cpp:(.text._ZNSt6threadC2IRFvvEJEvEEOT_DpOT0_[_ZNSt6threadC5IRFvvEJEvEEOT_DpOT0_]+0x2f): undefined reference to `pthread_create'
collect2: error:ld return 1
I have to add the link for pthread:
$ g++ main.cpp -o main -lpthread
My OS is archlinux/manjaro, the C++ standard version is C++ 17.
Do I also need to link pthreads if I'm on another OS?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|