简体   繁体   English

std :: this_thread :: sleep_for()和GCC

[英]std::this_thread::sleep_for() and GCC

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? 为什么没有定义_GLIBCXX_USE_NANOSLEEP
How do I get this example to compile? 如何编译这个例子?


Update 17 Sep 2012 (jogojapan): I ran into this same problem today, using GCC 4.7.1. 2012年9月17日更新 (jogojapan):今天我使用GCC 4.7.1遇到了同样的问题。 I wonder if there is any news on how to avoid it, other than by defining _GLIBCXX_USE_NANOSLEEP . 我想知道是否有关于如何避免它的消息,除了定义_GLIBCXX_USE_NANOSLEEP I tried using -std=gnu11 , but to no avail. 我尝试使用-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? 2012年10月19日更新 (jogojapan):现在,Jonathan Wakely已经解决并解决了这个问题,作为这个问题的答案: _GLIBCXX_USE_NANOSLEEP到底是什么? This is particularly relevant for anyone who builds GCC himself instead of using a ready-made package. 这对于自己构建GCC而不是使用现成包的任何人来说尤为重要。

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. 在包含任何std :: headers之前,您可以做到显而易见并简单地定义它。 A bit dirty but will work until GCC fixes it (unless this is intended behavior). 有点脏,但会工作,直到GCC修复它(除非这是预期的行为)。 The #define shouldn't break anything anyways. #define不应该破坏任何东西。 Either in source or -D_GLIBCXX_USE_NANOSLEEP flag to GCC. 在源代码或-D_GLIBCXX_USE_NANOSLEEP标志到GCC。

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经常会引入这样的东西。

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. 我不需要在Ubuntu 11.10,gcc 4.6.1,glibc 2.13中定义_GLIBCXX_USE_NANOSLEEP

But I do need 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. 我不打算编译Gentoo系统来更新glibc。 At least not before the weekend ;) 至少不是在周末之前;)

似乎没有使用gcc版本4.7.3在ubuntu 13.04上定义工作

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: 或者,使用以下commamd编译:

g++ a.cpp -o a -std=c++0x -D_GLIBCXX_USE_NANOSLEEP    //compile c++ code
./a       // run c++ code

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM