定时器 c++ 基于时间线

获取当前时间std::chrono::system_clock::now();

std::chrono::time_point_cast<std::chrono::milliseconds>(now) 是 std::chrono 标准库中的一个函数调用,用于将时间点 now 转换为毫秒级别精度的时间点。

friend class timermanger; 表示将类 timermanger 声明为当前类的友元类。当一个类被声明为另一个类的友元类时,这意味着被声明为友元类的类可以访问声明它为友元的类的私有成员。

就是现有时间上,根据给的时间间隔,去执行相对应的数,把这个时间间隔发生的事件按照大小存储在map里

原.cpp

cpp 复制代码
#include <iostream>
#include <string>
#include <thread>
#include "标头.h"
#include "timemanger.h"
using std::string;
void foo1()
{
    std::cout << "1000" << std::endl;
}
void foo2()
{
    std::cout << "1001" << std::endl;
}
void bar(const string& name)
{
    std::cout << "bar:" << name << std::endl;
}
int main()
{
    timemanger mar1;
    mar1.schedule(1000,5,foo1);//注册定时任务    
    mar1.schedule(1001,3, foo2);
    while (true) {
        mar1.update();}//触发定时任务
    return 0;
}
cpp 复制代码
Timer.h
#pragma once
#include<functional>;
#include<iostream>;

    class timer {
      friend class timemanger;
    public:
      timer();
      timer(int repeat);
      ~timer();
      template<typename F, typename ...Arges>
      void callback(int milliseconds, F&& f, Arges&& ...arges);
      void on_timer();
    
      int m_time;//定时器触发的时间点
      int m_period;//定时器触发的周期
      std::function<void()>m_func;
      int m_repeat;//定时器触发的次数,-1无限触发
      static int now();
    };
    template<typename F, typename ...Arges>
    inline void timer::callback(int milliseconds, F&& f, Arges&& ...arges)
    {
      m_period = milliseconds;
      //每个绑定的函数对象可以传递不同的参数。
      m_func = std::bind(std::forward<F>(f), std::forward < Arges>(arges)...);}
cpp 复制代码
timer.cpp
#include "标头.h"
#include<chrono>
#include<ctime>
timer::timer():m_period(0),m_repeat(-1)
{
  m_time = now();
}
timer::timer(int repeat): m_period(0), m_repeat(repeat)
{
  m_time = now();
}
timer::~timer()
{
}
void timer::on_timer()
{
  if (!m_func || m_repeat == 0)
    return ;//void函数用
  m_func();//执行任务
  m_time += m_period;更新时间,因为m_time是从now那里获取的当前时间
  if (m_repeat > 0)m_repeat--;//触发次数减少
}
int timer::now()
{//获取当前时间
  auto now = std::chrono::system_clock::now();
//再把当前时间点转换为毫秒数,c++11里面的时间库
  auto now_hs = std::chrono::time_point_cast<std::chrono::milliseconds>(now);
  //我们计算了从系统纪元到时间点 now_hs 的时间间隔,并以毫秒为单位表示
  //最后,使用 count() 函数获取这个时间间隔的毫秒数值。
  return now_hs.time_since_epoch().count();
}
cpp 复制代码
Timemanger.h

#pragma once
#include<map>
#include "标头.h"
class timemanger {
public:
  timemanger() = default;
  ~timemanger() = default;

  //std::multimap 是 C++ 标准库中提供的一个关联容器,它类似于 std::map,但允许键(key)重复,排序,通过key知道下一个触发时间
  std::multimap<int, timer>m_timers;

  template<typename F,typename ...Arges>
  void schedule(int milliseconds, F&& f, Arges&& ...arges);//F是函数,然后f是他可以接受的可变参数
  template<typename F, typename ...Arges>
  void schedule(int milliseconds,int repeat, F&& f, Arges&& ...arges);
  void update();
};

template<typename F, typename ...Arges>
inline void timemanger::schedule(int milliseconds, F&& f, Arges && ...arges)
{
  schedule(milliseconds, -1, std::forward<F>(f), std::forward<Arges>(arges)...);
}

template<typename F, typename ...Arges>
inline void timemanger::schedule(int milliseconds, int repeat, F&& f, Arges && ...arges)
{
  timer t(repeat);

  t.callback(milliseconds, std::forward<F>(f), std::forward<Arges>(arges)...);
  m_timers.insert(std::make_pair(t.m_time, t)); 
}
cpp 复制代码
Timemanger.cpp
#include "timemanger.h"
void timemanger::update()
{
  if (m_timers.empty())  return;
  int now= timer::now();

  for (auto it = m_timers.begin(); it != m_timers.end();) {
    if (it->first > now)
      return;
    it->second.on_timer();
    timer t = it->second;
    it = m_timers.erase(it); 
    if (t.m_repeat!= 0)
    {
      auto new_it = m_timers.insert(std::make_pair(t.m_time, t));
      if (it == m_timers.end()||new_it->first<it->first) {
        it = new_it;     }        }     }}

欢迎交流

相关推荐
9677几秒前
cURL curl
c++
一匹电信狗11 分钟前
【Linux我做主】进程程序替换和exec函数族
linux·运维·服务器·c++·ubuntu·小程序·开源
无人机90132 分钟前
Delphi 网络编程实战:TIdTCPClient 与 TIdTCPServer 类深度解析
java·开发语言·前端
会编程的土豆36 分钟前
【数据结构与算法】二叉树从建立开始
数据结构·c++·算法
froginwe111 小时前
CSS 图像拼合技术
开发语言
计算机安禾1 小时前
【数据结构与算法】第22篇:线索二叉树(Threaded Binary Tree)
c语言·开发语言·数据结构·学习·算法·链表·visual studio code
:mnong1 小时前
Superpowers 项目设计分析
java·c语言·c++·python·c#·php·skills
a里啊里啊1 小时前
测试开发面试题
开发语言·chrome·python·xpath
豆沙糕1 小时前
Python异步编程从入门到实战:结合RAG流式回答全解析
开发语言·python·面试
信奥胡老师2 小时前
P1255 数楼梯
开发语言·数据结构·c++·学习·算法