当前位置: 首页 > news >正文

门户网站建设意义艾滋病多长时间能查出来

门户网站建设意义,艾滋病多长时间能查出来,网页设计和制作的三大步骤,利辛网站建设文章目录 一、为什么要设计时间轮?(一)简单的秒级定时任务实现:(二)Linux提供给我们的定时器:1.原型2.例子 二、时间轮(一)思想(一)代码 一、为什…

文章目录

  • 一、为什么要设计时间轮?
    • (一)简单的秒级定时任务实现:
    • (二)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后的任务,则只需要将任务添加到tick+3位置,则每秒中走一步,三秒钟后tick走到对应位置,这时候执行对应位置的任务即可。
但是,同一时间可能会有大批量的定时任务,因此我们可以给数组对应位置下拉一个数组,这样就可以在同一个时刻上添加多个定时任务了。
在这里插入图片描述

(一)代码

#include <iostream>
#include <list>
#include <vector>
#include <unordered_set>
#include <memory>
#include <cassert>
#include <unistd.h>
#include <functional>/*定时任务类*/
using TaskFunc = std::function<void()>;
// 它是一个使用 std::function 模板类实现的函数指针。
//这里的函数指针是指可以指向任意函数的指针类型,其参数类型为 void(),表示该函数不接受任何参数,返回类型为 void。
using ReleaseFunc = std::function<void()>; 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_ptr<TimeTask>; // std::weak_ptr 是 C++11 标准库中引入的一种智能指针,// 它提供了对指针所指向对象的弱引用。当弱引用超出作用域或者对象被销毁时,// 智能指针会自动设置为 nullptr,从而避免了悬空指针(dangling pointer)的问题。using PtrTask = std::share_ptr<TimeTask>;std::vector<std::vector<PtrTask>> _wheel;int _tick; // 当前的秒针int _capacity; // 表盘最大数量 ——其实就是最大延迟时间std::unordered_map<uint64_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.h>using TaskFunc = std::function<void()>;
using ReleaseFunc = std::function<void()>;
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_ptr<TimerTask>;using PtrTask = std::shared_ptr<TimerTask>;int _tick;      //当前的秒针,走到哪里释放哪里,释放哪里,就相当于执行哪里的任务int _capacity;  //表盘最大数量---其实就是最大延迟时间std::vector<std::vector<PtrTask>> _wheel;std::unordered_map<uint64_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;
}

一个时间轮写的我都要痛苦死了。。。
呜呜呜呜谁能救救我。。。。。。。。

http://www.hengruixuexiao.com/news/11465.html

相关文章:

  • 政府网站建设工作的自查报告seo网站seo
  • wordpress 上下篇西安seo排名
  • seo网站计划书北京网站推广排名
  • 青岛模板化网站网站推广公司
  • 做企业网站用哪个cms种子库
  • 虚拟云主机 wordpress免费的关键词优化工具
  • 大庆小程序开发东莞网站优化公司哪家好
  • 郑州今日头条热点新闻seo优化运营专员
  • 深圳关键词seo国外网站谷歌seo推广
  • 网站建设要程序员吗外链查询
  • jsp制作网站宁波网站优化公司价格
  • 织梦网站建设商丘网站建设公司
  • 举例网站联盟优化
  • 网站备案管局北京网站推广营销服务电话
  • 深圳app客户端做网站外贸网站建站
  • 品牌建设的创新与特色seo建站是什么意思
  • 湛江市建设教育协会学校网站百度网站推广关键词怎么查
  • 怎么自己做wordpress主题东莞网站建设优化排名
  • 浙江网站制作公司陕西网站设计
  • 国内买机票的网站建设可口可乐软文营销案例
  • wordpress广告平台优化网站关键词
  • shopex官方网站论坛网络运营师
  • 百度怎么做网站域名武汉seo优化
  • 商标设计网站提供哪些服务网络营销的六大特征
  • 番禺做网站哪家好贵阳网站优化公司
  • 广州网站开发公司排名病毒营销案例
  • 天空人体网站怎么做河南网站开发公司
  • 举报个人备案网站做经营性北京网络排名优化
  • 上海营销型网站报价网站快速收录付费入口
  • 广州建网站哪家最好百度站长资源平台