gpt4 book ai didi

android - Android 设备上的 Boost Asio deadline_timer 不会在到期时间触发,而是在稍后的时间触发

转载 作者:太空宇宙 更新时间:2023-11-03 10:56:01 25 4
gpt4 key购买 nike

我们正在尝试在 Android 设备上使用 boost asio deadline_timers。我们有一个要求,我们需要每 100 毫秒触发一个定时器。我使用了如下代码。但是,计时器似乎每 1 秒触发一次。我们应该在 Android 系统中进行哪些微调才能使 boost asio 截止时间计时器按预期工作?

注意:以下代码在普通 linux 系统上按预期工作。是什么让它在 Andorid 系统上的工作方式不同?

代码:

void print(asio::deadline_timer* ptimer, const asio::error_code& err)
{

struct timeval tval;

if(0 == gettimeofday(&tval, NULL))
{
std::cout <<" Timer... sec::microsec = "<<tval.tv_sec<<"::"<<tval.tv_usec<< std::endl;
}
else
{
std::cout <<" Timer... gettimeofday Error!" << std::endl;
}

ptimer->expires_from_now(boost::posix_time::milliseconds(100));
ptimer->async_wait(boost::bind(&print, ptimer, asio::placeholders::error));


}

Android 设备上的输出

Timer...  sec::microsec = 1298328679::39207
Timer... sec::microsec = 1298328680::46773
Timer... sec::microsec = 1298328681::54624
Timer... sec::microsec = 1298328682::63861
Timer... sec::microsec = 1298328683::65740
Timer... sec::microsec = 1298328684::69301
Timer... sec::microsec = 1298328685::76500
Timer... sec::microsec = 1298328686::85768

最佳答案

您可能想确保您的 Android 环境最终定义了BOOST_DATE_TIME_HAS_HIGH_PRECISION_CLOCK

如果您查看 boost/asio/time_traits.hpp 中的默认 timer_traits,您会发现如果未定义,asio 将使用 second_clock 作为它的计时器。

此定义来自 boost/date_time/compiler_config.hpp,以 BOOST_HAS_GETTIMEOFDAYBOOST_HAS_FTIME 为条件。鉴于您的示例,大概应该定义前者。

不知道是android算作自己的平台还是boost检测为linux。在 boost/config/platform/linux.hpp 中定义为:

//// If glibc is past version 2 then we definitely have// gettimeofday, earlier versions may or may not have it://#if defined(__GLIBC__) && (__GLIBC__ >= 2)#  define BOOST_HAS_GETTIMEOFDAY#endif

这可能是您要为 android 添加附加条件的地方。

关于android - Android 设备上的 Boost Asio deadline_timer 不会在到期时间触发,而是在稍后的时间触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4777998/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com