【发布时间】:2020-10-28 14:24:30
【问题描述】:
考虑以下代码:
using namespace std;
using namespace std::chrono;
typedef std::intmax_t intmax_type;
steady_clock::time_point start = steady_clock::now();
this_thread::sleep_for(chrono::milliseconds(25000));
steady_clock::duration dur = steady_clock::now() - start;
intmax_type desired_dur = duration_cast<milliseconds>(dur).count();
if(desired_dur < intmax_type(25000))
std::cout << "WTF happend there?" << std::endl;
根据标准,std::this_thread::sleep_for(sleep_duration) 可能会因为调度或资源争用延迟而阻塞比sleep_duration 更长的时间,但至少它会阻塞指定sleep_duration 的线程执行。
可能存在线程实际休眠指定持续时间但由于std::chrono::steady_clock或std::chrono::system_clock使用与sleep_for实现不同的操作系统时钟(不同粒度fe)时间段测量给我们的结果与实际休眠时间不同的情况是。
我的问题是:
C++11 标准是否禁止条件if(desired_dur < intmax_type(25000)) 发生?如果有,请提供准确的报价。
【问题讨论】:
标签: c++ c++11 language-lawyer