C++ std::this_thread::sleep_for() 和 GCC
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4438084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
std::this_thread::sleep_for() and GCC
提问by Predrag
When I try to compile this simple program:
当我尝试编译这个简单的程序时:
#include<thread>
void f() {
std::this_thread::sleep_for(std::chrono::seconds(3));
}
int main() {
std::thread t(f);
t.join();
}
with gcc version 4.4.3 on Ubuntu 10.04 (32 bit):
在 Ubuntu 10.04(32 位)上使用 gcc 版本 4.4.3:
$ g++ -std=c++0x -pthread a.cpp -o a
I get:
我得到:
error: ‘sleep_for' is not a member of ‘std::this_thread'
I looked in header 'thread'.
sleep_for() is protected with _GLIBCXX_USE_NANOSLEEP
我查看了标题“线程”。
sleep_for() 受 _GLIBCXX_USE_NANOSLEEP 保护
#ifdef _GLIBCXX_USE_NANOSLEEP
...
/// sleep_for
template<typename _Rep, typename _Period>
inline void
sleep_for(const chrono::duration<_Rep, _Period>& __rtime)
...
Why is _GLIBCXX_USE_NANOSLEEP
not defined?
How do I get this example to compile?
为什么_GLIBCXX_USE_NANOSLEEP
没有定义?
我如何编译这个例子?
Update 17 Sep 2012(jogojapan): I ran into this same problem today, using GCC 4.7.1. I wonder if there is any news on how to avoid it, other than by defining _GLIBCXX_USE_NANOSLEEP
. I tried using -std=gnu11
, but to no avail.
2012 年 9 月 17 日更新(jogojapan):我今天遇到了同样的问题,使用 GCC 4.7.1。我想知道除了定义_GLIBCXX_USE_NANOSLEEP
. 我尝试使用-std=gnu11
,但无济于事。
There is also an old, unresolved bug report for GCC 4.4: https://bugs.launchpad.net/ubuntu/+source/gcc-4.4/+bug/608145
GCC 4.4 还有一个旧的未解决的错误报告:https: //bugs.launchpad.net/ubuntu/+source/gcc-4.4/+bug/608145
Update 19 Oct 2012(jogojapan): The issue has now been explained and resolved by Jonathan Wakely as an anwer to this question: What is _GLIBCXX_USE_NANOSLEEP all about?This is particularly relevant for anyone who builds GCC himself instead of using a ready-made package.
2012 年 10 月 19 日更新(jogojapan):Jonathan Wakely 现在已经解释并解决了这个问题,作为对这个问题的回答:_GLIBCXX_USE_NANOSLEEP 是什么?这对于那些自己构建 GCC 而不是使用现成包的人来说尤其重要。
采纳答案by Maister
Confirmed that it doesn't work here as well. (Recent GCC 4.6 snapshot).
确认它在这里也不起作用。(最近的 GCC 4.6 快照)。
You could do the obvious and simply define it before you include any std:: headers. A bit dirty but will work until GCC fixes it (unless this is intended behavior). The #define shouldn't break anything anyways. Either in source or -D_GLIBCXX_USE_NANOSLEEP flag to GCC.
在包含任何 std:: 标头之前,您可以做显而易见的事情并简单地定义它。有点脏,但在 GCC 修复它之前会起作用(除非这是预期的行为)。#define 无论如何都不应该破坏任何东西。在源代码或 GCC 的 -D_GLIBCXX_USE_NANOSLEEP 标志中。
You might want to try using -std=gnu++0x rather than -std=c++0x, since gnu++0x often pulls in stuff like this.
您可能想尝试使用 -std=gnu++0x 而不是 -std=c++0x,因为 gnu++0x 经常会引入这样的东西。
回答by steffen
Additional information, in case it helps someone:
附加信息,以防它对某人有帮助:
I do not need to define _GLIBCXX_USE_NANOSLEEP
in Ubuntu 11.10, gcc 4.6.1, glibc 2.13.
我不需要_GLIBCXX_USE_NANOSLEEP
在 Ubuntu 11.10、gcc 4.6.1、glibc 2.13 中定义。
But I doneed to compile with -D_GLIBCXX_USE_NANOSLEEP on Gentoo, gcc 4.6.1, glibc 2.12.2.
但我确实需要在 Gentoo、gcc 4.6.1、glibc 2.12.2 上使用 -D_GLIBCXX_USE_NANOSLEEP 进行编译。
I am not going to compile the Gentoo system for updating the glibc. At least not before the weekend ;)
我不打算编译 Gentoo 系统来更新 glibc。至少不是在周末之前;)
回答by bHymanfly
Seems to work without the define on ubuntu 13.04 using gcc version 4.7.3
似乎没有使用 gcc 版本 4.7.3 在 ubuntu 13.04 上定义的情况下工作
回答by mahbub_siddique
Need to define _GLIBCXX_USE_NANOSLEEP
on top of the source code.
需要_GLIBCXX_USE_NANOSLEEP
在源代码之上定义。
#define _GLIBCXX_USE_NANOSLEEP //add it top of c++ code
OR, Compile with following commamd:
或者,使用以下命令编译:
g++ a.cpp -o a -std=c++0x -D_GLIBCXX_USE_NANOSLEEP //compile c++ code
./a // run c++ code