网站app开发费用,漳州网站开发,汉中网站建设公司,做淘宝链接的网站文章目录 一、为什么要设计时间轮#xff1f;#xff08;一#xff09;简单的秒级定时任务实现#xff1a;#xff08;二#xff09;Linux提供给我们的定时器#xff1a;1.原型2.例子 二、时间轮#xff08;一#xff09;思想#xff08;一#xff09;代码 一、为什… 文章目录 一、为什么要设计时间轮一简单的秒级定时任务实现二Linux提供给我们的定时器1.原型2.例子 二、时间轮一思想一代码 一、为什么要设计时间轮
一简单的秒级定时任务实现
在当前的高并发服务器中我们不得不考虑⼀个问题那就是连接的超时关闭问题。我们需要避免⼀个连接长时间不通信但是也不关闭空耗资源的情况。 这时候我们就需要⼀个定时任务定时的将超时过期的连接进行释放。
二Linux提供给我们的定时器
1.原型
#include sys/timerfd.h
int timerfd_create(int clockid, int flags);clockid: CLOCK_REALTIME-系统实时时间如果修改了系统时间就会出问题 CLOCK_MONOTONIC-从开机到现在的时间是⼀种相对时间flags: 0-默认阻塞属性int timerfd_settime(int fd, int flags, struct itimerspec *new, struct itimerspec *old);fd: timerfd_create返回的⽂件描述符flags: 0-相对时间 1-绝对时间默认设置为0即可.new ⽤于设置定时器的新超时时间old ⽤于接收原来的超时时间struct timespec {time_t tv_sec; /* Seconds */long tv_nsec; /* Nanoseconds */
};struct itimerspec {struct timespec it_interval; /* 第⼀次之后的超时间隔时间 */struct timespec it_value; /* 第⼀次超时时间 */
};
定时器会在每次超时时⾃动给fd中写⼊8字节的数据表⽰在上⼀次读取数据到当前读取数据期间超时了多少次。2.例子
#include iostream
#include cstdio
#include string
#include ctime
#include cstdlib
#include unistd.h
#include sys/timerfd.h
#include sys/select.h
int main()
{
/*创建⼀个定时器 */
int timerfd timerfd_create(CLOCK_MONOTONIC, 0);struct itimerspec itm;itm.it_value.tv_sec 3;//设置第⼀次超时的时间itm.it_value.tv_nsec 0;itm.it_interval.tv_sec 3;//第⼀次超时后每隔多⻓时间超时itm.it_interval.tv_nsec 0;timerfd_settime(timerfd, 0, itm, NULL);//启动定时器/*这个定时器描述符将每隔三秒都会触发⼀次可读事件*/time_t start time(NULL);while(1) {uint64_t tmp;/*需要注意的是定时器超时后,则描述符触发可读事件必须读取8字节的数据保存的是⾃上*/int ret read(timerfd, tmp, sizeof(tmp));if (ret 0) {return -1;}std::cout tmp time(NULL) - start std::endl;}close(timerfd);return 0;}二、时间轮
一思想
上述的例子存在⼀个很大的问题每次超时都要将所有的连接遍历一遍如果有上万个连接效率无疑是较为低下的。 这时候大家就会想到我们可以针对所有的连接根据每个连接最近⼀次通信的系统时间建立⼀个小根堆这样只需要每次针对堆顶部分的连接逐个释放直到没有超时的连接为止这样也可以大大提高处理的效率。 上述方法可以实现定时任务但是这里给大家介绍另⼀种方案时间轮 时间轮的思想来源于钟表如果我们定了⼀个3点钟的闹铃则当时针走到3的时候就代表时间到了。
同样的道理如果我们定义了一个数组并且有一个指针指向数组起始位置这个指针每秒钟向后走动一步走到哪里则代表哪里的任务该被执行了那么如果我们想要定一个3s后的任务则只需要将任务添加到tick3位置则每秒中走一步三秒钟后tick走到对应位置这时候执行对应位置的任务即可。 但是同一时间可能会有大批量的定时任务因此我们可以给数组对应位置下拉一个数组这样就可以在同一个时刻上添加多个定时任务了。
一代码
#include iostream
#include list
#include vector
#include unordered_set
#include memory
#include cassert
#include unistd.h
#include functional/*定时任务类*/
using TaskFunc std::functionvoid();
// 它是一个使用 std::function 模板类实现的函数指针。
//这里的函数指针是指可以指向任意函数的指针类型其参数类型为 void()表示该函数不接受任何参数返回类型为 void。
using ReleaseFunc std::functionvoid(); class TimeTask {private:uint64_t _id; // 定时器任务对象uint64_t _timeout; // 定时任务的超时时间bool _canceled; // false-表示没有被取消 true-表示被取消TaskFunc _task_cb; // 定时器对象要执行的定时任务ReleaseFunc _release; //用于删除TimerWheel中保存的定时器对象信息public:// 1.构造函数TimeTask(uint64_t id,uint32_t delay,const TaskFunc cb) : _id(id),_timeout(delay),_task_cb(cb) {}// 2.析构函数~TimerTask() { if (_canceled false) _task_cb(); _release(); }void Cancel() { _canceled true; }void SetRelease(const ReleaseFunc cb) { _release cb; }uint32_t DelayTime() { return _timeout; }
};class TimeWheel {private:using WeakTask std::weak_ptrTimeTask; // std::weak_ptr 是 C11 标准库中引入的一种智能指针// 它提供了对指针所指向对象的弱引用。当弱引用超出作用域或者对象被销毁时// 智能指针会自动设置为 nullptr从而避免了悬空指针dangling pointer的问题。using PtrTask std::share_ptrTimeTask;std::vectorstd::vectorPtrTask _wheel;int _tick; // 当前的秒针int _capacity; // 表盘最大数量 ——其实就是最大延迟时间std::unordered_mapuint64_t,WeakTask _timers;private:void RomoveTimer(uint64_t id) {auto it _timers.find(id);if (it ! _timers.find(id)) {_timers.arase(it);}}public:Wheel() _capacity(60),_tick(0),_wheel(_capacity) {}}
#include iostream
#include vector
#include unordered_map
#include cstdint
#include functional
#include memory
#include unistd.husing TaskFunc std::functionvoid();
using ReleaseFunc std::functionvoid();
class TimerTask{private:uint64_t _id; // 定时器任务对象IDuint32_t _timeout; //定时任务的超时时间bool _canceled; // false-表示没有被取消 true-表示被取消TaskFunc _task_cb; //定时器对象要执行的定时任务ReleaseFunc _release; //用于删除TimerWheel中保存的定时器对象信息public:TimerTask(uint64_t id, uint32_t delay, const TaskFunc cb): _id(id), _timeout(delay), _task_cb(cb), _canceled(false) {}~TimerTask() { if (_canceled false) _task_cb(); _release(); }void Cancel() { _canceled true; }void SetRelease(const ReleaseFunc cb) { _release cb; }uint32_t DelayTime() { return _timeout; }
};class TimerWheel {private:using WeakTask std::weak_ptrTimerTask;using PtrTask std::shared_ptrTimerTask;int _tick; //当前的秒针走到哪里释放哪里释放哪里就相当于执行哪里的任务int _capacity; //表盘最大数量---其实就是最大延迟时间std::vectorstd::vectorPtrTask _wheel;std::unordered_mapuint64_t, WeakTask _timers;private:void RemoveTimer(uint64_t id) {auto it _timers.find(id);if (it ! _timers.end()) {_timers.erase(it);}}public:TimerWheel():_capacity(60), _tick(0), _wheel(_capacity) {}void TimerAdd(uint64_t id, uint32_t delay, const TaskFunc cb) {PtrTask pt(new TimerTask(id, delay, cb));pt-SetRelease(std::bind(TimerWheel::RemoveTimer, this, id));int pos (_tick delay) % _capacity;_wheel[pos].push_back(pt);_timers[id] WeakTask(pt);}//刷新/延迟定时任务void TimerRefresh(uint64_t id) {//通过保存的定时器对象的weak_ptr构造一个shared_ptr出来添加到轮子中auto it _timers.find(id);if (it _timers.end()) {return;//没找着定时任务没法刷新没法延迟}PtrTask pt it-second.lock();//lock获取weak_ptr管理的对象对应的shared_ptrint delay pt-DelayTime();int pos (_tick delay) % _capacity;_wheel[pos].push_back(pt);}void TimerCancel(uint64_t id) {auto it _timers.find(id);if (it _timers.end()) {return;//没找着定时任务没法刷新没法延迟}PtrTask pt it-second.lock();if (pt) pt-Cancel();}//这个函数应该每秒钟被执行一次相当于秒针向后走了一步void RunTimerTask() {_tick (_tick 1) % _capacity;_wheel[_tick].clear();//清空指定位置的数组就会把数组中保存的所有管理定时器对象的shared_ptr释放掉}
};class Test {public:Test() {std::cout 构造 std::endl;}~Test() {std::cout 析构 std::endl;}
};void DelTest(Test *t) {delete t;
}int main()
{TimerWheel tw;Test *t new Test();tw.TimerAdd(888, 5, std::bind(DelTest, t));for(int i 0; i 5; i) {sleep(1);tw.TimerRefresh(888);//刷新定时任务tw.RunTimerTask();//向后移动秒针std::cout 刷新了一下定时任务重新需要5s中后才会销毁\n;}tw.TimerCancel(888);while(1) {sleep(1);std::cout -------------------\n;tw.RunTimerTask();//向后移动秒针}return 0;
}一个时间轮写的我都要痛苦死了。。。 呜呜呜呜谁能救救我。。。。。。。。