加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
main.cpp 2.11 KB
一键复制 编辑 原始数据 按行查看 历史
fhykfil 提交于 2021-08-14 10:36 . select代替sleep计时
#include "HeapTimer.h"
#include "TimeWheel.h"
int main()
{
int msgid=0;
//timewheel test example
auto wheelt=[&msgid](int id,int delay,std::string name){
std::cout<< ++msgid <<" name:"<<name<< " id:"<<id<<" delay:"<<delay<<std::endl;
};
TimeWheel* wheel_timer=new TimeWheel();
wheel_timer->addTimeWheelArray(60,1000,"sceond");
wheel_timer->addTimeWheelArray(1000,1,"milisceond");
wheel_timer->addTimeWheelArray(60,60*1000,"min");
wheel_timer->addTimer("test1",2000,wheelt,TimeWheelNode::TIMER_TYPE_CYCLE);
wheel_timer->addTimer("test2",8000,wheelt,TimeWheelNode::TIMER_TYPE_CYCLE);
wheel_timer->addTimer("test3",3000,wheelt,TimeWheelNode::TIMER_TYPE_CYCLE);
wheel_timer->start();
for (int i = 0; i < 20; i++)
{
std::cout<< ++msgid<<" " << i<<std::endl;
if(i==17)
wheel_timer->stop();
sleep(1);
}
wheel_timer->deleteTimeWheelArray(1);
wheel_timer->deleteTimeWheelArray(60*1000);
wheel_timer->deleteTimeWheelArray(1000);
// heaptimer test example
// auto test=[&msgid](int id,int timestamp){
// std::cout<< ++msgid << " id:"<<id<<" timestamp:"<<timestamp<<std::endl;
// // std::cout<< ++msgid <<std::endl;
// };
// int msgid=0;
// HeapTimer *timer=HeapTimer::instance();
// auto test=[&msgid](int id,int timestamp){
// std::cout<< ++msgid << " id:"<<id<<" timestamp:"<<timestamp<<std::endl;
// // std::cout<< ++msgid <<std::endl;
// };
// timer->addTimer(new TimeNode(5,test,TimeNode::TimerType::TIMER_TYPE_ONCE));
// timer->addTimer(new TimeNode(4,test,TimeNode::TimerType::TIMER_TYPE_ONCE));
// timer->addTimer(new TimeNode(3,test,TimeNode::TimerType::TIMER_TYPE_ONCE));
// timer->addTimer(new TimeNode(2,test,TimeNode::TimerType::TIMER_TYPE_CYCLE));
// timer->addTimer(new TimeNode(1,test,TimeNode::TimerType::TIMER_TYPE_ONCE));
// timer->start();
// for (int i = 0; i < 10; i++)
// {
// std::cout<< ++msgid <<std::endl;
// if(i==5) timer->stop();
// sleep(2);
// }
getchar();
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化