【问题标题】:How to let this_thread::sleep_for(a while)? [duplicate]如何让 this_thread::sleep_for(a while)? [复制]
【发布时间】:2013-01-04 18:58:06
【问题描述】:

可能重复:
std::this_thread::sleep_for() and GCC

尝试编写一个简单的计时器和一些代码来测试它,让程序休眠一段时间:

#include <chrono>
#include <thread>

Aux::Timer timer;
timer.start();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
timer.stop();
std::chrono::duration<int64_t, std::milli> t = timer.elapsed();

这给了我一个编译器错误:

17:45:28 **** Incremental Build of configuration EnsembleClustering-DPar for project EnsembleClustering ****
make all 
Building file: ../src/aux/test/AuxGTest.cpp
Invoking: GCC C++ Compiler
/usr/local/bin/g++-4.7 -I/usr/local/Cellar/gcc/4.7.2/gcc/include/c++/4.7.2 -I/Users/cls/workspace/gtest/include -I/usr/local/Cellar/log4cxx/0.10.0/include -I/Users/cls/workspace/STINGER/include -O0 -g3 -D_GLIBCXX_DEBUG -p -pg -Wall -c -fmessage-length=0 -std=c++11 -g -pg -fopenmp -MMD -MP -MF"src/aux/test/AuxGTest.d" -MT"src/aux/test/AuxGTest.d" -o "src/aux/test/AuxGTest.o" "../src/aux/test/AuxGTest.cpp"
../src/aux/test/AuxGTest.cpp: In member function 'virtual void AuxGTest_testTimer_Test::TestBody()':
../src/aux/test/AuxGTest.cpp:72:2: error: 'sleep_for' is not a member of 'std::this_thread'
make: *** [src/aux/test/AuxGTest.o] Error 1

我之前没有使用过&lt;thread&gt; 库。你能建议一个快速修复吗?我也很高兴有一个替代方法来测试计时器的正确性。

【问题讨论】:

  • 这看起来非常正确。你的标准库实现是否真的有sleep_for?看来这就是错误所在。
  • @Dave 确实如此,并且以_GLIBCXX_USE_NANOSLEEP 为条件。
  • 在文件开头使用#define _GLIBCXX_USE_NANOSLEEP 1 对其进行了修复,如@Ajay 链接的答案中所述
  • 查看stackoverflow.com/a/12961816/981959以获得正确的解释

标签: c++ c++11 sleep stdthread


【解决方案1】:

修复了此处描述的问题:std::this_thread::sleep_for() and GCC

文件开头带有#define _GLIBCXX_USE_NANOSLEEP 1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-15
    • 2011-05-25
    • 1970-01-01
    • 2020-10-19
    • 1970-01-01
    • 2020-10-07
    • 2021-12-25
    • 1970-01-01
    相关资源
    最近更新 更多