i007.cc

i007.cc

优先队列-降维打击

介绍一个方便好用的CronTab定时器

 

 

这是一个使用CronTab表达式的定时器,使用C++编写的,可以在指定时间点触发定时器事件,也可以在一段时间之后触发定时器事件。

特点:

  1. 对时间的表达能力强,毕竟CronTab表达式已经在Linux平台上广泛使用,久经考验。
  2. 使用方便,一个头文件搞定一切,拷贝过去就可以使用,不依赖第三方库,Windows、Centos、Ubuntu、Mac都可以运行。一行代码添加一个定时器,可传入成员函数,携带自定义参数。
  3. 精度高、误差不累积。
  4. 性能好,对于定时器内的对象个数,时间判断的时间复杂度做到了O(log(n))。
  5. 可以选择在主线程中也可以选择在子线程中触发定时器。

 

在主线程中触发定时器事件的例子:

void TestCronTimerInMainThread() {
    cron_timer::TimerMgr mgr;

    mgr.AddTimer("* * * * * *", [](void) {
        // 每秒钟都会执行一次
        Log("1 second cron timer hit");
    });

    mgr.AddTimer("0/3 * * * * *", [](void) {
        // 从0秒开始,每3秒钟执行一次
        Log("3 second cron timer hit");
    });

    mgr.AddTimer("0 * * * * *", [](void) {
        // 1分钟执行一次(每次都在0秒的时候执行)的定时器
        Log("1 minute cron timer hit");
    });

    mgr.AddTimer("15;30;33 * * * * *", [](void) {
        // 指定时间(15秒、30秒和33秒)点都会执行一次
        Log("cron timer hit at 15s or 30s or 33s");
    });

    mgr.AddTimer("40-50 * * * * *", [](void) {
        // 指定时间段(40到50内的每一秒)执行的定时器
        Log("cron timer hit between 40s to 50s");
    });

    auto timer = mgr.AddDelayTimer(3000, [](void) {
        // 3秒钟之后执行
        Log("3 second delay timer hit");
    });

    // 可以在执行之前取消
    timer->Cancel();

    mgr.AddDelayTimer(
        10000,
        [](void) {
            // 每10秒钟执行一次,总共执行3次
            Log("10 second delay timer hit");
        },
        3);
    Log("10 second delay timer added");

    while (!_shutDown) {
        auto nearest_timer =
            (std::min)(std::chrono::system_clock::now() + std::chrono::milliseconds(500), mgr.GetNearestTime());
        std::this_thread::sleep_until(nearest_timer);
        mgr.Update();
    }
}

 

代码在这里

https://github.com/williamxin/cron_timer

编程不易,如果你觉得好,请多关注点赞,谢谢!

 

发表回复