{"id":5246,"date":"2021-05-20T12:49:23","date_gmt":"2021-05-20T04:49:23","guid":{"rendered":"https:\/\/damogame.cn\/wordpress\/?p=5246"},"modified":"2021-05-29T14:13:58","modified_gmt":"2021-05-29T06:13:58","slug":"%e4%bb%8b%e7%bb%8d%e4%b8%80%e4%b8%aa%e6%96%b9%e4%be%bf%e5%a5%bd%e7%94%a8%e7%9a%84crontab%e5%ae%9a%e6%97%b6%e5%99%a8","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/5246","title":{"rendered":"\u4ecb\u7ecd\u4e00\u4e2a\u65b9\u4fbf\u597d\u7528\u7684CronTab\u5b9a\u65f6\u5668"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium\" src=\"https:\/\/i007.cc\/chevereto\/images\/2021\/05\/20\/e3f388652a4fa5c0f987e47af7e6b154.jpg\" width=\"650\" height=\"403\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a\u4f7f\u7528CronTab\u8868\u8fbe\u5f0f\u7684\u5b9a\u65f6\u5668\uff0c\u4f7f\u7528C++\u7f16\u5199\u7684\uff0c\u53ef\u4ee5\u5728\u6307\u5b9a\u65f6\u95f4\u70b9\u89e6\u53d1\u5b9a\u65f6\u5668\u4e8b\u4ef6\uff0c\u4e5f\u53ef\u4ee5\u5728\u4e00\u6bb5\u65f6\u95f4\u4e4b\u540e\u89e6\u53d1\u5b9a\u65f6\u5668\u4e8b\u4ef6\u3002<\/p>\n<p>\u7279\u70b9\uff1a<\/p>\n<ol>\n<li>\u5bf9\u65f6\u95f4\u7684\u8868\u8fbe\u80fd\u529b\u5f3a\uff0c\u6bd5\u7adfCronTab\u8868\u8fbe\u5f0f\u5df2\u7ecf\u5728Linux\u5e73\u53f0\u4e0a\u5e7f\u6cdb\u4f7f\u7528\uff0c\u4e45\u7ecf\u8003\u9a8c\u3002<\/li>\n<li>\u4f7f\u7528\u65b9\u4fbf\uff0c\u4e00\u4e2a\u5934\u6587\u4ef6\u641e\u5b9a\u4e00\u5207\uff0c\u62f7\u8d1d\u8fc7\u53bb\u5c31\u53ef\u4ee5\u4f7f\u7528\uff0c\u4e0d\u4f9d\u8d56\u7b2c\u4e09\u65b9\u5e93\uff0cWindows\u3001Centos\u3001Ubuntu\u3001Mac\u90fd\u53ef\u4ee5\u8fd0\u884c\u3002\u4e00\u884c\u4ee3\u7801\u6dfb\u52a0\u4e00\u4e2a\u5b9a\u65f6\u5668\uff0c\u53ef\u4f20\u5165\u6210\u5458\u51fd\u6570\uff0c\u643a\u5e26\u81ea\u5b9a\u4e49\u53c2\u6570\u3002<\/li>\n<li>\u7cbe\u5ea6\u9ad8\u3001\u8bef\u5dee\u4e0d\u7d2f\u79ef\u3002<\/li>\n<li>\u6027\u80fd\u597d\uff0c\u5bf9\u4e8e\u5b9a\u65f6\u5668\u5185\u7684\u5bf9\u8c61\u4e2a\u6570\uff0c\u65f6\u95f4\u5224\u65ad\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u505a\u5230\u4e86O(log(n))\u3002<\/li>\n<li>\u53ef\u4ee5\u9009\u62e9\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u4e5f\u53ef\u4ee5\u9009\u62e9\u5728\u5b50\u7ebf\u7a0b\u4e2d\u89e6\u53d1\u5b9a\u65f6\u5668\u3002<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p>\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u89e6\u53d1\u5b9a\u65f6\u5668\u4e8b\u4ef6\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">void TestCronTimerInMainThread() {\r\n    cron_timer::TimerMgr mgr;\r\n\r\n    mgr.AddTimer(\"* * * * * *\", [](void) {\r\n        \/\/ \u6bcf\u79d2\u949f\u90fd\u4f1a\u6267\u884c\u4e00\u6b21\r\n        Log(\"1 second cron timer hit\");\r\n    });\r\n\r\n    mgr.AddTimer(\"0\/3 * * * * *\", [](void) {\r\n        \/\/ \u4ece0\u79d2\u5f00\u59cb\uff0c\u6bcf3\u79d2\u949f\u6267\u884c\u4e00\u6b21\r\n        Log(\"3 second cron timer hit\");\r\n    });\r\n\r\n    mgr.AddTimer(\"0 * * * * *\", [](void) {\r\n        \/\/ 1\u5206\u949f\u6267\u884c\u4e00\u6b21\uff08\u6bcf\u6b21\u90fd\u57280\u79d2\u7684\u65f6\u5019\u6267\u884c\uff09\u7684\u5b9a\u65f6\u5668\r\n        Log(\"1 minute cron timer hit\");\r\n    });\r\n\r\n    mgr.AddTimer(\"15;30;33 * * * * *\", [](void) {\r\n        \/\/ \u6307\u5b9a\u65f6\u95f4\uff0815\u79d2\u300130\u79d2\u548c33\u79d2\uff09\u70b9\u90fd\u4f1a\u6267\u884c\u4e00\u6b21\r\n        Log(\"cron timer hit at 15s or 30s or 33s\");\r\n    });\r\n\r\n    mgr.AddTimer(\"40-50 * * * * *\", [](void) {\r\n        \/\/ \u6307\u5b9a\u65f6\u95f4\u6bb5\uff0840\u523050\u5185\u7684\u6bcf\u4e00\u79d2\uff09\u6267\u884c\u7684\u5b9a\u65f6\u5668\r\n        Log(\"cron timer hit between 40s to 50s\");\r\n    });\r\n\r\n    auto timer = mgr.AddDelayTimer(3000, [](void) {\r\n        \/\/ 3\u79d2\u949f\u4e4b\u540e\u6267\u884c\r\n        Log(\"3 second delay timer hit\");\r\n    });\r\n\r\n    \/\/ \u53ef\u4ee5\u5728\u6267\u884c\u4e4b\u524d\u53d6\u6d88\r\n    timer-&gt;Cancel();\r\n\r\n    mgr.AddDelayTimer(\r\n        10000,\r\n        [](void) {\r\n            \/\/ \u6bcf10\u79d2\u949f\u6267\u884c\u4e00\u6b21\uff0c\u603b\u5171\u6267\u884c3\u6b21\r\n            Log(\"10 second delay timer hit\");\r\n        },\r\n        3);\r\n    Log(\"10 second delay timer added\");\r\n\r\n    while (!_shutDown) {\r\n        auto nearest_timer =\r\n            (std::min)(std::chrono::system_clock::now() + std::chrono::milliseconds(500), mgr.GetNearestTime());\r\n        std::this_thread::sleep_until(nearest_timer);\r\n        mgr.Update();\r\n    }\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>\u4ee3\u7801\u5728\u8fd9\u91cc<\/p>\n<div class=\"highlight\">\n<pre><code class=\"language-text\">https:\/\/github.com\/williamxin\/cron_timer<\/code><\/pre>\n<\/div>\n<p>\u7f16\u7a0b\u4e0d\u6613\uff0c\u5982\u679c\u4f60\u89c9\u5f97\u597d\uff0c\u8bf7\u591a\u5173\u6ce8\u70b9\u8d5e\uff0c\u8c22\u8c22\uff01<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; &nbsp; \u8fd9\u662f\u4e00\u4e2a\u4f7f\u7528<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[],"tags":[119],"class_list":["post-5246","post","type-post","status-publish","format-standard","hentry","tag-03-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/5246","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=5246"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/5246\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=5246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=5246"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=5246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}