Ctimer.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // CTimer.hpp
  3. // ZJCrossCpp
  4. //
  5. // Created by eafy on 2020/10/5.
  6. // Copyright © 2020 ZJ. All rights reserved.
  7. //
  8. #ifndef CTimer_h
  9. #define CTimer_h
  10. #include <stdio.h>
  11. #include <functional>
  12. #include <chrono>
  13. #include <thread>
  14. #include <atomic>
  15. #include <mutex>
  16. #include <string>
  17. #include <condition_variable>
  18. class CTimer
  19. {
  20. public:
  21. CTimer(const std::string sTimerName = ""); //构造定时器,附带名称
  22. ~CTimer();
  23. /**
  24. 开始运行定时器
  25. @param msTime 延迟运行(单位ms)
  26. @param task 任务函数接口
  27. @param bLoop 是否循环(默认执行1次)
  28. @param async 是否异步(默认异步)
  29. @return true:已准备执行,否则失败
  30. */
  31. bool Start(unsigned int msTime, std::function<void()> task, bool bLoop = false, bool async = true);
  32. /**
  33. 取消定时器,同步定时器无法取消(若任务代码已执行则取消无效)
  34. */
  35. void Cancel();
  36. /**
  37. 同步执行一次
  38. #这个接口感觉作用不大,暂时现实在这里
  39. @param msTime 延迟时间(ms)
  40. @param fun 函数接口或lambda代码块
  41. @param args 参数
  42. @return true:已准备执行,否则失败
  43. */
  44. template<typename callable, typename... arguments>
  45. bool SyncOnce(int msTime, callable&& fun, arguments&&... args) {
  46. std::function<typename std::result_of<callable(arguments...)>::type()> task(std::bind(std::forward<callable>(fun), std::forward<arguments>(args)...)); //绑定任务函数或lambda成function
  47. return Start(msTime, task, false, false);
  48. }
  49. /**
  50. 异步执行一次任务
  51. @param msTime 延迟及间隔时间(毫秒)
  52. @param fun 函数接口或lambda代码块
  53. @param args 参数
  54. @return true:已准备执行,否则失败
  55. */
  56. template<typename callable, typename... arguments>
  57. bool AsyncOnce(int msTime, callable&& fun, arguments&&... args) {
  58. std::function<typename std::result_of<callable(arguments...)>::type()> task(std::bind(std::forward<callable>(fun), std::forward<arguments>(args)...));
  59. return Start(msTime, task, false);
  60. }
  61. /**
  62. 异步执行一次任务(默认延迟10毫秒后执行)
  63. @param fun 函数接口或lambda代码块
  64. @param args 参数
  65. @return true:已准备执行,否则失败
  66. */
  67. template<typename callable, typename... arguments>
  68. bool AsyncOnce(callable&& fun, arguments&&... args) {
  69. std::function<typename std::result_of<callable(arguments...)>::type()> task(std::bind(std::forward<callable>(fun), std::forward<arguments>(args)...));
  70. return Start(1, task, false);
  71. }
  72. /**
  73. 异步循环执行任务
  74. @param msTime 延迟及间隔时间(毫秒)
  75. @param fun 函数接口或lambda代码块
  76. @param args 参数
  77. @return true:已准备执行,否则失败
  78. */
  79. template<typename callable, typename... arguments>
  80. bool AsyncLoop(int msTime, callable&& fun, arguments&&... args) {
  81. std::function<typename std::result_of<callable(arguments...)>::type()> task(std::bind(std::forward<callable>(fun), std::forward<arguments>(args)...));
  82. return Start(msTime, task, true);
  83. }
  84. public:
  85. /// 获取时间戳(毫秒)
  86. static uint64_t Timestamp();
  87. /// 获取格式化时间
  88. static std::string FormatTime(const std::string sFormat = "%Y-%m-%d %H:%M:%S");
  89. /// 获取UTC时间
  90. static struct tm *UTCTime(long long secTime = 0);
  91. /// 获取UTC时间(秒)
  92. static int64_t UTCTime();
  93. /// 获取与0时区的时差(以秒为单位)
  94. static int TimeDifFrimGMT();
  95. private:
  96. void DeleteThread(); //删除任务线程
  97. public:
  98. int m_nCount = 0; //循环次数
  99. int m_nTag = 0; //定时器标签
  100. private:
  101. std::string m_sName; //定时器名称
  102. std::atomic_bool m_bExpired; //装载的任务是否已经过期
  103. std::atomic_bool m_bTryExpired; //装备让已装载的任务过期(标记)
  104. std::atomic_bool m_bLoop; //是否循环
  105. std::thread *m_Thread = nullptr;
  106. std::mutex m_ThreadLock;
  107. std::condition_variable_any m_ThreadCon;
  108. };
  109. #endif /* CTimer_hpp */