更新 README.md

添加介绍和使用说明
master
极简美 2019-05-06 22:45:42 +08:00
parent 3f7f62a2d5
commit 8a5640fb1c
1 changed files with 97 additions and 12 deletions

109
README.md
View File

@ -1,23 +1,108 @@
# timer
# timerMT译为Multiple或Multi
#### 介绍
一个超级简介的Linux下的定时器
#### 软件架构
软件架构说明
一个Linux下的超级简洁的定时器++利用epoll机制和timerfd新特性实现的多重、多用、多个定时器实现++。只需要使用TIMER_CREATE()接口创建一个定时器实体,即**可向其添加成千上万个定时任务,定时任务可达到纳秒级别的精度,且可在同一时间点添加不同的定时任务!**。
#### 软件接口
#### 安装教程
整个定时器包含如下几类接口。
1. xxxx
2. xxxx
3. xxxx
1. **创建和声明定时器实例**使用定时器的第一步就是使用TIMER_CREATE()创建一个定时器实例在其它文件使用到该定时器时使用TIMER_DECLEAR()进行声明:
```
TIMER_CREATE(name);
TIMER_DECLEAR(name);
```
#### 使用说明
2. **初始化和反初始化定时器**在正式使用定时器之前首先使用TIMER_INIT()初始化前面创建的定时器实例name是实例名称max是创建定时器要检测的定时任务数量当不再使用定时器时可使用TIMER_DEINIT()反初始化定时器(退出定时器,并释放所有资源):
```
TIMER_INIT(name, max);
TIMER_DEINIT(name);
```
1. xxxx
2. xxxx
3. xxxx
3. **添加和删除定时任务**
```
TIMER_ADD(name, itimespec, repeat, cb, data);
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代表永远重复
- cb为定时任务超时回调函数其类型为void (*timer_callback_t)(void *data)
- data为定时任务回调函数的参数为void *类型,用户可指定为自己定义的结构体;
TIMER_ADD()添加定时任务成功返回新定时任务的文件描述符,失败返回 < 0TIMER_DEL()
4. **查询和清空定时器**
```
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;
}
```
#### 参与贡献