我正在使用c ++ 11 <chrono>并且有一些秒表示为double.我想在这段时间内使用c ++ 11来休眠,但我无法理解如何将它转换为需要的std::chrono::duration对象std::this_thread::sleep_for.
const double timeToSleep = GetTimeToSleep();
std::this_thread::sleep_for(std::chrono::seconds(timeToSleep)); // cannot convert from double to seconds
Run Code Online (Sandbox Code Playgroud)
我已经锁定了<chrono>参考,但我发现它相当令人困惑.
谢谢
编辑:
以下给出了错误:
std::chrono::duration<double> duration(timeToSleep );
std::this_thread::sleep_for(duration);
Run Code Online (Sandbox Code Playgroud)
错误:
:\program files (x86)\microsoft visual studio 11.0\vc\include\chrono(749): error C2679: binary '+=' : no operator found which takes a right-hand operand of type 'const std::chrono::duration<double,std::ratio<0x01,0x01>>' (or there is no acceptable conversion)
2> c:\program files (x86)\microsoft visual studio 11.0\vc\include\chrono(166): could be 'std::chrono::duration<__int64,std::nano> &std::chrono::duration<__int64,std::nano>::operator +=(const std::chrono::duration<__int64,std::nano> &)'
2> while …Run Code Online (Sandbox Code Playgroud) std::chrono::time_point::time_since_epoch()返回a duration,参考time_point过去的一些.什么时候是这样的time_point?它取决于C++实现还是由C++标准定义的?或者它是将事件设定为UTC 1970年1月1日的事实上的标准?
该<chrono>库允许日期默默地陷入 !ok() 状态。例如:
#include <chrono>
#include <iostream>
int
main()
{
using namespace std;
using namespace chrono;
auto date = 2023y/October/31;
cout << date.ok() << '\n';
date += months{1};
cout << date.ok() << '\n';
}
Run Code Online (Sandbox Code Playgroud)
输出:
1
0
Run Code Online (Sandbox Code Playgroud)
我知道 10 月 31 日是有效日期,而 11 月 31 日则不是。但为什么 11 月 31 日不是一个错误(断言或抛出)?或者为什么它不像其他日期库那样回滚到 11 月 30 日,或者滚动到 12 月 1 日?
让11月31日默默存在岂不是容易出错?
boost :: date_time和std :: chrono的互操作性如何?
例如,有没有办法在boost :: posix_time :: ptime和std :: chrono :: time_point之间进行转换?
我尝试搜索有关此类转换的文档,但找不到任何内容.
C++ 11定义high_resolution_clock并且它具有成员类型period和rep.但我无法弄清楚如何才能获得该时钟的精度.
或者,如果我可能达不到精度,我能以某种方式至少得到一个在纳秒之间的最小可表示持续时间的计数吗?可能用period?
#include <iostream>
#include <chrono>
void printPrec() {
std::chrono::high_resolution_clock::rep x = 1;
// this is not the correct way to initialize 'period':
//high_resolution_clock::period y = 1;
std::cout << "The smallest period is "
<< /* what to do with 'x' or 'y' here? */
<< " nanos\n";
}
Run Code Online (Sandbox Code Playgroud) 在C++ 11中是否有一种简单的方法可以使用与正在使用的ostream相关联的语言环境的相应格式规则来打印当前的挂起时间?
我真正想做的是这样的事情:
myStream << std::chrono::system_clock::now();
Run Code Online (Sandbox Code Playgroud)
并根据与之相关的任何区域设置打印日期和时间myStream.C++ 11提供put_time,但它需要格式化字符串,我希望格式由与流关联的语言环境确定.还有time_put和time_put_byname,但基于在cppreference.com的例子中,这些功能配合使用put_time.
有没有简单的方法来打印时间点值而不手动格式化它?
在C++中编写一个以超时作为其参数之一的函数时,我应该使用什么类型的超时参数本身?
这种功能的一个例子可能是:
void my_function(bool work_really_hard, timeout_type timeout)
{
// Do stuff, until timeout is reached.
}
Run Code Online (Sandbox Code Playgroud)
我曾考虑过使用std::chrono::secondsfor timeout_type,但是不允许在sub-second领域使用任何超时.
std::chrono::nanoseconds相反,使用时,指定10分钟是很麻烦的.
有什么办法以合理的方式解决这个问题,同时保持功能签名和调用整洁简单?
我正在尝试使用带有计时和线程的C++来限制执行交叉检查的循环中的每秒帧数.
这是我的代码:
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
std::chrono::system_clock::time_point lastFrame = std::chrono::system_clock::now();
while (true)
{
// Maintain designated frequency of 5 Hz (200 ms per frame)
now = std::chrono::system_clock::now();
std::chrono::duration<double, std::milli> delta = now - lastFrame;
lastFrame = now;
if (delta.count() < 200.0)
{
std::chrono::duration<double, std::milli> delta_ms(200.0 - delta.count());
auto delta_ms_duration = std::chrono::duration_cast<std::chrono::milliseconds>(delta_ms);
std::this_thread::sleep_for(std::chrono::milliseconds(delta_ms_duration.count()));
}
printf("Time: %f \n", delta.count());
// Perform intersection test
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是delta的每个其他输出显示的是微不足道的数量,而不是我想要的~200 ms /帧:
Time: 199.253200
Time: 2.067700
Time: 199.420400
Time: 2.408100
Time: 199.494200
Time: 2.306200
Time: 199.586800
Time: …Run Code Online (Sandbox Code Playgroud) 在 MSVC chrono 实现中我看到以下代码
_EXPORT_STD template <int = 0>
_NODISCARD constexpr year_month operator+(const year_month& _Left, const months& _Right) noexcept {
const auto _Mo = static_cast<long long>(static_cast<unsigned int>(_Left.month())) + (_Right.count() - 1);
const auto _Div = (_Mo >= 0 ? _Mo : _Mo - 11) / 12;
return year_month{_Left.year() + years{_Div}, month{static_cast<unsigned int>(_Mo - _Div * 12 + 1)}};
}
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下为什么它使用带有未命名参数的模板吗?