当前仓库属于关闭状态,部分功能使用受限,详情请查阅 仓库状态说明
1 Star 0 Fork 92

viyjk99 / mult_timer
关闭

forked from 极简美 / mult_timer 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

MT_Timer(MT译为Multiple或Multi)

一、介绍

一个Linux下的超级简洁的定时器:利用epoll机制和timerfd新特性实现的多重、多用、多个定时任务实现。只需要使用TIMER_CREATE()接口创建一个定时器实体,即可向其添加成千上万个定时任务,定时任务可达到纳秒级别的精度,且可在同一时间点添加不同的定时任务!

二、软件接口

整个定时器包含如下几类接口。

  1. 创建和声明定时器实例:使用定时器的第一步就是使用TIMER_CREATE()创建一个定时器实例,在其它文件使用到该定时器时,使用TIMER_DECLEAR()进行声明:
TIMER_CREATE(name);
TIMER_DECLEAR(name);
  1. 初始化和反初始化定时器:在正式使用定时器之前,首先使用TIMER_INIT()初始化前面创建的定时器实例,name是实例名称,max是创建定时器要检测的定时任务数量;当不再使用定时器时,可使用TIMER_DEINIT()反初始化定时器(退出定时器,并释放所有资源):
TIMER_INIT(name, max);
TIMER_DEINIT(name);
  1. 添加和删除定时任务
TIMER_ADD(name, itimespec, repeat, cb, data, rb);
TIMER_DEL(name, timerfd);

TIMER_ADD()用于向定时器实例name中添加一个定时任务,其参数描述如下:

  • ittimespec是定时任务的定时时间和循环时间,其结构体类型如下:
struct timespec {
    time_t tv_sec;  // seconds
    long   tv_nsec; // nanoseconds
};
struct itimerspec {
    struct timespec it_value;
    struct timespec it_interval;
};

其中it_value即是超时时间(相对时间),若想定义周期定时任务,则设置it_interval成员;若不想定义周期定时任务,则需设置it_interval成员都为0。因此,第一次超时和后面周期定时任务是可以使用不同时间的。

  • repeat是周期定时任务的重复次数,若设置为**-1,代表永远重复;0,代表一次都不执行**;因此repeat应至少为1,或者使用-1;
  • cb为定时任务超时回调函数,其类型为:void (*timer_callback_t)(void *data);
  • data为定时任务回调函数的参数,为void *类型,用户可指定为自己定义的结构体;

TIMER_ADD()添加定时任务成功返回新定时任务的文件描述符,失败返回 < 0。返回的文件描述符,可用于在TIMER_DEL()中删除定时任务。

  1. 查询和清空定时任务
TIMER_COUNT(name);
TIMER_CLEAR(name);

TIMER_COUNT(name)用于查询定时器实例name中现存的定时任务个数;TIMER_CLEAR(name)用于清空定时器实例name中的所有定时任务。

三、使用实例

下面是一个非常简单的使用示例:共创建了两个定时任务,每个第一次超时都是3S,后面每隔1S超时一次;但第一个定时任务频次为8,第二个定时任务频次为3;当所有定时任务都超时后,输入回车即可退出:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "mt_timer.h"

void timeout_handle(void *arg)
{
    printf("[%ld]:timeout1\n", time(NULL));
}

void timeout_handler(void *arg)
{
    printf("[%ld]:timeout2\n", time(NULL));
}

TIMER_CREATE(test);

int main(void)
{
    int timer;
    struct itimerspec itimespec;

    TIMER_INIT(test, 10);
    itimespec.it_value.tv_sec = 3;
    itimespec.it_value.tv_nsec = 0;
    itimespec.it_interval.tv_sec = 1;
    itimespec.it_interval.tv_nsec = 0;
    
    timer = TIMER_ADD(test, &itimespec, 8, timeout_handle, NULL);
    TIMER_ADD(test, &itimespec, 3, timeout_handler, NULL);
    printf("[%ld]:timer_add : %d\n", time(NULL), TIMER_COUNT(test));
    
    sleep(4);//getchar();
    TIMER_DEL(test, timer);
    printf("[%ld]:timer_del : %d\n", time(NULL), TIMER_COUNT(test));
    TIMER_CLEAR(test);
    printf("[%ld]:timer_clear : %d\n", time(NULL), TIMER_COUNT(test));
    getchar();

    TIMER_DEINIT(test);
    
    return 0;
}

四、赞赏作者

image

五、参与贡献

  1. Fork 本仓库
  2. 新建 Feat_xxx 分支
  3. 提交代码
  4. 新建 Pull Request

六、码云特技

  1. 使用 Readme_XXX.md 来支持不同的语言,例如 Readme_en.md, Readme_zh.md
  2. 码云官方博客 blog.gitee.com
  3. 你可以 https://gitee.com/explore 这个地址来了解码云上的优秀开源项目
  4. GVP 全称是码云最有价值开源项目,是码云综合评定出的优秀开源项目
  5. 码云官方提供的使用手册 https://gitee.com/help
  6. 码云封面人物是一档用来展示码云会员风采的栏目 https://gitee.com/gitee-stars/
MIT License Copyright (c) 2019 极简美 & konishi5202@163.com Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

一个Linux下的超级精简的多重定时器:可实现成千上万个定时任务,定时精度可达纳秒级别,且同一时间点可添加不同的任务!适用于云后台服务和嵌入式等各种环境。 展开 收起
C
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
C
1
https://gitee.com/david2lf/mult_timer.git
git@gitee.com:david2lf/mult_timer.git
david2lf
mult_timer
mult_timer
master

搜索帮助

14c37bed 8189591 565d56ea 8189591