• 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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73

    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();
      }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
  • 相关阅读:
    Vue----全局组件和局部组件
    【数据仓库】hadoop生态圈与数据仓库
    SpringBoot第三方登录JustAuth
    利刃出鞘 | 从五大核心技术来看Bonree ONE 2.0的全球竞争力
    产品代码都给你看了,可别再说不会DDD(十):CQRS
    真·Redis缓存优化—97%的优化率你见过嘛?
    基于瞬时无功功率ip-iq的谐波信号检测Simulink仿真
    【Q1—45min】
    “气运”其实是可以改变的,方法也很简单!
    Vue2.0打包指定路由前缀
  • 原文地址:https://blog.csdn.net/weixin_50448879/article/details/134277910