要在5秒内使计时器到期,
这两者之间有什么实际区别吗?
在这种情况下,对于另一个人来说,是否优先(表现,资源等)?
[选项1] deadline_timer
:
boost::asio::deadline_timer timer(io_service);
timer.expires_from_now(boost::posix_time::seconds(5));
[选项2] waitable_timer
(system_timer
或steady_timer
):
boost::asio::system_timer timer(io_service);
timer.expires_from_now(std::chrono::seconds(5));
PS:请集中精力比较deadline_timer
与system_timer
,而不是system_timer
与steady_timer
。
答案 0 :(得分:10)
唯一的区别在于使用的时钟类型。
自Boost 1.56起,basic_deadline_timer
和basic_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延迟将受到这些更改的影响