`boost asio`中`deadline_timer`和`waitable_timer`之间的区别

时间:2014-10-22 07:00:38

标签: c++ boost timer boost-asio

要在5秒内使计时器到期,
这两者之间有什么实际区别吗? 在这种情况下,对于另一个人来说,是否优先(表现,资源等)?

[选项1] deadline_timer

boost::asio::deadline_timer timer(io_service);
timer.expires_from_now(boost::posix_time::seconds(5));

[选项2] waitable_timer system_timersteady_timer):

boost::asio::system_timer timer(io_service);
timer.expires_from_now(std::chrono::seconds(5));

PS:请集中精力比较deadline_timersystem_timer,而不是system_timersteady_timer

2 个答案:

答案 0 :(得分:10)

唯一的区别在于使用的时钟类型。

自Boost 1.56起,basic_deadline_timerbasic_waitable_timer都使用detail::deadline_timer_service

等待的方式没有区别,唯一的区别在于它如何进行时间计算。

wait()方法中,它使用Time_Traits::now()来检查是否需要等待更多。对于system_timer std::chrono::system_clock::now()deadline_timer boost::posix_time::microsec_clock::universal_time()boost::posix_time::second_clock::universal_time(),取决于高精度时钟的存在(请参阅time_traits.hpp)。

std::chrono::system_clock实现由编译器/标准库供应商提供,而boost::posix_time::*clock由Boost使用可用的系统函数实现。

这些实现当然可能具有不同的性能和/或精度,具体取决于平台和编译器。

答案 1 :(得分:2)

这里对类似问题有很好的回应:https://stackoverflow.com/a/16364002/3491378

基本上,主要区别在于,如果您希望对系统时钟进行任何外部更改,则应该使用steady_timer - deadline_timer延迟将受到这些更改的影响

相关问题