A preprocessor macro called _GLIBCXX_USE_NANOSLEEP appears in two standard header files:
- c++/4.7.1/x86_64-unknown-linux-gnu/bits/c++config.h
- c++/4.7.1/thread
In a default build of GCC 4.7.1 (Linux, 64-bit) the only thing c++config.h includes is this comment:
/* Defined if nanosleep is available. */
/* #undef _GLIBCXX_USE_NANOSLEEP */
Whereas in thread, the definition of std::this_thread::sleep_for()
and std::this_thread::sleep_until()
depend on the macro to be defined. If it isn't defined, both functions – although required by the C++ Standard – won't be defined either.
On my system (glibc 2.15), the macro is not defined, although the nanosleep()
function (declared in ctime
) exists and is operational.
I'd like to know what this is all about and how to deal with it. Specifically:
- Is there a configuration option that should be used when building GCC to activate this macro by default, as suggested by this post? (I couldn't find any in the online documentation of the build process.)
- Is there really a relation between the
nanosleep()
function and the macro? The declaration ofnanosleep()
inctime
/time.h
does not seem to depend on, or define, the macro. - Is there any specific risk involved in defining the macro in my own header files, or as a
-D
option on the command line (as suggested in this related question)? What if I do this on a system wherenanosleep()
is not available, and how can I actually find out?
Update From GCC 4.8 onwards, support for std::this_thread::sleep_for()
and the like is automatically included in libstdc++. No configuration flag is required any more. From the GCC 4.8 change log:
this_thread::sleep_for(), this_thread::sleep_until() and this_thread::yield() are defined without requiring the configure option --enable-libstdcxx-time;
But note the further details on this for GCC 4.8 and 4.9 given in Jonathan's answer.
See Question&Answers more detail:os