vmr*_*rob 4 c++ time c++11 c++-chrono
我还是很新的库和我在std :: chrono上找到的文档对我不起作用.
我正在尝试实现一个包含时间戳的对象容器.对象将按照从最近到最近的顺序存储,我决定尝试使用std :: chrono :: time_point来表示每个时间戳.处理数据的线程将定期唤醒,处理数据,查看何时需要再次唤醒然后在该持续时间内休眠.
static std::chrono::time_point<std::chrono::steady_clock, std::chrono::milliseconds> _nextWakeupTime;
Run Code Online (Sandbox Code Playgroud)
我的印象是上面的声明使用了具有毫秒精度的替代时钟.
下一步是将_nextWakeupTime设置为now的表示;
_nextWakeupTime = time_point_cast<milliseconds>(steady_clock::now());
Run Code Online (Sandbox Code Playgroud)
该行不会编译:
error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::chrono::time_point<_Clock,_Duration>' (or there is no acceptable conversion)
with
[
_Clock=std::chrono::system_clock,
_Duration=std::chrono::milliseconds
]
chrono(298): could be 'std::chrono::time_point<_Clock,_Duration> &std::chrono::time_point<_Clock,_Duration>::operator =(const std::chrono::time_point<_Clock,_Duration> &)'
with
[
_Clock=std::chrono::steady_clock,
_Duration=std::chrono::milliseconds
]
while trying to match the argument list '(std::chrono::time_point<_Clock,_Duration>, std::chrono::time_point<_Clock,_Duration>)'
with
[
_Clock=std::chrono::steady_clock,
_Duration=std::chrono::milliseconds
]
and
[
_Clock=std::chrono::system_clock,
_Duration=std::chrono::milliseconds
]
Run Code Online (Sandbox Code Playgroud)
我知道在Windows系统上,stead_clock与system_clock是一样的,但我不知道这里发生了什么.我知道我可以这样做:
_nextWakeupTime += _nextWakeupTime.time_since_epoch();
Run Code Online (Sandbox Code Playgroud)
我觉得这不是我应该做的很好的代表.
同样,实例化给定时钟/持续时间的time_point对象并将其设置为现在的最佳方法是什么?
你最容易做的就是给出_nextWakeupTime类型steady_clock::time_point.
steady_clock::time_point _nextWakeupTime;
Run Code Online (Sandbox Code Playgroud)
你可以查询它的分辨率time_point是什么steady_clock::time_point::period,在哪里给你一个std::ratio静态成员num和den.
typedef steady_clock::time_point::period resolution;
cout << "The resolution of steady_clock::time_point is " << resolution::num
<< '/' <<resolution::den << " of a second.\n";
Run Code Online (Sandbox Code Playgroud)
从您的错误消息中可以看出您的供应商已经制作system_clock::time_point并且steady_clock::time_point相同time_point,因此他们共享相同的时期,您可以将两者混合在算术中.为了便于处理这种情况,您可以使用以下命令查询time_point时钟:
time_point::clock
Run Code Online (Sandbox Code Playgroud)
即你的实施steady_clock::time_point::clock不是steady_clock但是system_clock.如果你真的想要一个time_point兼容steady_clock::time_point但具有毫秒分辨率的你可以这样做:
time_point<steady_clock::time_point::clock, milliseconds> _nextWakeupTime;
Run Code Online (Sandbox Code Playgroud)