什么是_GLIBCXX_USE_NANOSLEEP ?

时间:2023-01-14 09:12:41

A preprocessor macro called _GLIBCXX_USE_NANOSLEEP appears in two standard header files:

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

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

In a default build of GCC 4.7.1 (Linux, 64-bit) the only thing c++config.h includes is this comment:

在GCC 4.7.1 (Linux, 64位)的默认版本中,只有c++配置。h包括以下评论:

/* 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.

而在线程中,std::this_thread::sleep_for()和std::this_thread::sleep_until()的定义取决于要定义的宏。如果没有定义,那么这两个函数——尽管是c++标准所要求的——也不会被定义。

On my system (glibc 2.15), the macro is not defined, although the nanosleep() function (declared in ctime) exists and is operational.

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

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.)
  • 如本文所建议的,在构建GCC时是否应该使用配置选项来默认激活这个宏?(我在构建过程的在线文档中找不到任何文档。)
  • 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.
  • 在nanosleep()函数和宏之间真的有关系吗?在ctime/time中声明nanosleep()。h似乎不依赖或定义宏。
  • 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?
  • 在我自己的头文件中定义宏,或者作为命令行上的-D选项(如相关问题中所建议的),是否存在特定的风险?如果我在一个无法使用nanosleep()的系统上执行此操作,那么我如何才能真正找到呢?

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:

从GCC 4.8开始更新,对std::this_thread: sleep_for()等的支持将自动包含在libstdc++中。不再需要配置标志。从GCC 4.8变更日志:

this_thread::sleep_for(), this_thread::sleep_until() and this_thread::yield() are defined without requiring the configure option --enable-libstdcxx-time;

this_thread::sleep_for(), this_thread::sleep_until()和this_thread:::yield()的定义不需要配置选项——启用-libstdcx -time;

But note the further details on this for GCC 4.8 and 4.9 given in Jonathan's answer.

但是请注意乔纳森的回答中关于GCC 4.8和4.9的详细信息。

1 个解决方案

#1


68  

When libstdc++ is built its configure script tests your system to see what features are supported, and based on the results it defines (or undefines) various macros in c++config.h

当构建libstdc++时,它的configure脚本会测试您的系统,看看支持哪些特性,并基于它在c++config.h中定义(或不定义)各种宏的结果

In your case configure determined that the POSIX nanosleep() function is not available and the macro is not defined. However, as you say, nanosleep() is available on your system. The reason it's not enabled by configure is that the checks for it don't even run unless you use the --enable-libstdcxx-time option (documented in the Configuration chapter of the libstdc++ manual, not the GCC configure docs)

在您的案例中,configure确定了POSIX nanosleep()函数是不可用的,而宏没有定义。但是,正如您所说,nanosleep()在您的系统中是可用的。配置未启用它的原因是,它的检查甚至不会运行,除非您使用——libstdcxx-time选项(在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.)
  • 如本文所建议的,在构建GCC时是否应该使用配置选项来默认激活这个宏?(我在构建过程的在线文档中找不到任何文档。)

Yes, --enable-libstdcxx-time

是的,——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.
  • 在nanosleep()函数和宏之间真的有关系吗?在ctime/time中声明nanosleep()。h似乎不依赖或定义宏。

The declaration of glibc's function doesn't depend on libstdc++'s macro, no. But the macro tells libstdc++ whether to use the function or not.

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?
  • 在我自己的头文件中定义宏,或者作为命令行上的-D选项(如相关问题中所建议的),是否存在特定的风险?如果我在一个无法使用nanosleep()的系统上执行此操作,那么我如何才能真正找到呢?

It's naughty and is unsupported, but will work. The macro is an internal implementation detail that should be set by configure and not by users and changing the definition of the implementation's internal macros can break things. But in this case it won't because the only code that depends on it is in a header, no library code in libstdc++.so is affected.

它很顽皮,没有人支持,但会起作用。宏是一个内部实现细节,应该由configure来设置,而不是由用户来设置,更改实现内部宏的定义可能会造成破坏。但是在这种情况下,它不会因为唯一依赖于它的代码是在header中,在libstdc++中没有库代码。因此受到影响。

But it would be better to reinstall GCC and use the --enable-libstdcxx-time option, or if that's not possible edit your c++config.h to define the macro to true.

但是,最好重新安装GCC并使用——启用-libstdcxx-time选项,或者如果不可能编辑c++配置的话。将宏定义为真。

If you define it on a different system where nanosleep() isn't available you'll get a compilation error when you #include <thread>.

如果在另一个系统上定义它,而nanosleep()不可用,那么当您#include 时,您将会得到一个编译错误。

I have some ideas for improving that configuration, so nanosleep() and sched_yield() will be checked for by default, but I haven't had time to work on them yet.

我有一些改进配置的想法,因此默认情况下将检查nanosleep()和sched_yield(),但我还没有时间对它们进行研究。

Update: I've committed some changes so that building GCC 4.8 without --enable-libstdcxx-time will still define std::this_thread::yield() (as a no-op) and will implement std::this_thread::sleep_for() and std::this_thread::sleep_until() using the lower resolution ::sleep() and ::usleep() functions instead of ::nanosleep(). It's still better to define --enable-libstdcxx-time though.

更新:我已经进行了一些更改,以便构建没有启用的GCC 4.8时仍然会定义std::this_thread: yield()(作为一个无操作),并将实现std::this_thread: sleep_for()和std: this_thread::sleep_until(),使用较低的分辨率:更好的方法是定义——启用-libstdcxx-time。

Another update: GCC 4.9.0 is out and now defaults to automatically enabling nanosleep and sched_yield on platforms that are known to support them. There is no longer any need to use --enable-libstdcxx-time.

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

#1


68  

When libstdc++ is built its configure script tests your system to see what features are supported, and based on the results it defines (or undefines) various macros in c++config.h

当构建libstdc++时,它的configure脚本会测试您的系统,看看支持哪些特性,并基于它在c++config.h中定义(或不定义)各种宏的结果

In your case configure determined that the POSIX nanosleep() function is not available and the macro is not defined. However, as you say, nanosleep() is available on your system. The reason it's not enabled by configure is that the checks for it don't even run unless you use the --enable-libstdcxx-time option (documented in the Configuration chapter of the libstdc++ manual, not the GCC configure docs)

在您的案例中,configure确定了POSIX nanosleep()函数是不可用的,而宏没有定义。但是,正如您所说,nanosleep()在您的系统中是可用的。配置未启用它的原因是,它的检查甚至不会运行,除非您使用——libstdcxx-time选项(在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.)
  • 如本文所建议的,在构建GCC时是否应该使用配置选项来默认激活这个宏?(我在构建过程的在线文档中找不到任何文档。)

Yes, --enable-libstdcxx-time

是的,——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.
  • 在nanosleep()函数和宏之间真的有关系吗?在ctime/time中声明nanosleep()。h似乎不依赖或定义宏。

The declaration of glibc's function doesn't depend on libstdc++'s macro, no. But the macro tells libstdc++ whether to use the function or not.

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?
  • 在我自己的头文件中定义宏,或者作为命令行上的-D选项(如相关问题中所建议的),是否存在特定的风险?如果我在一个无法使用nanosleep()的系统上执行此操作,那么我如何才能真正找到呢?

It's naughty and is unsupported, but will work. The macro is an internal implementation detail that should be set by configure and not by users and changing the definition of the implementation's internal macros can break things. But in this case it won't because the only code that depends on it is in a header, no library code in libstdc++.so is affected.

它很顽皮,没有人支持,但会起作用。宏是一个内部实现细节,应该由configure来设置,而不是由用户来设置,更改实现内部宏的定义可能会造成破坏。但是在这种情况下,它不会因为唯一依赖于它的代码是在header中,在libstdc++中没有库代码。因此受到影响。

But it would be better to reinstall GCC and use the --enable-libstdcxx-time option, or if that's not possible edit your c++config.h to define the macro to true.

但是,最好重新安装GCC并使用——启用-libstdcxx-time选项,或者如果不可能编辑c++配置的话。将宏定义为真。

If you define it on a different system where nanosleep() isn't available you'll get a compilation error when you #include <thread>.

如果在另一个系统上定义它,而nanosleep()不可用,那么当您#include 时,您将会得到一个编译错误。

I have some ideas for improving that configuration, so nanosleep() and sched_yield() will be checked for by default, but I haven't had time to work on them yet.

我有一些改进配置的想法,因此默认情况下将检查nanosleep()和sched_yield(),但我还没有时间对它们进行研究。

Update: I've committed some changes so that building GCC 4.8 without --enable-libstdcxx-time will still define std::this_thread::yield() (as a no-op) and will implement std::this_thread::sleep_for() and std::this_thread::sleep_until() using the lower resolution ::sleep() and ::usleep() functions instead of ::nanosleep(). It's still better to define --enable-libstdcxx-time though.

更新:我已经进行了一些更改,以便构建没有启用的GCC 4.8时仍然会定义std::this_thread: yield()(作为一个无操作),并将实现std::this_thread: sleep_for()和std: this_thread::sleep_until(),使用较低的分辨率:更好的方法是定义——启用-libstdcxx-time。

Another update: GCC 4.9.0 is out and now defaults to automatically enabling nanosleep and sched_yield on platforms that are known to support them. There is no longer any need to use --enable-libstdcxx-time.

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