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

muduo源码剖析之Timer定时器

简介

Timer 类是 muduo 网络库中的一个定时器类,用于在指定的时间间隔后执行某个任务。

Timer 类提供了一系列的方法来创建、启动、停止和删除定时器,以及设置定时器的时间间隔和回调函数等。

在 muduo 网络库中,Timer 类被广泛应用于各种网络任务中,例如定期发送心跳包、更新缓存、清理资源等。通过使用 Timer 类,我们可以方便地实现定时任务,提高网络应用程序的可靠性和稳定性。

以下是 muduo 网络库中 Timer 类的主要方法和功能:

  1. Timer::start():启动定时器,在指定的时间间隔后调用回调函数。
  2. Timer::stop():停止定时器,不再执行定时任务。
  3. Timer::restart():重新启动定时器,重新开始执行定时任务。
  4. Timer::reset():重新设置定时器的时间间隔和回调函数。
  5. Timer::getExpiryTime():获取定时器的到期时间。

通过使用 Timer 类,我们可以方便地实现各种定时任务,提高网络应用程序的可靠性和稳定性。同时,Timer 类也提供了一些高级功能,例如可以设置多个定时器,以及在多个线程中安全地使用定时器等。

源码剖析

Timer.h

// Copyright 2010, Shuo Chen.  All rights reserved.
// http://code.google.com/p/muduo/
//
// Use of this source code is governed by a BSD-style license
// that can be found in the License file.// Author: Shuo Chen (chenshuo at chenshuo dot com)
//
// This is an internal header file, you should not include this.#ifndef MUDUO_NET_TIMER_H
#define MUDUO_NET_TIMER_H#include "muduo/base/Atomic.h"
#include "muduo/base/Timestamp.h"
#include "muduo/net/Callbacks.h"namespace muduo
{
namespace net
{///
/// Internal class for timer event.
///
class Timer : noncopyable
{public:Timer(TimerCallback cb, Timestamp when, double interval): callback_(std::move(cb)),expiration_(when),interval_(interval),repeat_(interval > 0.0),sequence_(s_numCreated_.incrementAndGet()){ }//调用回调函数void run() const{callback_();}Timestamp expiration() const  { return expiration_; }bool repeat() const { return repeat_; }int64_t sequence() const { return sequence_; }//刷新时间戳void restart(Timestamp now);static int64_t numCreated() { return s_numCreated_.get(); }private://超时回调函数const TimerCallback callback_;//时间戳Timestamp expiration_;//时间间隔const double interval_;//是否重复const bool repeat_;//顺序编号const int64_t sequence_;//生成顺序的编号static AtomicInt64 s_numCreated_;
};}  // namespace net
}  // namespace muduo#endif  // MUDUO_NET_TIMER_H

Timer.cc

// Copyright 2010, Shuo Chen.  All rights reserved.
// http://code.google.com/p/muduo/
//
// Use of this source code is governed by a BSD-style license
// that can be found in the License file.// Author: Shuo Chen (chenshuo at chenshuo dot com)#include "muduo/net/Timer.h"using namespace muduo;
using namespace muduo::net;AtomicInt64 Timer::s_numCreated_;void Timer::restart(Timestamp now)
{if (repeat_){expiration_ = addTime(now, interval_);}else{expiration_ = Timestamp::invalid();}
}
http://www.lryc.cn/news/221753.html

相关文章:

  • CocosCreator:背景滚动 、背景循环滚动
  • 中远麒麟堡垒机SQL注入漏洞复现
  • ActiveMq学习⑨__基于zookeeper和LevelDB搭建ActiveMQ集群
  • Ansible概述以及模块
  • Cannot run program “D:\c\IntelliJ IDEA 2021.1.3\jbr\bin\java.exe“
  • 案例-注册页面(css)
  • Ansible--playbook 剧本
  • Vue3.0路由拦截
  • EtherCAT转EtherNET/IP协议网关控制EtherCAT伺服驱动器的方法
  • 钉钉内嵌H5遇到的一些问题
  • LeetCode 热题100——链表专题(二)
  • 【Rust日报】2023-11-06 ESP上使用 Rust实现 SNTP协议
  • LibreOJ - 2874 历史研究 (回滚莫队)
  • 人工智能-卷积神经网络之多输入多输出通道
  • Open3D(C++) Umeyama算法求两个点云的变换矩阵
  • 【C++】从入门到精通第二弹——类的构造与析构函数
  • C#8.0本质论第十一章--异常处理
  • FPGA高端项目:图像缩放+GTP+UDP架构,高速接口以太网视频传输,提供2套工程源码加QT上位机源码和技术支持
  • ansible安装和常见模块
  • 【Python基础】 Python设计模式之单例模式介绍
  • 算法小白的心得笔记:关于Nan
  • Photoshop 2023 v24.7
  • 进程间通信(IPC)-管道、消息队列、信号量、共享存储、socket
  • 「Verilog学习笔记」使用generate…for语句简化代码
  • 互联网Java工程师面试题·Spring篇·第七弹
  • mysql驱动包引起的告警问题using SSL the verifyServerCertificate property is set to ‘false‘
  • draw.io与项目管理——如何利用流程图工具提高项目管理效率
  • LoRaWAN物联网架构
  • 数据结构(五):哈希表及面试常考的算法
  • 水利部加快推进小型水库除险加固,大坝安全监测是重点