muduo源码剖析之Timer定时器

简介: Timer 类是 muduo 网络库中的一个定时器类,用于在指定的时间间隔后执行某个任务。Timer 类提供了一系列的方法来创建、启动、停止和删除定时器,以及设置定时器的时间间隔和回调函数等。在 muduo 网络库中,Timer 类被广泛应用于各种网络任务中,例如定期发送心跳包、更新缓存、清理资源等。通过使用 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();
  }
}
目录
相关文章
|
安全 API
muduo源码剖析之EventLoop事件循环类
EventLoop.cc就相当于一个reactor,多线程之间的函数调用(用eventfd唤醒),epoll处理,超时队列处理,对channel的处理。运行loop的进程被称为IO线程,EventLoop提供了一些API确保相应函数在IO线程中调用,确保没有用互斥量保护的变量只能在IO线程中使用,也封装了超时队列的基本操作。
181 0
|
传感器 JSON 物联网
什么是MQTT遗嘱消息?如何配置和处理遗嘱消息?
什么是MQTT遗嘱消息?如何配置和处理遗嘱消息?
1031 0
什么是MQTT遗嘱消息?如何配置和处理遗嘱消息?
|
SQL 数据可视化 关系型数据库
SQLite3使用笔记(1)——查询
SQLite3使用笔记(1)——查询
710 0
|
JSON 物联网 数据格式
什么是MQTT 遗嘱消息(Will Message)
【2月更文挑战第17天】
936 7
什么是MQTT 遗嘱消息(Will Message)
|
Java C++ 开发者
muduo网络库
【6月更文挑战第15天】
211 7
|
Linux 编译器 API
探索Qt图像处理的奥秘:从入门到精通
探索Qt图像处理的奥秘:从入门到精通
928 2
|
存储
大端法与小段法的区别
大端法与小段法的区别
805 0
|
算法 应用服务中间件 nginx
QPS 提升60%,揭秘阿里巴巴轻量级开源 Web 服务器 Tengine 负载均衡算法
前言 在阿里七层流量入口接入层(Application Gateway)场景下, Nginx 官方的Smooth Weighted Round-Robin( SWRR )负载均衡算法已经无法再完美施展它的技能。
9382 91
|
存储 Unix Linux
Linux中的文本三剑客
今天给大家聊一聊Linux中文本操作的`三剑客:awk、grep、sed`,因其功能强大、使用频繁,且是Linux下文本处理的得力利器,常被称之为`文本三剑客`。`grep`常用于查找,`sed`常用于取行和替换,而`awk`常用于运算。
487 0