/******************************************************************************************************
@author ghq
@time 2018-4-2
@topic 定时器
1、定时分执行一次、循环执行
2、单线程和多线程。主线程为单线程有多个定时器,如果每个定时器都用一个线程去实现,有些不太合理,也不易管理。
3、程序本来就是多线程,也会造成更大量的线程,另外定时器事件不太好解决。
4、借鉴boost利用最小堆实现
5、多线程的业务线程中不包含定时器管理器,单独启一个线程用来管理所有定时器,当时间触发时,向业务线程投递定时器消息即可。
******************************************************************************************************/
#ifndef MYTIMER_H
#define MYTIMER_H
#include <iostream>
#include <functional>
#include <vector>
using namespace std;
class TimerManager;
class MyTimer
{
public:
enum class TimerType{ONCE=0,CIRCLE=1};
MyTimer (TimerManager& manager);
~MyTimer ();
//启动一个定时器
template<typename Func>
void start (Func func, unsigned int ms, TimerType type);
//终止一个定时器
void stop ();
private:
//执行
void on_timer(un
最小堆:C++定时器实现 仿boost
最新推荐文章于 2025-04-06 21:24:29 发布