How to use wait_for in c ++ 0x thread

In gcc 4.4.6, RHEL 6.3 linux, I see a template in the stream header

template<typename _Rep, typename _Period>
  inline void
  sleep_for(const chrono::duration<_Rep, _Period>& __rtime)

But if I try to use it in my application, I get an error sleep_for is not a member of std::this_thread

My application is simple as

#include <thread>
#include <chrono>

using namespace std;

int main()
{
  this_thread::sleep_for(chrono::seconds(1));
   return 0;
}
+5
source share
1 answer

When compiling, add this parameter to your command line:

-D_GLIBCXX_USE_NANOSLEEP

So compile like this:

gcc -D_GLIBCXX_USE_NANOSLEEP test.cpp -o test

if you use g ++ you can use

g++ -std=gnu++0x -pthread test.cpp -o test
+6
source

All Articles