如何在c ++ 0x thread中使用wait_for

时间:2013-01-31 01:39:29

标签: c++ multithreading

在gcc 4.4.6,RHEL 6.3 linux上,我看到了线程头中的模板

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

但如果我尝试在我的应用程序中使用它,我会收到sleep_for is not a member of std::this_thread

的错误

我的申请很简单

#include <thread>
#include <chrono>

using namespace std;

int main()
{
  this_thread::sleep_for(chrono::seconds(1));
   return 0;
}

1 个答案:

答案 0 :(得分:6)

编译时将此参数添加到命令行:

-D_GLIBCXX_USE_NANOSLEEP

所以这样编译:

gcc -D_GLIBCXX_USE_NANOSLEEP test.cpp -o test

如果使用g ++,你可以使用

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