百度关键词首页排名,怎么做网站的优化,wordpress怎么写html,郑州超凡装饰公司怎么样目录
基本概念
函数定义
函数实现与测试
测试1结果如下
测试2结果如下 基本概念 时间轮 是一种 实现延迟功能#xff08;定时器#xff09; 的 巧妙算法。如果一个系统存在大量的任务调度#xff0c;时间轮可以高效的利用线程资源来进行批量化调度。把大批量的调度任务…目录
基本概念
函数定义
函数实现与测试
测试1结果如下
测试2结果如下 基本概念 时间轮 是一种 实现延迟功能定时器 的 巧妙算法。如果一个系统存在大量的任务调度时间轮可以高效的利用线程资源来进行批量化调度。把大批量的调度任务全部都绑定时间轮上通过时间轮进行所有任务的管理触发以及运行。能够高效地管理各种延时任务周期任务通知任务等。 这几张图是了解了解用的可能看不懂用以解决mudo服务器中高并发性能的要求的 weak_ptr了解一下下 函数定义
#include iostream
#include stdint.h
#include functional
#include vector
#include memory
#include unordered_mapusing TaskFunc std::functionvoid();
using ReleaseFunc std::functionvoid();
class TimerTask
{
private:uint64_t _id; // 定时器任务对象uint32_t _timeout; // 定时任务的超时时间TaskFunc _task_cb; // 定时器要执行的定时任务ReleaseFunc _release; // 用于删除TimerWheel中保存的定时器对象信息
public:TimerTask(uint64_t id, uint32_t delay, const TaskFunc cb) : _id(id), _timeout(delay), _task_cb(cb) {}~TimerTask(){_task_cb();_release();}void SetRelease(const ReleaseFunc cb) { _release cb; }
};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; // 用weak_ptr来构造出新的shared_ptr用来计数不过后续要记得释放
public:TimerWheel() : _capacity(60), _tick(0), _wheel(_capacity) {}void TimerAdd(uint64_t id, uint32_t delay, const TaskFunc cb); // 添加定时任务void TimerRefresh(uint64_t id); // 刷新/延迟定时任务
}; 函数实现与测试
#include iostream
#include stdint.h
#include functional
#include vector
#include memory
#include unordered_map
#include unistd.husing TaskFunc std::functionvoid();
using ReleaseFunc std::functionvoid();
class TimerTask
{
private:uint64_t _id; // 定时器任务对象uint32_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; // 用weak_ptr来构造出新的shared_ptr用来计数不过后续要记得释放
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)); // 第0个位置是隐藏的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;
} 测试1结果如下 达到预期
测试2结果如下
将下列代码放开 测试2结果如下 取消成功达到预期