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):
$ 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
#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?
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.
There is also an old, unresolved bug report for 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.
See Question&Answers more detail:os