{"id":112,"date":"2017-05-19T14:14:10","date_gmt":"2017-05-19T06:14:10","guid":{"rendered":"http:\/\/www.myway5.com\/?p=112"},"modified":"2017-08-21T09:40:33","modified_gmt":"2017-08-21T01:40:33","slug":"cpp11-implement-cross-platform-timer","status":"publish","type":"post","link":"https:\/\/www.myway5.com\/index.php\/2017\/05\/19\/cpp11-implement-cross-platform-timer\/","title":{"rendered":"c++11\u5b9e\u73b0\u7684\u8de8\u5e73\u53f0\u5b9a\u65f6\u5668"},"content":{"rendered":"<h2>\u4e00\u3001\u5f15\u8a00<\/h2>\n<p>\u5728\u4e00\u4e2a\u6bd4\u8f83\u5927\u7684\u7cfb\u7edf\u5f53\u4e2d\uff0c\u5f88\u591a\u5730\u65b9\u90fd\u9700\u8981\u5b9a\u65f6\u529f\u80fd\uff0c\u6bd4\u5982\u67d0\u4e9b\u5b58\u5728\u5185\u5b58\u4e2d\u7684\u6570\u636e\uff0c\u9700\u8981\u5b9a\u65f6\u7684\u505a\u6301\u4e45\u5316\uff08\u5b9e\u65f6\u6301\u4e45\u5316\u53ef\u80fd\u5e26\u6765\u5f88\u5927\u7684\u6027\u80fd\u95ee\u9898\uff09\uff0c\u5982\u679c\u8fd9\u4e2a\u7cfb\u7edf\u8981\u6c42\u8de8\u5e73\u53f0\u3002\u90a3\u4e48\u5b9e\u73b0\u4e00\u4e2a\u8de8\u5e73\u53f0\u7684\u5b9a\u65f6\u5668\u5c31\u5f88\u6709\u5fc5\u8981\u4e86\u3002<br \/>\nc++11\u7684\u6807\u51c6\u4e2d\uff0c\u5b9e\u73b0\u4e86\u591a\u7ebf\u7a0b,\u5934\u6587\u4ef6\u4e3a<code>&lt;thread&gt;<\/code>,\u4e5f\u6709\u4e86\u7cfb\u7edf\u65f6\u95f4\u76f8\u5173\u7684\u5e93\uff0c\u5934\u6587\u4ef6\u4e3a<code>&lt;chrono&gt;<\/code>,\u8fd9\u4f7f\u5f97\u4f7f\u7528c++11\u5b9e\u73b0\u8de8\u5e73\u53f0\u7684\u5b9a\u65f6\u5668\u6709\u4e86\u53ef\u80fd\u3002\u5728\u8fd9\u4e4b\u524d\uff0c\u591a\u7ebf\u7a0b\u4e00\u822c\u90fd\u662f\u4f7f\u7528\u64cd\u4f5c\u7cfb\u7edf\u76f8\u5173\u7684\u5e93\u3002<\/p>\n<h2>\u4e8c\u3001 \u5b9a\u65f6\u5668\u7684\u57fa\u672c\u529f\u80fd<\/h2>\n<p>\u5728\u5b9a\u65f6\u5668\u7684\u5b9e\u73b0\u4e4b\u524d\uff0c\u9700\u8981\u4e8b\u5148\u8ba1\u5212\u597d\u4e00\u4e2a\u5b9a\u65f6\u5668\u7684\u57fa\u672c\u529f\u80fd\u3002\u5927\u6982\u7684\u529f\u80fd\u5982\u4e0b\uff1a<br \/>\n1.\u6dfb\u52a0\u4e00\u4e2a\u5b9a\u65f6\u4e8b\u4ef6<br \/>\n2.\u5220\u9664\u4e00\u4e2a\u6307\u5b9a\u7684\u5b9a\u65f6\u4e8b\u4ef6<br \/>\n3.\u5b9a\u65f6\u4e8b\u4ef6\u652f\u6301\u53ea\u6267\u884c\u4e00\u6b21\u6216\u8005\u91cd\u590d\u6267\u884c<br \/>\n4.\u5b9a\u65f6\u5668\u542f\u52a8\u6307\u4ee4<br \/>\n\u8fd9\u4e2a\u90e8\u5206\u7684\u5185\u5bb9\u5728<a href=\"https:\/\/www.ibm.com\/developerworks\/cn\/linux\/l-cn-timers\/index.html\" target=\"_blank\">Linux \u4e0b\u5b9a\u65f6\u5668\u7684\u5b9e\u73b0\u65b9\u5f0f\u5206\u6790<\/a>\u4e2d\u6709\u5f88\u8be6\u7ec6\u7684\u4ecb\u7ecd<\/p>\n<h2>\u4e09\u3001 \u6570\u636e\u7ed3\u6784\u77e5\u8bc6\u9884\u5907<\/h2>\n<p>\u5728\u5b9e\u73b0\u5b9a\u65f6\u5668\u4e2d\uff0c\u9700\u8981\u6709\u4e00\u4e2a\u5bb9\u5668\u53bb\u5b58\u50a8\u6240\u6709\u7684\u5b9a\u65f6\u4e8b\u4ef6\uff0c\u6bd4\u5982\u8bf4\u6700\u5bb9\u6613\u60f3\u5230\u7684\u6570\u7ec4\uff0c\u5982\u679c\u6211\u4eec\u7528\u6570\u7ec4\u4fdd\u5b58\u5b9a\u65f6\u4e8b\u4ef6\uff0c\u90a3\u4e48\u5728\u627e\u51fa\u5f53\u524d\u9700\u8981\u6267\u884c\u7684\u5b9a\u65f6\u4e8b\u4ef6\u7684\u4f2a\u4ee3\u7801\u5c31\u662f<\/p>\n<pre class=\"line-numbers prism-highlight\" data-start=\"1\"><code class=\"language-null\">    for(i = 0 ; i &lt; events_array_size; ++i){\n        if(events[i].time &lt;= current_time){\n            exec_in_child_thread(events);\n        }\n    }\n<\/code><\/pre>\n<p>\u8fd9\u91cc\u5c31\u662f\u904d\u5386\u6240\u6709\u7684\u4e8b\u4ef6\uff0c\u5c06\u5df2\u7ecf\u5230\u4e86\u6267\u884c\u65f6\u95f4\u7684\u4e8b\u4ef6\u653e\u5230\u5b50\u7ebf\u7a0b\u4e2d\u53bb\u6267\u884c\u3002<\/p>\n<p>\u5728\u8fd9\u91cc\u6709\u4e00\u4e2a\u53ef\u4ee5\u4f18\u5316\u6548\u7387\u95ee\u9898\uff1a\u6ce8\u610f\u5230\u6bcf\u4e00\u6b21\u67e5\u8be2\u662f\u5426\u6709\u4e8b\u4ef6\u5230\u4e86\u6267\u884c\u65f6\u95f4\uff0c\u90fd\u4f1a\u904d\u5386\u6574\u4e2a\u6570\u7ec4\uff0c\u4e5f\u5c31\u662fO(n)\u7684\u590d\u6742\u5ea6\uff0c\u5e76\u4e14\u67e5\u8be2\u5468\u671f\u4e5f\u662f\u975e\u5e38\u77ed\u7684\uff0c\u6240\u4ee5\u4f1a\u6709\u975e\u5e38\u5927\u7684\u6027\u80fd\u95ee\u9898\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u8fd9\u91cc\u7684\u6570\u7ec4\u53d8\u6210\u6709\u5e8f\u7684\uff0c\u6bcf\u4e00\u6b21\u53ea\u9700\u67e5\u8be2\u7b2c\u4e00\u4e2a\u4e8b\u4ef6\uff0c\u5982\u679c\u8fd8\u6ca1\u5230\u6267\u884c\u65f6\u95f4\uff0c\u90a3\u4e48\u4e4b\u540e\u7684\u4e8b\u4ef6\u5c31\u4e0d\u7528\u904d\u5386\u4e86\u3002\u8fd9\u6837\u5e73\u5747\u4e0b\u6765\u5c31\u662fO(1)\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u3002\u8003\u8651\u5230\u6211\u4eec\u5728\u63d2\u5165\uff0c\u5220\u9664\u65f6\u59cb\u7ec8\u8981\u4fdd\u8bc1\u6570\u7ec4\u7684\u6709\u5e8f\uff0c\u53ef\u4ee5\u91c7\u7528\u6700\u5c0f\u5806\u3002\u4e5f\u5c31\u662f\u5806\u9876\u59cb\u7ec8\u662f\u6700\u5148\u5230\u6267\u884c\u65f6\u95f4\u7684\u4e00\u9879\u3002<\/p>\n<h2>\u56db\u3001c++11\u591a\u7ebf\u7a0b\u7684\u77e5\u8bc6\u9884\u5907<\/h2>\n<h3>4.1 join()\u8fd8\u662fdetach()<\/h3>\n<p>c++11\u4e2d\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u5f88\u5bb9\u6613<\/p>\n<pre class=\"line-numbers prism-highlight\" data-start=\"1\"><code class=\"language-null\">#include &lt;iostream&gt;\n#include &lt;thread&gt;\n\nvoid function_1(){\n    std::cout &lt;&lt; \"hello world\" &lt;&lt; std::endl;\n    while(true){}\n}\n\nint main(){\n    std::thread t1(function_1);\n    t1.join();      \/\/\u52a0\u5165\u8fd0\u884c\n\n    std::cout &lt;&lt; \"hahah\" &lt;&lt; std::endl;\n\n    return 0;\n}\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c31\u662f\u521b\u5efa\u4e86\u4e00\u4e2at1\u7684\u7ebf\u7a0b\uff0c\u6765\u6267\u884cfunction_1\u51fd\u6570\u3002\u6ce8\u610f\u5230\u6211\u4eec\u8fd9\u91cc\u4f7f\u7528\u4e86t1.join(),\u5b57\u9762\u4e0a\u7684\u610f\u601d\u662f\u5c06\u5b50\u7ebf\u7a0b\u52a0\u5165\u7684\u4e3b\u7ebf\u7a0b\u6765\u6267\u884c\uff0c\u8fd9\u6837\u5bfc\u81f4\u7684\u7ed3\u679c\u662f\u4e3b\u7ebf\u7a0b\u4f1a\u5728t1.join()\u8fd9\u91cc\u505c\u6b62\uff0c\u76f4\u5230t1\u6267\u884c\u7ed3\u675f\u540e\u518d\u5f80\u4e0b\u6267\u884c\u3002\u5728\u8fd9\u91ccfunction_1\u4e2d\u5b58\u5728\u6b7b\u5faa\u73af\uff0c\u6240\u4ee5hahah\u6c38\u8fdc\u4e0d\u4f1a\u88ab\u8f93\u51fa\u3002\u800c\u5982\u679c\u4f7f\u7528t1.detach()\u7684\u8bdd\uff0c\u5373\u5c06\u5b50\u7ebf\u7a0b\u4ece\u4e3b\u7ebf\u7a0b\u5206\u79bb\u51fa\u53bb\uff0c\u90a3\u4e48\u4e3b\u7ebf\u7a0b\u5c31\u4f1a\u4e00\u76f4\u5411\u4e0b\u6267\u884c\uff0c\u5728\u8fd9\u91cc\u5c31\u4f1a\u6267\u884c\u5230return 0,\u7136\u540e\u4e3b\u7ebf\u7a0b\u9500\u6bc1\uff0cdetach\u51fa\u53bb\u7684\u5b50\u7ebf\u7a0b\u53ef\u80fd\u5c31\u6ca1\u6709\u4efb\u4f55\u663e\u793a\u7684\u673a\u4f1a\u4e86\uff08\u67e5\u8fc7\u8d44\u6599\u8bf4detach\u51fa\u53bb\u5b50\u7ebf\u7a0b\u7684\u884c\u4e3a\u7684undefined\u7684\uff09\u3002<\/p>\n<p>\u5728\u8fd9\u4e2a\u5b9a\u65f6\u5668\u7684\u5b9e\u73b0\u4e2d\uff0c\u5f53\u7136\u4e0d\u80fd\u4f7f\u7528join(),\u56e0\u4e3a\u4f1a\u963b\u585e\u7a0b\u5e8f\u7684\u6b63\u5e38\u8fd0\u884c\u3002<\/p>\n<h3>4.2 \u5982\u4f55\u4f7f\u5f97\u5b9a\u65f6\u5668\u7684\u4f7f\u7528\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002<\/h3>\n<p>\u5728\u4e00\u4e2a\u7a0b\u5e8f\u4e2d\uff0c\u53ef\u80fd\u5f88\u591a\u5730\u65b9\u4f1a\u4f7f\u7528\u5230\u5b9a\u65f6\u5668\u53bb\u89e6\u53d1\u67d0\u4e2a\u52a8\u4f5c\uff0c\u4f46\u662f\u4e3a\u4e86\u6548\u7387\uff0c\u80af\u5b9a\u4e0d\u4f1a\u521b\u5efa\u591a\u4e2a\u5b9a\u65f6\u5668\u5b9e\u4f8b\uff0c\u56e0\u6b64\u5b9a\u65f6\u5668\u7684\u5b9e\u73b0\u5e94\u8be5\u662f\u5355\u4f8b\u7684\u3002\u56e0\u4e3a\u9700\u8981\u5355\u4f8b\uff0c\u56e0\u6b64\u5b9a\u65f6\u5668\u7684\u521b\u5efa\uff0c\u5b9a\u65f6\u5668\u4e2d\u5b9a\u65f6\u4e8b\u4ef6\u7684\u6dfb\u52a0\u548c\u5220\u9664\u90fd\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff08\u56e0\u4e3a\u540c\u4e00\u65f6\u95f4\u53ef\u80fd\u4f1a\u6709\u591a\u4e2a\u7ebf\u7a0b\u64cd\u4f5c\u5b9a\u65f6\u5668\uff09\u3002<\/p>\n<p>4.2.1 \u7ebf\u7a0b\u5b89\u5168\u7684\u5355\u4f8b\u6a21\u5f0f<\/p>\n<p>\u5355\u4f8b\u6a21\u5f0f\u7684\u5b9e\u73b0\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u56e0\u4e3a\u80fd\u529b\u95ee\u9898\uff0c\u4e0d\u5bf9\u8fd9\u4e00\u70b9\u591a\u505a\u53d9\u8ff0\u3002\u53ef\u4ee5\u770b\u8fd9\u4e00\u7bc7<a href=\"http:\/\/www.zkt.name\/dan-li-mo-shi-singleton-ji-c-shi-xian\/\" target=\"_blank\">\u5355\u4f8b\u6a21\u5f0f(Singleton)\u53ca\u5176C++\u5b9e\u73b0<\/a>\u3002<\/p>\n<p>\u6211\u8fd9\u91cc\u4f7f\u7528\u7684\u662f\u4f7f\u7528static\u6765\u521b\u5efa\u5c40\u90e8\u53ef\u89c1\u7684\u5168\u5c40\u53d8\u91cf<\/p>\n<pre class=\"line-numbers prism-highlight\" data-start=\"1\"><code class=\"language-null\">static Timer* getInstance(std::chrono::milliseconds tick){\n    static Timer timer(tick);\n    return &amp;timer;\n}\n<\/code><\/pre>\n<p>4.2.2 \u4f7f\u7528\u9501\u4fdd\u8bc1\u5b9a\u65f6\u4e8b\u4ef6\u7684\u6dfb\u52a0\u548c\u5220\u9664\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002<\/p>\n<p>\u5728\u5f80\u5b9a\u65f6\u5668\u4e2d\u6dfb\u52a0\u548c\u5220\u9664\u5b9a\u65f6\u4e8b\u4ef6\u65f6\uff0c\u5c31\u662f\u5728\u505a\u6700\u5c0f\u5806\u7684\u6dfb\u52a0\u548c\u5220\u9664\u3002\u8fd9\u4e2a\u4e2d\u95f4\u7ebf\u7a0b\u4e0d\u5b89\u5168\u7684\u73b0\u8c61\u4e3a:\u4ee5\u6dfb\u52a0\u4e3a\u4f8b\uff0c\u5f80\u6700\u5c0f\u5806\u6570\u7ec4\u672b\u5c3e\u52a0\u5165\u4e00\u4e2a\u6570\u636e\uff0c\u7136\u540e\u518d\u9010\u5c42\u5411\u4e0a\u8c03\u6574\u3002\u5982\u679c\u5728\u8fd9\u4e2a\u8c03\u6574\u7684\u8fc7\u7a0b\u4e2d\uff0c\u53e6\u5916\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884c\u4e86\u5220\u9664\u64cd\u4f5c\uff0c\u4e24\u4e2a\u8c03\u6574\u8fc7\u7a0b\u5c31\u53ef\u80fd\u540c\u65f6\u53d1\u751f\uff0c\u9020\u6210\u4e0d\u53ef\u9884\u77e5\u7684\u9519\u8bef\uff0c\u4f7f\u5f97\u6700\u5c0f\u5806\u5e76\u4e0d\u6709\u5e8f\u3002<\/p>\n<p>\u6240\u4ee5\u6dfb\u52a0\u548c\u5220\u9664\u64cd\u4f5c\u5e94\u8be5\u662f\u4e92\u65a5\u7684\uff0c\u53ea\u6709\u5176\u4e2d\u4e00\u4e2a\u64cd\u4f5c\u6267\u884c\u7ed3\u675f\uff0c\u53e6\u4e00\u4e2a\u64cd\u4f5c\u624d\u53ef\u4ee5\u5f00\u59cb\u3002c++\u4e2d\u53ef\u4ee5\u4f7f\u7528\u4fe1\u53f7\u91cfmutex\u548c\u9501locker\u8f7b\u677e\u7684\u5b8c\u6210\u4e92\u65a5\u7684\u9700\u6c42\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"line-numbers prism-highlight\" data-start=\"1\"><code class=\"language-null\">#include &lt;iostream&gt;\n#include &lt;thread&gt;\n#include &lt;string&gt;\n#include &lt;mutex&gt;\n#include &lt;fstream&gt;\n\nclass LofFile{\n    public:\n        LofFile(){\n            f.open(\"log.txt\");\n        }\n\n        void shared_print(std::string id,int value){\n            std::lock(m_mutex,m_mutex2);\n            std::lock_guard&lt;std::mutex&gt; locker(m_mutex,std::adopt_lock);\n            std::lock_guard&lt;std::mutex&gt; locker2(m_mutex2,std::adopt_lock);\n            std::cout &lt;&lt; \"from\" &lt;&lt; id &lt;&lt; \":\" &lt;&lt; value &lt;&lt; std::endl;\n        }\n\n        void shared_print2(std::string id,int value){\n            std::lock(m_mutex,m_mutex2);\n            std::lock_guard&lt;std::mutex&gt; locker2(m_mutex2,std::adopt_lock);      \/\/lock_guard\u662f\u4e3a\u4e86\u9632\u6b62\u8bed\u53e5\u6267\u884c\u4e2d\u51fa\u73b0\u5f02\u5e38\uff0c\u9501\u4e0d\u88ab\u91ca\u653e\u3002\u8fd9\u6837\u505a\u53ef\u4ee5\u4fdd\u8bc1\u5728\u9000\u51fa\u4ee3\u7801\u5757\u540e\u89e3\u9501\n            std::lock_guard&lt;std::mutex&gt; locker(m_mutex,std::adopt_lock);\n            std::cout &lt;&lt; \"from\" &lt;&lt; id &lt;&lt; \":\" &lt;&lt; value &lt;&lt; std::endl;\n        }\n\n    protected:\n    private:\n        std::mutex m_mutex;     \/\/\u4f7f\u7528\u4fe1\u53f7\u91cf\u6765\u89e3\u51b3\u8d44\u6e90\u7ade\u4e89\n        std::mutex m_mutex2;\n        std::ofstream f;\n\n};\n\nvoid function_1(LofFile&amp; log){\n    for(int i = 0; i &gt; -100; i--){\n        log.shared_print(\"From t1:\",i);\n    }\n};\n\nint main(){\n    LofFile log;\n    std::thread t1(function_1,std::ref(log));\/\/\u7ebf\u7a0b\u5f00\u59cb\u8fd0\u884c\n    for(int i = 0; i &lt; 100; i++){\n        log.shared_print2(\"From main\",i);\n    }\n\n    t1.join();\n    return 0;\n}\n<\/code><\/pre>\n<h2>\u4e94\u3001\u603b\u7ed3\u4e0e\u5b9e\u73b0<\/h2>\n<p>\u5206\u6790\u5230\u8fd9\u91cc\uff0c\u4e00\u4e2a\u5b9a\u65f6\u5668\u7684\u57fa\u672c\u8981\u7d20\u5df2\u7ecf\u5177\u5907\u4e86\u3002<br \/>\n    1.\u7528\u6765\u5b58\u50a8\u5b9a\u65f6\u4e8b\u4ef6\uff0c\u5e76\u4e14\u6dfb\u52a0\u548c\u5220\u9664\u64cd\u4f5c\u90fd\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u6700\u5c0f\u5806<br \/>\n    2.\u53ef\u4ee5\u591a\u7ebf\u7a0b\u5b9e\u73b0\u5b9a\u65f6\u4e8b\u4ef6\u7684\u6267\u884c\u4ece\u800c\u4e0d\u963b\u585e\u4e3b\u7ebf\u7a0b\u3002<\/p>\n<p>\u4e0b\u9762\u8d34\u51fa\u6240\u6709\u7684\u5b9e\u73b0\u4ee3\u7801\uff1a<\/p>\n<h3>5.1 \u6700\u5c0f\u5806\u7684\u5b9e\u73b0\u3002<\/h3>\n<p>SortedHeap.hpp<\/p>\n<pre class=\"line-numbers prism-highlight\" data-start=\"1\"><code class=\"language-null\">\/**\n * \u6392\u5e8f\u5806\u7684\u5b9e\u73b0\n * \u5806\u4f7f\u7528\u7684\u662f\u5b8c\u5168\u4e8c\u53c9\u6811\u7684\u7ed3\u6784\uff0c\u4f7f\u7528\u6570\u7ec4\uff08\u6570\u7ec4\u4ece\u96f6\u5f00\u59cb\uff09\u4fdd\u5b58\uff0c\u90a3\u4e48\u6700\u540e\u4e00\u4e2a\u975e\u53f6\u5b50\u8282\u70b9\u4e3a(n - 1) \/ 2\n * \u5806\u7684\u6784\u5efa\u4e00\u76f4\u90fd\u662f\u5728\u6709\u5e8f\u7684\u57fa\u7840\u4e0a\u7684\uff0c\u90a3\u4e48\u6bcf\u6b21\u8c03\u6574\u53ea\u9700\u6bd4\u8f83i\u548c(i - 1) \/ 2\u7684\u5143\u7d20\uff0c\u4f9d\u6b21\u4e0a\u63a8\n * \u652f\u6301\u4efb\u610f\u7c7b\u7684\u6392\u5e8f\n * \u5f53\u524d\u8fd8\u4e0d\u652f\u6301\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u7684\u4f7f\u7528\n * author:jiangpengfie\n * date:2017-05-09\n *\/\n#ifndef SORTEDHEAP_H\n#define SORTEDHEAP_H\n#include &lt;iostream&gt;\n#include &lt;vector&gt;\n#include &lt;functional&gt;\n#include &lt;memory&gt;\n#include &lt;mutex&gt;\n#include &lt;condition_variable&gt;\n#include \"src\/core\/util\/util.h\"\n\n\ntemplate&lt;class T&gt;\nclass SortedHeap{\n    private:\n        struct HeapNode{\n            unsigned int id;\n            T obj;\n            HeapNode(unsigned int id,T t):obj(t){\n                this-&gt;id = id;\n            }\n        };\n        std::vector&lt;HeapNode&gt; heap;\n        unsigned int autoIncrementId;\n        std::function&lt;bool(T&amp; ,T&amp;)&gt; cmp;    \/\/\u6bd4\u8f83\u51fd\u6570\uff0c\u5b9e\u73b0\u9009\u62e9\u6784\u9020\u6700\u5927\u5806\u8fd8\u662f\u6700\u5c0f\u5806\n        std::mutex mu1;                \n        std::mutex mu2;                \n\n        \/**\n         * \u63d2\u5165\u8282\u70b9\u540e\u8c03\u6574\u5806\u4e2d\u4e0d\u7b26\u5408\u7684\u8282\u70b9\n         *\/\n        void adjustAfterInsert();\n\n        \/**\n         * pop\u51fa\u5806\u9876\u5143\u7d20\u540e\u8c03\u6574\u5806\u4e2d\u4e0d\u7b26\u5408\u7684\u8282\u70b9\n         *\/\n        void adjustAfterPopTop();\n\n        \/**\n         * \u5220\u9664\u8282\u70b9\u540e\u8c03\u6574\u5806\u4e2d\u4e0d\u7b26\u5408\u7684\u8282\u70b9\n         * @param i \u5220\u9664\u7684\u8282\u70b9id\n         *\/\n        void adjustAfterDelete(int id);         \n\n        void swap(HeapNode&amp; t1,HeapNode&amp; t2);\n\n        void deleteNodeByPos(const unsigned int pos);\n    public:\n        \/**\n         * \u6784\u9020\u51fd\u6570\n         * @param cmp \u7528\u6765\u6bd4\u8f83\n         *\/\n        SortedHeap(std::function&lt;bool(T&amp;,T&amp;)&gt; cmp);\n        \/**\n         * \u63d2\u5165\u8282\u70b9\n         * @param node \u63d2\u5165\u7684\u8282\u70b9\n         *\/\n        unsigned int insertNode(T&amp; node);\n        \/**\n         * \u5220\u9664\u8282\u70b9\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n)\n         * @param id  \u8981\u5220\u9664\u7684\u8282\u70b9id\n         *\/\n        void deleteNode(unsigned int id);\n\n        \/**\n         * pop\u6700\u5c0f\u7684\u8282\u70b9\n         * @return T* \u8fd4\u56de\u7684\u6700\u9876\u90e8\u7684\u8282\u70b9\u6307\u9488\n         *\/\n        std::unique_ptr&lt;T&gt; popTopNode();\n\n        \/**\n         * \u83b7\u53d6\u6700\u9876\u90e8\u7684\u8282\u70b9\n         * @return T \u6700\u9876\u90e8\u7684\u8282\u70b9\u6307\u9488\n         *\/\n        std::unique_ptr&lt;T&gt; getTopNode();\n\n        \/**\n         * \u5220\u9664\u9876\u90e8\u7684\u8282\u70b9\n         *\n         *\/\n        void deleteTopNode();\n};\n\ntemplate&lt;typename T&gt;\nSortedHeap&lt;T&gt;::SortedHeap(std::function&lt;bool(T&amp;,T&amp;)&gt; cmp){\n    this-&gt;cmp = cmp;\n    this-&gt;autoIncrementId = 0;\n}\n\ntemplate&lt;typename T&gt;\nvoid SortedHeap&lt;T&gt;::swap(HeapNode&amp; t1,HeapNode&amp; t2){\n    HeapNode tmp = t1;\n    t1 = t2;\n    t2 = tmp;\n}\n\ntemplate&lt;typename T&gt;\nvoid SortedHeap&lt;T&gt;::adjustAfterInsert(){\n    int last = this-&gt;heap.size() - 1;\n    int flag = true;\n    \/\/\u4ece\u63d2\u5165\u7684\u8282\u70b9\u4f4d\u7f6e\u5f00\u59cb\u5411\u4e0a\u8c03\u6574\n    while(last &gt; 0 &amp;&amp; flag){\n        if(this-&gt;cmp(this-&gt;heap[last].obj,this-&gt;heap[(last - 1) \/ 2].obj)){\n            this-&gt;swap(this-&gt;heap[(last - 1) \/ 2],this-&gt;heap[last]);\n        }else{\n            \/\/\u4e0d\u9700\u8981\u8c03\u6574\u4e86\n            flag = false;\n        }\n        last = (last - 1) \/ 2;\n    }\n}\n\ntemplate&lt;typename T&gt;\nvoid SortedHeap&lt;T&gt;::adjustAfterDelete(int pos){\n    \/\/\u4ecepos\u4f4d\u7f6e\u5f00\u59cb\u5411\u4e0b\u8c03\u6574\n    int last = this-&gt;heap.size() - 1;\n    if(last == 0)\n        return;     \/\/\u6700\u540e\u4e00\u4e2a\u4e0d\u9700\u8981\u8c03\u6574\n    bool flag = true;   \/\/\u6807\u8bb0\u662f\u5426\u9700\u8981\u8c03\u6574\n    while(pos &lt;= (last - 1) \/ 2 &amp;&amp; flag){\n        \/\/\u4e00\u76f4\u8c03\u6574\u5230\u6700\u540e\u4e00\u4e2a\u975e\u53f6\u5b50\u7ed3\u70b9\n        int topNum = 0;     \/\/\u8bb0\u5f55\u6700\u5c0f\u7684\u7ed3\u70b9\u7f16\u53f7\n\n          \/\/(pos + 1) * 2 - 1\u662f\u5de6\u5b69\u5b50\uff0cpos\u662f\u7236\n        if(this-&gt;cmp(this-&gt;heap[(pos + 1) * 2 - 1].obj,this-&gt;heap[pos].obj)){\n            topNum = (pos + 1) * 2 - 1;\n        }else{\n            topNum = pos;\n        }\n\n        if((pos + 1) * 2 &lt;= last){\n            \/\/\u5982\u679c\u5b58\u5728\u53f3\u7ed3\u70b9\n            if(this-&gt;cmp(this-&gt;heap[(pos + 1) * 2].obj,this-&gt;heap[topNum].obj)){\n                topNum = (pos + 1) * 2;\n            }\n        }\n\n        \/\/\u770b\u770btopNum\u662f\u4e0d\u662f\u81ea\u5df1\n        if(pos == topNum){\n            \/\/\u662f\u81ea\u5df1\u5c31\u4e0d\u7528\u8c03\u6574\u4e86\n            flag = false;\n        }else{\n            \/\/\u4ea4\u6362\n            this-&gt;swap(this-&gt;heap[pos],this-&gt;heap[topNum]);\n        }\n        pos = topNum;\n    }\n}\n\n\ntemplate&lt;typename T&gt;\nvoid SortedHeap&lt;T&gt;::deleteNodeByPos(const unsigned int pos){\n    unsigned int last = this-&gt;heap.size() - 1;\n    if(pos &gt; last){\n        return;\n    }\n    std::lock(mu1,mu2);             \/\/\u4e0a\u9501\n    std::lock_guard&lt;std::mutex&gt; locker1(mu1,std::adopt_lock);\n    std::lock_guard&lt;std::mutex&gt; locker2(mu2,std::adopt_lock);\n    \/\/\u4e0e\u6700\u540e\u4e00\u4e2a\u4ea4\u6362\n    swap(this-&gt;heap[pos],this-&gt;heap[last]);\n    \/\/\u5220\u9664\u6700\u540e\u4e00\u4e2a\n    this-&gt;heap.pop_back();      \n\n    this-&gt;adjustAfterDelete(pos);\n}\n\n\n\ntemplate&lt;typename T&gt;\nunsigned int SortedHeap&lt;T&gt;::insertNode(T&amp; node){\n    HeapNode hNode(this-&gt;autoIncrementId++,node);\n    std::lock(mu1,mu2);             \/\/\u4e0a\u9501\n    std::lock_guard&lt;std::mutex&gt; locker1(mu1,std::adopt_lock);\n    std::lock_guard&lt;std::mutex&gt; locker2(mu2,std::adopt_lock);\n    this-&gt;heap.push_back(hNode);     \/\/\u5148\u5c06node\u653e\u5728\u6700\u540e\u4e00\u4f4d\n    if(this-&gt;heap.size() != 1){\n        \/\/\u5982\u679c\u5927\u5c0f\u4e0d\u7b49\u4e8e1\uff0c\u5219\u5728\u65b0\u589e\u8282\u70b9\u540e\u8c03\u6574\n        this-&gt;adjustAfterInsert();\n    }\n    return this-&gt;autoIncrementId - 1;\n}\n\n\ntemplate&lt;typename T&gt;\nvoid SortedHeap&lt;T&gt;::deleteNode(unsigned int id){\n    for(unsigned int i = 0; i &lt; this-&gt;heap.size(); i++){\n        if(heap[i].id == id){\n            \/\/\u627e\u5230\u4e86id\n            this-&gt;deleteNodeByPos(i);\n            break;\n        }\n    }\n\n\n}\n\ntemplate&lt;typename T&gt;\nstd::unique_ptr&lt;T&gt; SortedHeap&lt;T&gt;::popTopNode(){\n    if(this-&gt;heap.size() != 0){\n        std::unique_ptr&lt;T&gt; top(new T(this-&gt;heap[0].obj));\n        this-&gt;deleteNodeByPos(0);\n        return top;\n    }else{\n        std::unique_ptr&lt;T&gt; p = nullptr;\n        return p;\n    }\n}\n\ntemplate&lt;typename T&gt;\nstd::unique_ptr&lt;T&gt; SortedHeap&lt;T&gt;::getTopNode(){\n    if(this-&gt;heap.size() != 0){\n        std::unique_ptr&lt;T&gt; top(new T(this-&gt;heap[0].obj));\n        return top;\n    }else{\n        std::unique_ptr&lt;T&gt; p = nullptr;\n        return p;\n    }\n}\n\ntemplate&lt;typename T&gt;\nvoid SortedHeap&lt;T&gt;::deleteTopNode(){\n   if(this-&gt;heap.size() != 0){\n        this-&gt;deleteNodeByPos(0);\n    }\n}\n\n#endif\n\n\n\n<\/code><\/pre>\n<h3>5.2 \u5b9a\u65f6\u5668\u7684\u5b9e\u73b0<\/h3>\n<p>Timer.h<\/p>\n<pre class=\"line-numbers prism-highlight\" data-start=\"1\"><code class=\"language-null\">\/**\n * \u5b9a\u65f6\u5668\u7684\u5b9e\u73b0\n * \u652f\u6301int setTimer(T interval,function action):\u8bbe\u7f6e\u4e00\u4e2a\u5b9a\u65f6\u5668\uff0c\u6307\u5b9a\u95f4\u9694interval\u548c\u56de\u8c03\u51fd\u6570action,\u8fd4\u56de\u5b9a\u65f6\u5668id\n * \u652f\u6301void deleteTimer(int timerId):\u5220\u9664\u4e00\u4e2a\u5b9a\u65f6\u5668\n * \u6570\u636e\u7ed3\u6784:\u6700\u5c0f\u5806\u6a21\u578b\uff0c\u6309\u7167\u5b9a\u65f6\u5668\u89e6\u53d1\u7684\u65f6\u95f4\u6392\u5e8f\n * author:jiangpengfei\n * date:2017-05-09\n *\/\n#ifndef TIMER_H\n#define TIMER_H\n#include &lt;iostream&gt;\n#include &lt;chrono&gt;\n#include &lt;functional&gt;\n#include &lt;thread&gt;\n#include &lt;memory&gt;\n#include \"SortedHeap.hpp\"\n\nclass Timer{\n    private:\n        std::chrono::milliseconds tick;\n        double timeline;     \/\/\u5f53\u524d\u65f6\u95f4\u7ebf,long double\u7684\u5b57\u8282\u6570\u4e3a12\n        bool isStart;        \/\/\u6807\u5fd7\u5f53\u524d\u5b9a\u65f6\u5668\u7684\u542f\u52a8\u72b6\u6001\n        struct SchedulerEvent{\n          unsigned int id;                   \/\/\u5b9a\u65f6\u4e8b\u4ef6\u7684\u552f\u4e00\u6807\u793aid\n          double interval;                   \/\/\u4e8b\u4ef6\u7684\u89e6\u53d1\u95f4\u9694\uff0c\u5728\u91cd\u590d\u4e8b\u4ef6\u4e2d\u4f1a\u7528\u5230\u8fd9\u4e2a\u5c5e\u6027\n          double deadline;                   \/\/\u5b9a\u65f6\u4e8b\u4ef6\u7684\u89e6\u53d1\u65f6\u95f4\n          std::function&lt;void()&gt; action;      \/\/\u89e6\u53d1\u7684\u4e8b\u4ef6\n          bool isRepeat;                     \/\/\u662f\u5426\u662f\u91cd\u590d\u6267\u884c\u4e8b\u4ef6\n          SchedulerEvent( double interval, double timeline,std::function&lt;void()&gt; action,bool isRepeat){\n              this-&gt;interval = interval;\n              this-&gt;deadline = interval + timeline;\n              this-&gt;action = action;\n              this-&gt;isRepeat = isRepeat;\n          }\n        };\n\n        SortedHeap&lt;SchedulerEvent&gt; eventQueue;\n\n        \/**\n         * \u6267\u884c\u5230\u8fbe\u671f\u9650\u7684\u5b9a\u65f6\u5668\n         *\/\n        void loopForExecute();\n\n        \/\/\u79c1\u6709\u7684\u6784\u9020\u51fd\u6570\n        Timer(std::chrono::milliseconds tick):eventQueue(\n            [](SchedulerEvent&amp; a,SchedulerEvent&amp; b){\n                return a.deadline &lt; b.deadline;\n            }\n        ){\n            this-&gt;timeline = 0;\n            this-&gt;tick = tick;\n            this-&gt;isStart = false;\n        }\n\n    public:\n\n        \/\/\u5355\u4f8b\u6a21\u5f0f\n        static Timer* getInstance(std::chrono::milliseconds tick){\n            static Timer timer(tick);\n            return &amp;timer;\n        }\n\n        \/**\n         * \u8bbe\u7f6e\u5b9a\u65f6\u5668\n         * @param interval \u5b9a\u65f6\u95f4\u9694\n         * @param action \u5b9a\u65f6\u6267\u884c\u7684\u52a8\u4f5c\n         * @param isRepeat \u662f\u5426\u91cd\u590d\u6267\u884c,\u9ed8\u8ba4\u4e0d\u91cd\u590d\u6267\u884c\n         * @return unsigned int \u5b9a\u65f6\u5668\u7684id,\u53ef\u4ee5\u6839\u636e\u8fd9\u4e2aid\u6267\u884c\u5220\u9664\u64cd\u4f5c\n         *\/\n        unsigned int addEvent(double interval,std::function&lt;void()&gt; action,bool isRepeat = false);\n\n        \/**\n         * \u5220\u9664\u5b9a\u65f6\u5668\n         * @param timerId \u5b9a\u65f6\u5668id\n         *\n         *\/\n        void deleteEvent(unsigned int timerId);\n\n        \/**\n         * \u540c\u6b65\u6267\u884c\u542f\u52a8\u5b9a\u65f6\u5668\n         *\/\n         void syncStart();\n\n         \/**\n         * \u5f02\u6b65\u6267\u884c\u542f\u52a8\u5b9a\u65f6\u5668\n         *\/\n         void asyncStart();\n\n};\n\n\n#endif\n<\/code><\/pre>\n<p>Timer.cpp<\/p>\n<pre class=\"line-numbers prism-highlight\" data-start=\"1\"><code class=\"language-null\">#include \"src\/core\/util\/Timer.h\"\n\nunsigned int Timer::addEvent(double interval,std::function&lt;void()&gt; action,bool isRepeat){\n    SchedulerEvent event(interval,this-&gt;timeline,action,isRepeat);\n    return this-&gt;eventQueue.insertNode(event);\n}\n\nvoid Timer::deleteEvent(unsigned int timerId){\n    this-&gt;eventQueue.deleteNode(timerId);\n}\n\nvoid Timer::loopForExecute(){\n    std::unique_ptr&lt;SchedulerEvent&gt; top = this-&gt;eventQueue.getTopNode();\n    while(top != nullptr &amp;&amp; top-&gt;deadline &lt;= this-&gt;timeline){\n        \/\/\u5982\u679c\u5df2\u7ecf\u5230\u4e86\u6267\u884c\u7684\u65f6\u95f4,\u65b0\u5f00\u4e00\u4e2a\u5b50\u7ebf\u7a0b\u6267\u884c\u4efb\u52a1\n        std::thread t(top-&gt;action);\n        t.detach();    \/\/\u5b50\u7ebf\u7a0b\u5206\u79bb\n\n        if(top-&gt;isRepeat){\n            \/\/\u5982\u679c\u662f\u91cd\u590d\u4e8b\u4ef6,\u5219\u91cd\u65b0\u6dfb\u52a0\n            this-&gt;addEvent(top-&gt;interval,top-&gt;action,top-&gt;isRepeat);\n        }\n\n        \/\/\u4ece\u5806\u4e2d\u5220\u9664\n        this-&gt;eventQueue.deleteTopNode();\n        top = this-&gt;eventQueue.getTopNode();\n    }\n    \/\/\u6267\u884c\u4e00\u6b21\u540e\u7b49\u5f85\u4e00\u4e2a\u5468\u671f\n    std::this_thread::sleep_for(this-&gt;tick);\n    \/\/\u5468\u671f\u589e1\n    this-&gt;timeline++;\n}\n\nvoid Timer::asyncStart(){\n    if(!this-&gt;isStart){\n        std::thread daemon_thread(&amp;Timer::syncStart,this);\n        daemon_thread.detach();     \/\/\u4ece\u5f53\u524d\u4e3b\u7ebf\u7a0b\u5206\u79bb\n    }\n}\n\nvoid Timer::syncStart(){\n    if(!this-&gt;isStart){\n        while(1)\n            this-&gt;loopForExecute();\n    }\n}\n<\/code><\/pre>\n<p>\u6d4b\u8bd5\u6267\u884c\u7684\u4ee3\u7801<\/p>\n<pre class=\"line-numbers prism-highlight\" data-start=\"1\"><code class=\"language-null\">#include &lt;iostream&gt;\n#include &lt;chrono&gt;\n#include &lt;ctime&gt;\n#include &lt;iomanip&gt;\n#include &lt;string&gt;\n#include &lt;functional&gt;\n#include &lt;thread&gt;\n#include &lt;memory&gt;\n#include &lt;fstream&gt;\n#include \"src\/core\/util\/Timer.h\"\n\nvoid myprint(std::string msg){\n    std::ofstream of(\"timer.txt\", std::ios::app);\n    std::thread::id this_id = std::this_thread::get_id();\n    auto t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());\n    of &lt;&lt; \"From Thread \" &lt;&lt; this_id &lt;&lt; \"at time \" &lt;&lt; std::put_time(std::localtime(&amp;t), \"%Y-%m-%d %H.%M.%S\") &lt;&lt; \":\" &lt;&lt; msg &lt;&lt; std::endl;\n}\n\nint main(){\n    std::chrono::milliseconds tick(2000);       \/\/1000\u6beb\u79d2\u4f5c\u4e3a\u4e00\u4e2a\u5468\u671f\n    Timer* timer = Timer::getInstance(tick);\n    std::function&lt;void()&gt; f1 = std::bind(myprint,\"\u7b2c\u4e00\u4e2a\u52a0\u5165,10tick\u540e\u6267\u884c\");\n    std::function&lt;void()&gt; f2 = std::bind(myprint,\"\u7b2c\u4e8c\u4e2a\u52a0\u5165\uff0c\u88ab\u5220\u9664\u4e0d\u6267\u884c\");\n    std::function&lt;void()&gt; f3 = std::bind(myprint,\"\u7b2c\u4e09\u4e2a\u52a0\u5165\uff0c\u6bcf5tick\u91cd\u590d\u6267\u884c\");\n    std::function&lt;void()&gt; f4 = std::bind(myprint,\"\u7b2c\u56db\u4e2a\u52a0\u5165\uff0c5tick\u540e\u6267\u884c\");\n    std::function&lt;void()&gt; f5 = std::bind(myprint,\"\u7b2c\u4e94\u4e2a\u52a0\u5165\uff0c5tick\u540e\u6267\u884c\");\n    std::function&lt;void()&gt; f6 = std::bind(myprint,\"\u7b2c\u516d\u4e2a\u52a0\u5165\uff0c5tick\u540e\u6267\u884c\");\n    std::function&lt;void()&gt; f7 = std::bind(myprint,\"\u7b2c\u4e03\u4e2a\u52a0\u5165\uff0c5tick\u540e\u6267\u884c\");\n    std::function&lt;void()&gt; f8 = std::bind(myprint,\"\u7b2c\u516b\u4e2a\u52a0\u5165\uff0c5tick\u540e\u6267\u884c\");\n    std::function&lt;void()&gt; f9 = std::bind(myprint,\"\u7b2c\u4e5d\u4e2a\u52a0\u5165\uff0c5tick\u540e\u6267\u884c\");\n    std::function&lt;void()&gt; f10 = std::bind(myprint,\"\u7b2c\u5341\u4e2a\u52a0\u5165\uff0c5tick\u540e\u6267\u884c\");\n    std::function&lt;void()&gt; f11 = std::bind(myprint,\"\u7b2c\u5341\u4e00\u4e2a\u52a0\u5165\uff0c15tick\u540e\u6267\u884c\");\n    std::function&lt;void()&gt; f12 = std::bind(myprint,\"\u7b2c\u5341\u4e8c\u4e2a\u5728\u6267\u884c\u540e\u52a0\u5165\uff0c20tick+5s\u540e\u6267\u884c\");\n\n    timer-&gt;addEvent(10,f1);\n    int id = timer-&gt;addEvent(11,f2);\n    timer-&gt;addEvent(5,f3,true);\n    timer-&gt;addEvent(5,f4);\n    timer-&gt;addEvent(5,f5);\n    timer-&gt;addEvent(5,f6);   \n    timer-&gt;addEvent(5,f7);\n    timer-&gt;addEvent(5,f8);\n    timer-&gt;addEvent(5,f9);\n    timer-&gt;addEvent(5,f10);\n    timer-&gt;addEvent(15,f11);\n\n    timer-&gt;deleteEvent(id);\n\n    myprint(\"\u7ebf\u7a0b\u5f00\u59cb\u542f\u52a8,\u6bcftick\u662f2\u79d2\");\n\n    \/\/\u5f02\u6b65\u6267\u884c\uff0c\u7a0b\u5e8f\u9000\u51fa\u540e\u8ba1\u65f6\u5668\u4e5f\u4f1a\u7ec8\u6b62\uff0c\u56e0\u6b64\u5728\u4e0b\u9762\u4f7f\u7528while\u5faa\u73af\u4fdd\u8bc1\u7a0b\u5e8f\u4e0d\u4f1a\u9000\u51fa\n    timer-&gt;asyncStart();\n    \/\/timer-&gt;syncStart();\n\n\n    \/\/\u4f11\u77205\u79d2\u949f\n    std::this_thread::sleep_for(std::chrono::seconds(5));   \n    \/\/\u5e94\u8be5\u5728\u5927\u698220*tick+5\u79d2\u540e\u6267\u884c,\n    \/\/TODO \u6267\u884c\u540e\u52a0\u5165\u7684\u5b9a\u65f6\u5668\u4e0d\u5bf9\n    timer-&gt;addEvent(20,f12);\n\n    getchar();\n\n    return 0;\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u5f15\u8a00 \u5728\u4e00\u4e2a\u6bd4\u8f83\u5927\u7684\u7cfb\u7edf\u5f53\u4e2d\uff0c\u5f88\u591a\u5730\u65b9\u90fd\u9700\u8981\u5b9a\u65f6\u529f\u80fd\uff0c\u6bd4\u5982\u67d0\u4e9b\u5b58\u5728\u5185\u5b58\u4e2d\u7684\u6570\u636e\uff0c\u9700\u8981\u5b9a\u65f6\u7684\u505a\u6301\u4e45\u5316\uff08\u5b9e\u65f6\u6301 &hellip; <a href=\"https:\/\/www.myway5.com\/index.php\/2017\/05\/19\/cpp11-implement-cross-platform-timer\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">c++11\u5b9e\u73b0\u7684\u8de8\u5e73\u53f0\u5b9a\u65f6\u5668<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[13,14,15],"class_list":["post-112","post","type-post","status-publish","format-standard","hentry","category-cplusplus","tag-c11","tag-14","tag-15"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>c++11\u5b9e\u73b0\u7684\u8de8\u5e73\u53f0\u5b9a\u65f6\u5668 - \u4e00\u53ea\u5b89\u9759\u7684\u732b<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.myway5.com\/index.php\/2017\/05\/19\/cpp11-implement-cross-platform-timer\/\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"c++11\u5b9e\u73b0\u7684\u8de8\u5e73\u53f0\u5b9a\u65f6\u5668 - \u4e00\u53ea\u5b89\u9759\u7684\u732b\" \/>\n<meta property=\"og:description\" content=\"\u4e00\u3001\u5f15\u8a00 \u5728\u4e00\u4e2a\u6bd4\u8f83\u5927\u7684\u7cfb\u7edf\u5f53\u4e2d\uff0c\u5f88\u591a\u5730\u65b9\u90fd\u9700\u8981\u5b9a\u65f6\u529f\u80fd\uff0c\u6bd4\u5982\u67d0\u4e9b\u5b58\u5728\u5185\u5b58\u4e2d\u7684\u6570\u636e\uff0c\u9700\u8981\u5b9a\u65f6\u7684\u505a\u6301\u4e45\u5316\uff08\u5b9e\u65f6\u6301 &hellip; \u7ee7\u7eed\u9605\u8bfbc++11\u5b9e\u73b0\u7684\u8de8\u5e73\u53f0\u5b9a\u65f6\u5668\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.myway5.com\/index.php\/2017\/05\/19\/cpp11-implement-cross-platform-timer\/\" \/>\n<meta property=\"og:site_name\" content=\"\u4e00\u53ea\u5b89\u9759\u7684\u732b\" \/>\n<meta property=\"article:published_time\" content=\"2017-05-19T06:14:10+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-08-21T01:40:33+00:00\" \/>\n<meta name=\"author\" content=\"jiangpengfei\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"jiangpengfei\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.myway5.com\/index.php\/2017\/05\/19\/cpp11-implement-cross-platform-timer\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.myway5.com\/index.php\/2017\/05\/19\/cpp11-implement-cross-platform-timer\/\"},\"author\":{\"name\":\"jiangpengfei\",\"@id\":\"https:\/\/www.myway5.com\/#\/schema\/person\/b19267e8b106343431e163ec96950685\"},\"headline\":\"c++11\u5b9e\u73b0\u7684\u8de8\u5e73\u53f0\u5b9a\u65f6\u5668\",\"datePublished\":\"2017-05-19T06:14:10+00:00\",\"dateModified\":\"2017-08-21T01:40:33+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.myway5.com\/index.php\/2017\/05\/19\/cpp11-implement-cross-platform-timer\/\"},\"wordCount\":39,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.myway5.com\/#\/schema\/person\/b19267e8b106343431e163ec96950685\"},\"keywords\":[\"c++11\",\"\u591a\u7ebf\u7a0b\",\"\u5b9a\u65f6\u5668\"],\"articleSection\":[\"c++\"],\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.myway5.com\/index.php\/2017\/05\/19\/cpp11-implement-cross-platform-timer\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.myway5.com\/index.php\/2017\/05\/19\/cpp11-implement-cross-platform-timer\/\",\"url\":\"https:\/\/www.myway5.com\/index.php\/2017\/05\/19\/cpp11-implement-cross-platform-timer\/\",\"name\":\"c++11\u5b9e\u73b0\u7684\u8de8\u5e73\u53f0\u5b9a\u65f6\u5668 - \u4e00\u53ea\u5b89\u9759\u7684\u732b\",\"isPartOf\":{\"@id\":\"https:\/\/www.myway5.com\/#website\"},\"datePublished\":\"2017-05-19T06:14:10+00:00\",\"dateModified\":\"2017-08-21T01:40:33+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.myway5.com\/index.php\/2017\/05\/19\/cpp11-implement-cross-platform-timer\/#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.myway5.com\/index.php\/2017\/05\/19\/cpp11-implement-cross-platform-timer\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.myway5.com\/index.php\/2017\/05\/19\/cpp11-implement-cross-platform-timer\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.myway5.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"c++11\u5b9e\u73b0\u7684\u8de8\u5e73\u53f0\u5b9a\u65f6\u5668\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.myway5.com\/#website\",\"url\":\"https:\/\/www.myway5.com\/\",\"name\":\"\u4e00\u53ea\u5b89\u9759\u7684\u732b\",\"description\":\"\u60f3\u5565\u5462\",\"publisher\":{\"@id\":\"https:\/\/www.myway5.com\/#\/schema\/person\/b19267e8b106343431e163ec96950685\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.myway5.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"zh-Hans\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/www.myway5.com\/#\/schema\/person\/b19267e8b106343431e163ec96950685\",\"name\":\"jiangpengfei\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.myway5.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/f8c7de757f6e0247412bcfd31b7c2271?s=96&d=monsterid&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/f8c7de757f6e0247412bcfd31b7c2271?s=96&d=monsterid&r=g\",\"caption\":\"jiangpengfei\"},\"logo\":{\"@id\":\"https:\/\/www.myway5.com\/#\/schema\/person\/image\/\"},\"url\":\"https:\/\/www.myway5.com\/index.php\/author\/joyme\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"c++11\u5b9e\u73b0\u7684\u8de8\u5e73\u53f0\u5b9a\u65f6\u5668 - \u4e00\u53ea\u5b89\u9759\u7684\u732b","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.myway5.com\/index.php\/2017\/05\/19\/cpp11-implement-cross-platform-timer\/","og_locale":"zh_CN","og_type":"article","og_title":"c++11\u5b9e\u73b0\u7684\u8de8\u5e73\u53f0\u5b9a\u65f6\u5668 - \u4e00\u53ea\u5b89\u9759\u7684\u732b","og_description":"\u4e00\u3001\u5f15\u8a00 \u5728\u4e00\u4e2a\u6bd4\u8f83\u5927\u7684\u7cfb\u7edf\u5f53\u4e2d\uff0c\u5f88\u591a\u5730\u65b9\u90fd\u9700\u8981\u5b9a\u65f6\u529f\u80fd\uff0c\u6bd4\u5982\u67d0\u4e9b\u5b58\u5728\u5185\u5b58\u4e2d\u7684\u6570\u636e\uff0c\u9700\u8981\u5b9a\u65f6\u7684\u505a\u6301\u4e45\u5316\uff08\u5b9e\u65f6\u6301 &hellip; \u7ee7\u7eed\u9605\u8bfbc++11\u5b9e\u73b0\u7684\u8de8\u5e73\u53f0\u5b9a\u65f6\u5668","og_url":"https:\/\/www.myway5.com\/index.php\/2017\/05\/19\/cpp11-implement-cross-platform-timer\/","og_site_name":"\u4e00\u53ea\u5b89\u9759\u7684\u732b","article_published_time":"2017-05-19T06:14:10+00:00","article_modified_time":"2017-08-21T01:40:33+00:00","author":"jiangpengfei","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"jiangpengfei","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"8 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.myway5.com\/index.php\/2017\/05\/19\/cpp11-implement-cross-platform-timer\/#article","isPartOf":{"@id":"https:\/\/www.myway5.com\/index.php\/2017\/05\/19\/cpp11-implement-cross-platform-timer\/"},"author":{"name":"jiangpengfei","@id":"https:\/\/www.myway5.com\/#\/schema\/person\/b19267e8b106343431e163ec96950685"},"headline":"c++11\u5b9e\u73b0\u7684\u8de8\u5e73\u53f0\u5b9a\u65f6\u5668","datePublished":"2017-05-19T06:14:10+00:00","dateModified":"2017-08-21T01:40:33+00:00","mainEntityOfPage":{"@id":"https:\/\/www.myway5.com\/index.php\/2017\/05\/19\/cpp11-implement-cross-platform-timer\/"},"wordCount":39,"commentCount":0,"publisher":{"@id":"https:\/\/www.myway5.com\/#\/schema\/person\/b19267e8b106343431e163ec96950685"},"keywords":["c++11","\u591a\u7ebf\u7a0b","\u5b9a\u65f6\u5668"],"articleSection":["c++"],"inLanguage":"zh-Hans","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.myway5.com\/index.php\/2017\/05\/19\/cpp11-implement-cross-platform-timer\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.myway5.com\/index.php\/2017\/05\/19\/cpp11-implement-cross-platform-timer\/","url":"https:\/\/www.myway5.com\/index.php\/2017\/05\/19\/cpp11-implement-cross-platform-timer\/","name":"c++11\u5b9e\u73b0\u7684\u8de8\u5e73\u53f0\u5b9a\u65f6\u5668 - \u4e00\u53ea\u5b89\u9759\u7684\u732b","isPartOf":{"@id":"https:\/\/www.myway5.com\/#website"},"datePublished":"2017-05-19T06:14:10+00:00","dateModified":"2017-08-21T01:40:33+00:00","breadcrumb":{"@id":"https:\/\/www.myway5.com\/index.php\/2017\/05\/19\/cpp11-implement-cross-platform-timer\/#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.myway5.com\/index.php\/2017\/05\/19\/cpp11-implement-cross-platform-timer\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.myway5.com\/index.php\/2017\/05\/19\/cpp11-implement-cross-platform-timer\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.myway5.com\/"},{"@type":"ListItem","position":2,"name":"c++11\u5b9e\u73b0\u7684\u8de8\u5e73\u53f0\u5b9a\u65f6\u5668"}]},{"@type":"WebSite","@id":"https:\/\/www.myway5.com\/#website","url":"https:\/\/www.myway5.com\/","name":"\u4e00\u53ea\u5b89\u9759\u7684\u732b","description":"\u60f3\u5565\u5462","publisher":{"@id":"https:\/\/www.myway5.com\/#\/schema\/person\/b19267e8b106343431e163ec96950685"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.myway5.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"zh-Hans"},{"@type":["Person","Organization"],"@id":"https:\/\/www.myway5.com\/#\/schema\/person\/b19267e8b106343431e163ec96950685","name":"jiangpengfei","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.myway5.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/f8c7de757f6e0247412bcfd31b7c2271?s=96&d=monsterid&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f8c7de757f6e0247412bcfd31b7c2271?s=96&d=monsterid&r=g","caption":"jiangpengfei"},"logo":{"@id":"https:\/\/www.myway5.com\/#\/schema\/person\/image\/"},"url":"https:\/\/www.myway5.com\/index.php\/author\/joyme\/"}]}},"views":8690,"_links":{"self":[{"href":"https:\/\/www.myway5.com\/index.php\/wp-json\/wp\/v2\/posts\/112","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.myway5.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.myway5.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.myway5.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.myway5.com\/index.php\/wp-json\/wp\/v2\/comments?post=112"}],"version-history":[{"count":9,"href":"https:\/\/www.myway5.com\/index.php\/wp-json\/wp\/v2\/posts\/112\/revisions"}],"predecessor-version":[{"id":194,"href":"https:\/\/www.myway5.com\/index.php\/wp-json\/wp\/v2\/posts\/112\/revisions\/194"}],"wp:attachment":[{"href":"https:\/\/www.myway5.com\/index.php\/wp-json\/wp\/v2\/media?parent=112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.myway5.com\/index.php\/wp-json\/wp\/v2\/categories?post=112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.myway5.com\/index.php\/wp-json\/wp\/v2\/tags?post=112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}