如何在 C++0X 线程中使用wait_for

how to use wait_for in c++0x thread

本文关键字:wait for C++0X 线程      更新时间:2023-10-16

在 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;
}

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

-D_GLIBCXX_USE_NANOSLEEP

所以像这样编译:

gcc -D_GLIBCXX_USE_NANOSLEEP test.cpp -o test

如果使用 G++,您可以使用

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