{"id":68714,"date":"2025-05-26T18:23:40","date_gmt":"2025-05-26T10:23:40","guid":{"rendered":"https:\/\/fwq.ai\/blog\/?p=68714"},"modified":"2025-05-26T18:23:40","modified_gmt":"2025-05-26T10:23:40","slug":"%e4%bd%bf%e7%94%a8c%e6%a0%87%e5%87%86%e5%ba%93%e6%9e%84%e5%bb%ba%e8%b7%a8%e5%b9%b3%e5%8f%b0%e5%ae%9a%e6%97%b6%e5%99%a8%e7%b1%bb%ef%bc%9a%e5%a4%9a%e7%ba%bf%e7%a8%8b%e5%ae%9a%e6%97%b6%e8%b0%83","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/68714\/","title":{"rendered":"\u4f7f\u7528C++\u6807\u51c6\u5e93\u6784\u5efa\u8de8\u5e73\u53f0\u5b9a\u65f6\u5668\u7c7b\uff1a\u591a\u7ebf\u7a0b\u5b9a\u65f6\u8c03\u5ea6\u7684\u6700\u4f73\u5b9e\u8df5"},"content":{"rendered":"<h2>\u524d\u8a00<\/h2>\n<p>\u5728\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u5f00\u53d1\u4e2d\uff0c\u5b9a\u65f6\u5668\u662f\u4e00\u4e2a\u5e38\u89c1\u4e14\u5173\u952e\u7684\u7ec4\u4ef6\u3002\u5c24\u5176\u662f\u5728\u8fdb\u884c\u670d\u52a1\u5668\u7aef\u5f00\u53d1\u3001\u4efb\u52a1\u8c03\u5ea6\u6216\u8d44\u6e90\u76d1\u63a7\u7b49\u5de5\u4f5c\u65f6\uff0c\u51c6\u786e\u5730\u8bbe\u5b9a\u5ef6\u65f6\u4efb\u52a1\u6267\u884c\u663e\u5f97\u5c24\u4e3a\u91cd\u8981\u3002\u672c\u6587\u5c06\u5e26\u60a8\u4e00\u6b65\u6b65\u7528 C++ \u6807\u51c6\u5e93\u5b9e\u73b0\u4e00\u4e2a\u529f\u80fd\u5b8c\u6574\u7684\u5b9a\u65f6\u5668\u7c7b\uff0c\u517c\u5bb9\u73b0\u4ee3 C++ \u5f00\u53d1\u65b9\u5f0f\uff0c\u540c\u65f6\u4e5f\u9002\u7528\u4e8e\u8fd0\u884c\u5728<strong>\u7f8e\u56fd\u670d\u52a1\u5668<\/strong>\u73af\u5883\u4e2d\u7684\u591a\u7ebf\u7a0b\u5e94\u7528\u3002<\/p>\n<h2>\u4e00\u3001\u5b9a\u65f6\u5668\u539f\u7406\u7b80\u4ecb<\/h2>\n<p>\u5b9a\u65f6\u5668\u7684\u5b9e\u73b0\u601d\u8def\u672c\u8d28\u4e0a\u975e\u5e38\u7b80\u5355\uff1a<\/p>\n<ol>\n<li>\u521b\u5efa\u4e00\u4e2a\u72ec\u7acb\u7684\u7ebf\u7a0b\uff1b<\/li>\n<li>\u8ba9\u8be5\u7ebf\u7a0b\u4f11\u7720\u6307\u5b9a\u65f6\u957f\uff1b<\/li>\n<li>\u4f11\u7720\u7ed3\u675f\u540e\u6267\u884c\u9884\u8bbe\u4efb\u52a1\u3002<\/li>\n<\/ol>\n<p>\u5728 Python \u4e2d\uff0c\u8fd9\u4e00\u903b\u8f91\u7531 <code>threading.Timer<\/code> \u5b8c\u7f8e\u8be0\u91ca\uff1a<\/p>\n<pre><code class=\"language-python\">from threading import Timer\r\n\r\ndef greet():\r\n    print(\"Hello, World!\")\r\n\r\nt = Timer(10.0, greet)\r\nt.start()\r\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u7c7b\u662f <code>Thread<\/code> \u7684\u5b50\u7c7b\uff0c\u901a\u8fc7 <code>start()<\/code> \u542f\u52a8\uff0c\u652f\u6301 <code>cancel()<\/code> \u4e2d\u65ad\u3002C++\u4e2d\u6ca1\u6709\u539f\u751f\u7684 Timer \u7c7b\uff0c\u4f46\u901a\u8fc7 <code>&lt;thread&gt;<\/code>\u3001<code>&lt;chrono&gt;<\/code> \u548c <code>std::function<\/code> \u7b49\u6807\u51c6\u5e93\uff0c\u6211\u4eec\u5b8c\u5168\u53ef\u4ee5\u6784\u9020\u4e00\u4e2a\u529f\u80fd\u76f8\u4eff\u7684\u5b9a\u65f6\u5668\u5de5\u5177\u3002<\/p>\n<h2>\u4e8c\u3001C++\u7248\u5b9a\u65f6\u5668\u7c7b\u63a5\u53e3\u8bbe\u8ba1<\/h2>\n<p>\u6a21\u4eff Python \u7684 <code>threading.Timer<\/code> \u63a5\u53e3\uff0c\u6211\u4eec\u8bbe\u8ba1\u5982\u4e0b\u7684 C++ \u5b9a\u65f6\u5668\u7c7b\uff1a<\/p>\n<pre><code class=\"language-cpp\">class Timer {\r\npublic:\r\n    typedef std::function&lt;void()&gt; Callback;\r\n\r\n    Timer(int interval, Callback function);\r\n    void start();\r\n    void cancel();\r\n};\r\n<\/code><\/pre>\n<ul>\n<li><code>Callback<\/code> \u662f\u4efb\u52a1\u6267\u884c\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u662f\u51fd\u6570\u6307\u9488\u3001Lambda\u3001\u51fd\u6570\u5bf9\u8c61\u7b49\uff1b<\/li>\n<li><code>interval<\/code> \u8868\u793a\u5b9a\u65f6\u5668\u5ef6\u8fdf\u6267\u884c\u7684\u79d2\u6570\uff1b<\/li>\n<li><code>start()<\/code> \u542f\u52a8\u5b9a\u65f6\u5668\u7ebf\u7a0b\uff1b<\/li>\n<li><code>cancel()<\/code> \u7528\u4e8e\u53d6\u6d88\u672a\u89e6\u53d1\u7684\u5b9a\u65f6\u4efb\u52a1\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u6837\u7684\u5c01\u88c5\u5bf9\u4e8e\u90e8\u7f72\u5728<strong>\u7f8e\u56fdVPS<\/strong>\u6216\u672c\u5730\u9ad8\u5e76\u53d1\u670d\u52a1\u4e2d\uff0c\u63d0\u4f9b\u4e86\u826f\u597d\u7684\u5f02\u6b65\u4efb\u52a1\u8c03\u5ea6\u80fd\u529b\u3002<\/p>\n<h2>\u4e09\u3001C++\u5b8c\u6574\u5b9e\u73b0\u4e0e\u4f7f\u7528\u793a\u4f8b<\/h2>\n<h3>1. \u5b9a\u65f6\u5668\u7c7b\u5b9e\u73b0<\/h3>\n<pre><code class=\"language-cpp\">#pragma once\r\n#include &lt;thread&gt;\r\n#include &lt;atomic&gt;\r\n#include &lt;functional&gt;\r\n#include &lt;chrono&gt;\r\n#include &lt;memory&gt;\r\n\r\nclass Timer {\r\npublic:\r\n    using Callback = std::function&lt;void()&gt;;\r\n\r\n    Timer(int interval, Callback function) {\r\n        impl = std::make_shared&lt;Impl&gt;(interval, std::move(function));\r\n    }\r\n\r\n    void start() {\r\n        std::thread([impl = impl]() {\r\n            if (!impl-&gt;active.load()) return;\r\n            std::this_thread::sleep_for(std::chrono::seconds(impl-&gt;interval));\r\n            if (impl-&gt;active.load()) impl-&gt;function();\r\n        }).detach();\r\n    }\r\n\r\n    void cancel() {\r\n        impl-&gt;active.store(false);\r\n    }\r\n\r\nprivate:\r\n    struct Impl {\r\n        Impl(int i, Callback f) : interval(i), function(std::move(f)) {}\r\n        int interval;\r\n        Callback function;\r\n        std::atomic&lt;bool&gt; active{true};\r\n    };\r\n    std::shared_ptr&lt;Impl&gt; impl;\r\n};\r\n<\/code><\/pre>\n<h3>2. \u6d4b\u8bd5\u9a71\u52a8\u7a0b\u5e8f<\/h3>\n<pre><code class=\"language-cpp\">#include \"timer.hpp\"\r\n#include &lt;iostream&gt;\r\n#include &lt;memory&gt;\r\n#include &lt;string&gt;\r\n\r\nstruct Alarm {\r\n    Alarm(int s, const std::string&amp; m) : seconds(s), message(m) {}\r\n    int seconds;\r\n    std::string message;\r\n};\r\n\r\nvoid callback(std::shared_ptr&lt;Alarm&gt; alarm) {\r\n    std::cout &lt;&lt; \"(\" &lt;&lt; alarm-&gt;seconds &lt;&lt; \") \" &lt;&lt; alarm-&gt;message &lt;&lt; std::endl;\r\n}\r\n\r\nint main() {\r\n    std::string input;\r\n    while (true) {\r\n        std::cout &lt;&lt; \"Alarm&gt; \";\r\n        if (!std::getline(std::cin, input) || input.empty()) continue;\r\n\r\n        try {\r\n            auto pos = input.find(' ');\r\n            if (pos == std::string::npos) throw std::runtime_error(\"Invalid input\");\r\n            int seconds = std::stoi(input.substr(0, pos));\r\n            std::string msg = input.substr(pos + 1);\r\n            auto alarm = std::make_shared&lt;Alarm&gt;(seconds, msg);\r\n            Timer t(seconds, std::bind(callback, alarm));\r\n            t.start();\r\n        } catch (...) {\r\n            std::cout &lt;&lt; \"Bad command\" &lt;&lt; std::endl;\r\n        }\r\n    }\r\n}\r\n<\/code><\/pre>\n<p>\u6b64\u7a0b\u5e8f\u63a5\u53d7\u7528\u6237\u8f93\u5165\u7684\u79d2\u6570\u4e0e\u6d88\u606f\u5185\u5bb9\uff0c\u901a\u8fc7 C++ \u5b9e\u73b0\u7684\u5b9a\u65f6\u5668\u7c7b\u5b9e\u73b0\u7c7b\u4f3c Python \u7684\u5ef6\u65f6\u63d0\u793a\u529f\u80fd\u3002<\/p>\n<h2>\u56db\u3001\u9002\u7528\u4e8e\u9ad8\u6027\u80fd\u670d\u52a1\u5668\u7684\u8bbe\u8ba1\u8003\u91cf<\/h2>\n<p>\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u90e8\u7f72\u8be5\u5b9a\u65f6\u5668\u7c7b\u65f6\uff0c\u6211\u4eec\u63a8\u8350\u4f7f\u7528\u7cbe\u5ea6\u66f4\u9ad8\u7684\u65f6\u95f4\u63a7\u5236\uff0c\u6bd4\u5982\u6beb\u79d2\u6216\u5fae\u79d2\u7ea7\u5ef6\u8fdf\u63a7\u5236\uff0c\u5e76\u8003\u8651\u7ebf\u7a0b\u6c60\u66ff\u4ee3 <code>std::thread::detach()<\/code> \u6765\u51cf\u5c11\u8d44\u6e90\u5f00\u9500\u3002<\/p>\n<p>\u5bf9\u4e8e\u90e8\u7f72\u5728<strong>\u7f8e\u56fd\u4e91\u670d\u52a1\u5668<\/strong>\u4e0a\u7684 C++ \u5e94\u7528\uff0c\u8fd9\u79cd\u8f7b\u91cf\u7ea7\u3001\u8de8\u5e73\u53f0\u3001\u65e0\u7b2c\u4e09\u65b9\u4f9d\u8d56\u7684\u5b9a\u65f6\u5668\u7c7b\u53ef\u4ee5\u7a33\u5b9a\u8fd0\u884c\uff0c\u7279\u522b\u9002\u7528\u4e8e\u9700\u8981\u5468\u671f\u6027\u4efb\u52a1\u6216\u5ef6\u8fdf\u4efb\u52a1\u7684\u573a\u666f\uff0c\u5982\u81ea\u52a8\u91cd\u8bd5\u673a\u5236\u3001\u5fc3\u8df3\u68c0\u6d4b\u3001\u5f02\u6b65\u6570\u636e\u6e05\u6d17\u7b49\u3002<\/p>\n<hr \/>\n<h2>\u4e94\u3001\u603b\u7ed3\u4e0e\u63a8\u5e7f<\/h2>\n<p>\u901a\u8fc7\u672c\u6587\u7684\u5b66\u4e60\uff0c\u6211\u4eec\u4ece\u539f\u7406\u8bb2\u8d77\uff0c\u9010\u6b65\u5b8c\u6210\u4e86\u4e00\u4e2a\u53ef\u5b9e\u9645\u90e8\u7f72\u7684 C++ \u5b9a\u65f6\u5668\u7c7b\u7684\u5b9e\u73b0\u3002\u76f8\u6bd4\u4f7f\u7528\u5916\u90e8\u5e93\uff0c\u672c\u6587\u65b9\u6848\u5177\u5907\u66f4\u9ad8\u7684\u7075\u6d3b\u6027\u4e0e\u53ef\u7ef4\u62a4\u6027\uff0c\u9002\u5408\u90e8\u7f72\u5728\u4efb\u610f\u5e73\u53f0\uff0c\u5c24\u5176\u662f\u8fd0\u884c\u5728<strong>\u7f8e\u56fdVPS<\/strong>\u3001<strong>\u7f8e\u56fd\u4e91\u670d\u52a1\u5668<\/strong>\u4e0a\u7684\u591a\u7ebf\u7a0b\u5e94\u7528\u3002<\/p>\n<p>\u5982\u60a8\u6b63\u5728\u5bfb\u627e\u9ad8\u6027\u80fd\u7684\u4e91\u8ba1\u7b97\u5e73\u53f0\u6765\u90e8\u7f72\u7c7b\u4f3c\u7684\u5b9a\u65f6\u4efb\u52a1\u8c03\u5ea6\u7a0b\u5e8f\uff0c\u63a8\u8350\u9009\u62e9\u53ef\u9760\u7684<a href=\"https:\/\/fwq.ai\/\"><strong>\u7f8e\u56fd\u670d\u52a1\u5668<\/strong><\/a>\u63d0\u4f9b\u5546\uff0c\u4eab\u53d7\u4f4e\u5ef6\u8fdf\u3001\u9ad8\u5e26\u5bbd\u3001\u7a33\u5b9a\u8fd0\u884c\u7684\u5f00\u53d1\u4e0e\u90e8\u7f72\u73af\u5883\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 \u5728\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u5f00\u53d1\u4e2d\uff0c\u5b9a\u65f6\u5668\u662f\u4e00\u4e2a\u5e38\u89c1\u4e14\u5173\u952e\u7684\u7ec4\u4ef6\u3002\u5c24\u5176\u662f\u5728\u8fdb\u884c\u670d\u52a1\u5668\u7aef\u5f00\u53d1\u3001\u4efb\u52a1\u8c03\u5ea6\u6216\u8d44\u6e90\u76d1\u63a7\u7b49\u5de5\u4f5c\u65f6\uff0c\u51c6\u786e\u5730\u8bbe\u5b9a\u5ef6\u65f6\u4efb\u52a1\u6267\u884c\u663e\u5f97\u5c24\u4e3a\u91cd\u8981\u3002\u672c\u6587\u5c06\u5e26\u60a8\u4e00\u6b65\u6b65\u7528 C++ \u6807\u51c6\u5e93\u5b9e\u73b0\u4e00\u4e2a\u529f\u80fd\u5b8c\u6574\u7684\u5b9a\u65f6\u5668\u7c7b\uff0c\u517c\u5bb9\u73b0\u4ee3 C++ \u5f00\u53d1\u65b9\u5f0f\uff0c\u540c\u65f6\u4e5f\u9002\u7528\u4e8e\u8fd0\u884c\u5728\u7f8e\u56fd\u670d\u52a1\u5668\u73af\u5883\u4e2d\u7684\u591a\u7ebf\u7a0b\u5e94\u7528\u3002 \u4e00\u3001\u5b9a\u65f6\u5668\u539f\u7406\u7b80\u4ecb \u5b9a\u65f6\u5668\u7684\u5b9e\u73b0\u601d\u8def\u672c\u8d28\u4e0a\u975e\u5e38\u7b80\u5355\uff1a \u521b\u5efa\u4e00\u4e2a\u72ec\u7acb\u7684\u7ebf\u7a0b\uff1b \u8ba9\u8be5\u7ebf\u7a0b\u4f11\u7720\u6307\u5b9a\u65f6\u957f\uff1b \u4f11\u7720\u7ed3\u675f\u540e\u6267\u884c\u9884\u8bbe\u4efb\u52a1\u3002 \u5728 Python \u4e2d\uff0c\u8fd9\u4e00\u903b\u8f91\u7531 threading.Timer \u5b8c\u7f8e\u8be0\u91ca\uff1a from threading import Timer def greet(): print(&#8220;Hello, World!&#8221;) t = Timer(10.0, greet) t.start() \u8fd9\u4e2a\u7c7b\u662f Thread \u7684\u5b50\u7c7b\uff0c\u901a\u8fc7 start() \u542f\u52a8\uff0c\u652f\u6301 cancel() \u4e2d\u65ad\u3002C++\u4e2d\u6ca1\u6709\u539f\u751f\u7684 Timer \u7c7b\uff0c\u4f46\u901a\u8fc7 &lt;thread&gt;\u3001&lt;chrono&gt; \u548c std::function \u7b49\u6807\u51c6\u5e93\uff0c\u6211\u4eec\u5b8c\u5168\u53ef\u4ee5\u6784\u9020\u4e00\u4e2a\u529f\u80fd\u76f8\u4eff\u7684\u5b9a\u65f6\u5668\u5de5\u5177\u3002 \u4e8c\u3001C++\u7248\u5b9a\u65f6\u5668\u7c7b\u63a5\u53e3\u8bbe\u8ba1 \u6a21\u4eff Python \u7684 threading.Timer \u63a5\u53e3\uff0c\u6211\u4eec\u8bbe\u8ba1\u5982\u4e0b\u7684 C++ \u5b9a\u65f6\u5668\u7c7b\uff1a class Timer { public: typedef [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-68714","post","type-post","status-publish","format-standard","hentry","category-coding"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/68714","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=68714"}],"version-history":[{"count":1,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/68714\/revisions"}],"predecessor-version":[{"id":68715,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/68714\/revisions\/68715"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=68714"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=68714"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=68714"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}