Why this_thread::sleep_for doesn't need to be linked with pthread?
Usually building thread-dependent code in GCC requires explicit linking against pthreads:
font>g-pthread main.cxx
However, the following code compiles, links and runs fine without linking with pthread:
font>#include <iostream>
#include <thread>
using namespace std::chrono_literals;
int main() {
std::this_thread::sleep_for(1000ms);
return 0;
}
I guess what happened herestd::this_thread ::sleep_for
is to use some POSIX functions in libc (instead of some in pthread function)? but if this is the case, does std::this_thread::sleep_for
Changes based on whether it is called from the main thread Execute ?
uj5u.com enthusiastic netizens replied:
because of the call to std::this_thread:: sleep_for
Convert to underlying call to pairnanosleep
, it is defined in libc.so.6
instead of in Definelibpthread.so.0
.
Please note that when linking with GLIBC-2.34 and later, use additional features (previously required -pthread
) is no longer needed because GLIBC got rid of libpthread
.
See alsothis answer.
0 Comments