美文网首页
实现准确的定时器

实现准确的定时器

作者: natewang | 来源:发表于2018-08-11 20:49 被阅读20次

1、纳秒级精度的Timer

#include <mach/mach.h>
#include <mach/mach_time.h>

static const uint64_t NANOS_PER_USEC = 1000ULL;
static const uint64_t NANOS_PER_MILLISEC = 1000ULL * NANOS_PER_USEC;
static const uint64_t NANOS_PER_SEC = 1000ULL * NANOS_PER_MILLISEC;

static mach_timebase_info_data_t timebase_info;

static uint64_t abs_to_nanos(uint64_t abs) {
    return abs * timebase_info.numer  / timebase_info.denom;
}

static uint64_t nanos_to_abs(uint64_t nanos) {
    return nanos * timebase_info.denom / timebase_info.numer;
}

void example_mach_wait_until(int argc, const char * argv[]) {
    mach_timebase_info(&timebase_info);
    uint64_t time_to_wait = nanos_to_abs(10ULL * NANOS_PER_SEC);
    uint64_t now = mach_absolute_time();
    mach_wait_until(now + time_to_wait);
}

2、后台开一个常驻线程,无繁重任务,开启一个NSTimer。

相关文章

  • 实现准确的定时器

    1、纳秒级精度的Timer 2、后台开一个常驻线程,无繁重任务,开启一个NSTimer。

  • Golang-基于TimeingWheel定时器

    设计思路 在linux下实现定时器主要有如下方式 基于链表实现定时器 基于排序链表实现定时器 基于最小堆实现定时器...

  • 【iOS】使用GCD创建定时器

    GCD中的定时器是靠 Dispatch Source 来实现的 优点 时间准确 可以使用子线程,解决定时间跑在主线...

  • iOS三大定时器:NSTimer、CADisplayLink、G

    一、介绍NSTimer:基于Runloop实现的定时器CADisplayLink:基于Runloop实现的定时器,...

  • GCD定时器

    NSTimer 定时器易受 RunLoop模式影响导致定时器不准确。 dispatch_source_t time...

  • 踩坑NSTimer

    0.目录 概论,非主线程定时器导致的问题,定时器在界面滑动时候失效,定时器的准确性,定时器中的强引用。 1.概论 ...

  • Linux 设备驱动之内核定时器 2020-02-20

    该内核定时器的实现是基于低精度定时器实现,高精度定时器的实现代码更为复杂,将在其他章节做相应介绍struct ti...

  • Linux C/C++定时器的实现原理和使用方法

    定时器的实现原理 定时器的实现依赖的是CPU时钟中断,时钟中断的精度就决定定时器精度的极限。一个时钟中断源如何实现...

  • 2017.12.21学习总结

    下午学习了定时器,定时器分为高级定时器、通用定时器和基本定时器,我们主要研究通用定时器。 定时器中断实现步骤:...

  • gcd 定时器

    GCD定时器的实现

网友评论

      本文标题:实现准确的定时器

      本文链接:https://www.haomeiwen.com/subject/toxjbftx.html