我是靠谱客的博主 动听蚂蚁,这篇文章主要介绍C++ 固定时间定时执行逻辑 - 二分定点法,现在分享给大家,希望可以做个参考。

文章目录

      • 定时任务 - 二分定点法
        • **简介**: 有需要例如每日晚上十二点整点执行清除某些数据或者做定期信息更新时需要用到这种逻辑、该逻辑是我认为比较节省资源与精准的逻辑,若有觉得不对的地方可以补充一下
        • **优点**: 节省CPU损耗,不需要频繁检测当前时间
        • **缺点**: 若更改系统时间,有可能会导致当天定时无法触发
        • **架构**: 每次睡眠时间 (目标时间 - 当前时间)÷ 2
        • **例**: 每日 正点0时执行

  • 定时任务 - 二分定点法

  • 简介: 有需要例如每日晚上十二点整点执行清除某些数据或者做定期信息更新时需要用到这种逻辑、该逻辑是我认为比较节省资源与精准的逻辑,若有觉得不对的地方可以补充一下

  • 优点: 节省CPU损耗,不需要频繁检测当前时间

  • 缺点: 若更改系统时间,有可能会导致当天定时无法触发

  • 架构: 每次睡眠时间 (目标时间 - 当前时间)÷ 2

CrrentTime 15:00:00
Sleep 04:30:00
Sleep 02:15:00
Sleep 01:07:30
...
DstTime 24:00:00
  • : 每日 正点0时执行

复制代码
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//C++ 11 #include <chrono> #include <thread> { //时区 - cst时间戳是从1970-01-01 08:00:00开始计时 static const uint32_t nTimestampZone = 8 * 60 * 60; //一整天所需要的秒数 static const uint64_t nWholeDaySec = 24 * 60 * 60; //偏差范围 - 目标12:00:00 -> 实际允许 12:00:00 ~ 12:00:20 static const uint32_t nDeviationAreaSec = 20; //目标时间 static const uint8_t nDstHour = 0; static const uint8_t nDstMinute = 0; static const uint8_t nDstSec = 0; static const uint32_t nDstTimestampSec = (nDstHour * 60 * 60) + (nDstMinute * 60) + (nDstSec); //今日是否已经执行过 bool bExecuted = false; uint64_t nTime = 0; while (true) { #ifdef __cplusplus > 201103L //C++11 //此处使用C++ 11(顺带了解C++11) - 可根据自己环境更改时间方法n //获取系统时间 + 8:00:00 ; 因为cst时间戳是从1970-01-01 08:00:00开始计时的 //可以查找方法将 CST时间戳自动转换成UTC时间戳 但我没找到方法,因此只能写死的8:00:00 nTime = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()) + nTimestampZone; #else //C语言中的time nTime = time(nullptr) + nTimestampZone; //std::localtime(&nTimet); #endif //实际偏差 int32_t nDeviation = nTime % nWholeDaySec - nDstTimestampSec; //偏差在一定范围内 if (!bExecuted && nDeviation <= nDeviationAreaSec && nDeviation >= 0) { //执行相应处理 bExecuted = true; /** * * * do something * **/ printf("Executen for Current Time [%02d:%02d:%02d]nn", nTime / 60 / 60, nTime / 60 % 60, nTime % 60); continue; } else if (bExecuted && nDeviation <= nDeviationAreaSec && nDeviation >= 0) { //仍在区间范围内 则直接跳过 std::this_thread::sleep_for(std::chrono::seconds(1)); printf("Sleep Once in the Current Area n"); continue; } else if (bExecuted && (nDeviation > nDeviationAreaSec || nDeviation < 0)) { //经过区间范围之外 bExecuted = false; continue; } static uint32_t nNeedSleep = 0; nNeedSleep = (nDeviation > 0 ? nWholeDaySec - nDeviation : (abs(nDeviation))) / 2; if (nNeedSleep == 1) { std::this_thread::sleep_for(std::chrono::milliseconds(100)); printf("Sleep once for [100 milliseconds]nn"); continue; } printf("Sleep once for [%d second]nn", nNeedSleep); //睡眠偏差值 std::this_thread::sleep_for(std::chrono::seconds(nNeedSleep)); } }

最后

以上就是动听蚂蚁最近收集整理的关于C++ 固定时间定时执行逻辑 - 二分定点法的全部内容,更多相关C++内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(93)

评论列表共有 0 条评论

立即
投稿
返回
顶部