c++ - _GLIBCXX_USE_NANOSLEEP 到底是什么?

标签 c++ gcc sleep glibc

一个名为 _GLIBCXX_USE_NANOSLEEP 的预处理器宏出现在两个标准头文件中:

  • c++/4.7.1/x86_64-unknown-linux-gnu/bits/c++config.h
  • c++/4.7.1/thread

在 GCC 4.7.1(Linux,64 位)的默认构建中,c++config.h 唯一包含的就是以下注释:

/* Defined if nanosleep is available. */
/* #undef _GLIBCXX_USE_NANOSLEEP */

而在thread中,std::this_thread::sleep_for()std::this_thread::sleep_until()的定义取决于要定义的宏。如果未定义,则这两个函数(尽管 C++ 标准要求)也不会被定义。

在我的系统 (glibc 2.15) 上,没有定义宏,尽管 nanosleep() 函数(在 ctime 中声明)存在并且可以运行。

我想知道这是怎么回事以及如何处理它。具体来说:

  • 在构建 GCC 时是否应该使用配置选项来默认激活此宏,如 this post 所建议的那样? (我在 online documentation of the build process 中找不到任何内容。)
  • nanosleep() 函数和宏之间真的有关系吗? ctime/time.h 中的 nanosleep() 声明似乎不依赖或定义宏。
  • 在我自己的头文件中定义宏或作为命令行上的 -D 选项(如 this related question 中的建议)是否存在任何特定风险?如果我在 nanosleep() 不可用的系统上执行此操作,我如何才能真正找到?

更新 从 GCC 4.8 开始,libstdc++ 中自动包含对 std::this_thread::sleep_for() 等的支持。不再需要配置标志。来自 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;

但请注意 Jonathan 的回答中给出的 GCC 4.8 和 4.9 的更多详细信息。

最佳答案

当构建 libstdc++ 时,它的 configure脚本测试您的系统以查看支持哪些功能,并根据结果定义(或取消定义)c++config.h 中的各种宏

在你的情况下 configure确定 POSIX nanosleep() 功能不可用且未定义宏。但是,正如您所说,nanosleep() 在您的系统上可用。 configure 未启用的原因除非您使用 --enable-libstdcxx-time,否则对它的检查甚至不会运行选项(记录在 Configuration chapter of the libstdc++ manual 中,而不是 GCC 配置文档中)

  • 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.)

是的,--enable-libstdcxx-time

  • Is there really a relation between the nanosleep() function and the macro? The declaration of nanosleep() in ctime/time.h does not seem to depend on, or define, the macro.

glibc 函数的声明不依赖于 libstdc++ 的宏,不。但是宏告诉 libstdc++ 是否使用该函数。

  • 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 where nanosleep() is not available, and how can I actually find out?

它很顽皮,不受支持,但会起作用。宏是一个内部实现细节,应该由配置而不是由用户设置,并且更改实现的内部宏的定义可能会破坏事情。但在这种情况下它不会因为唯一依赖它的代码在头文件中,libstdc++.so 中没有库代码被影响。

但最好重新安装 GCC 并使用 --enable-libstdcxx-time选项,或者如果这不可能,请编辑您的 c++config.h将宏定义为 true。

如果您在不同的系统上定义它,nanosleep()当你 #include <thread> 时你会得到一个编译错误。 .

我有一些 ideas用于改进该配置,所以 nanosleep()sched_yield()默认情况下会检查,但我还没有时间处理它们。

更新:我已经提交了一些更改,以便在没有 --enable-libstdcxx-time 的情况下构建 GCC 4.8仍将定义 std::this_thread::yield() (作为无操作)并将实现 std::this_thread::sleep_for()std::this_thread::sleep_until()使用较低的分辨率 ::sleep()::usleep()函数而不是 ::nanosleep() .最好定义--enable-libstdcxx-time不过。

另一个更新: GCC 4.9.0 已经发布,现在默认自动启用 nanosleepsched_yield在已知支持它们的平台上。不再需要使用--enable-libstdcxx-time .

关于c++ - _GLIBCXX_USE_NANOSLEEP 到底是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12523122/

相关文章:

c++ - 在 Windows 上将 filesystem::path 转换为 char*

c - 链接器错误 - undefined reference -gcc

python - 在 if 语句检查时使线程休眠,Python Pool

c++ - g++ -fno-enforce-eh-specs - 为什么/如何违反 C++ 标准?

C++ 多维数组问题 - 条目打印不止一次

c++ - gcc中除法的优化

linux - ld 不链接 Linux 上的 OpenGL

java - 结束话题

Promise循环中的javascript sleep 函数

c++ - 头文件中的单例编译错误