gpt4 book ai didi

c++ - boost 截止时间计时器在 iOS 中过期不准确

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:57:58 26 4
gpt4 key购买 nike

我正在使用非常简单的代码来测试 iOS 中的截止时间计时器

结果不准确:在桌面或模拟器中,结果将是 20 或 21,在移动设备中,结果将是 24、25,我想实现一个播放器库,所以 5ms 错误是 Not Acceptable 。

如何让 iOS 上的计时器更准确?

代码如下:

boost::thread*                                          _thread;
boost::asio::deadline_timer* _timer;
boost::asio::io_service _io_service;
boost::posix_time::ptime _lastTime;

void test()
{
_timer = new boost::asio::deadline_timer(_io_service);
_timer->expires_from_now(boost::posix_time::milliseconds(0));
_timer->async_wait(boost::bind(case1));
_thread = new boost::thread(boost::bind(&boost::asio::io_service::run, &_io_service));
}

void case1()
{
boost::posix_time::ptime currentTime = boost::posix_time::microsec_clock::local_time();

if (_lastTime.is_not_a_date_time() == false) {
boost::posix_time::time_duration diff = currentTime - _lastTime;
std::cout << "run time: " << diff.total_milliseconds() << std::endl;
}

_lastTime = boost::posix_time::microsec_clock::local_time();
_timer->expires_from_now(boost::posix_time::milliseconds(20));
_timer->async_wait(boost::bind(case1));
}

最佳答案

您可能想要使用 http://www.boost.org/doc/libs/1_56_0/doc/html/boost_asio/reference/high_resolution_timer.htmlchrono::high_resolution_clock 代替:

查看 Live On Coliru

输出是 run time: 20 无一异常(exception)。

#include <boost/asio.hpp>
#include <boost/chrono.hpp>
#include <boost/bind.hpp>
#include <boost/asio/high_resolution_timer.hpp>

typedef boost::chrono::high_resolution_clock hrc;
using boost::chrono::duration_cast;
using boost::chrono::milliseconds;

boost::asio::io_service io_service_;
boost::asio::high_resolution_timer timer_(io_service_);
hrc::time_point lastTime_ {};

void case1(boost::system::error_code ec)
{
hrc::time_point currentTime = hrc::now();

if (lastTime_.time_since_epoch().count()) {
hrc::duration diff = currentTime - lastTime_;
std::cout << "run time: " << duration_cast<milliseconds>(diff).count() << std::endl;
}

lastTime_ = hrc::now();
timer_.expires_from_now(milliseconds(20));
timer_.async_wait(boost::bind(case1, boost::asio::placeholders::error));
}

void test()
{
timer_.expires_from_now(milliseconds(0));
timer_.async_wait(boost::bind(case1, boost::asio::placeholders::error));
io_service_.run();
}

int main()
{
test();
}

关于c++ - boost 截止时间计时器在 iOS 中过期不准确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25967760/

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