{"id":243,"date":"2023-09-08T20:40:09","date_gmt":"2023-09-08T12:40:09","guid":{"rendered":"https:\/\/www.heene.tech\/?p=243"},"modified":"2023-09-08T20:40:09","modified_gmt":"2023-09-08T12:40:09","slug":"%e5%b8%a6%e6%b6%88%e6%81%af%e9%98%9f%e5%88%97%e7%9a%84%e7%ba%bf%e7%a8%8b%e6%b1%a0%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/blog.heene.tech\/index.php\/2023\/09\/08\/%e5%b8%a6%e6%b6%88%e6%81%af%e9%98%9f%e5%88%97%e7%9a%84%e7%ba%bf%e7%a8%8b%e6%b1%a0%e5%ae%9e%e7%8e%b0\/","title":{"rendered":"\u5e26\u6d88\u606f\u961f\u5217\u7684\u7ebf\u7a0b\u6c60\u5b9e\u73b0"},"content":{"rendered":"\n<div class=\"wp-block-argon-alert alert\" style=\"background-color:#3c4d69\"><span class=\"alert-inner--icon\"><i class=\"fa fa-info-circle\"><\/i><\/span><span class=\"alert-inner--text\">\u5f53\u524d\uff0c\u5df2\u6709\u591a\u79cd\u57fa\u4e8eC++\u7684\u7ebf\u7a0b\u6c60\u5b9e\u73b0\u65b9\u6848\uff0c\u672c\u6587\u5728\u5b66\u4e60\u4f18\u79c0\u7684\u5f00\u6e90\u9879\u76ee\u7684\u57fa\u7840\u4e0a\u505a\u51fa\u4e00\u4e2a\u5e26\u6709\u6d88\u606f\u961f\u5217\u7684\u7ebf\u7a0b\u6c60\u5b9e\u73b0\u3002\u8be5\u9879\u76ee\u57fa\u4e8eC++14\uff0c\u5b9e\u73b0\u4e86\u5e26\u6709\u7ebf\u7a0b\u5b89\u5168\u6027\u7684\u6d88\u606f\u961f\u5217\u7c7b\u3001\u7ebf\u7a0b\u6c60\u7c7b\u3002\u53c2\u8003\u7684\u5f00\u6e90\u9879\u76ee\u5982\u4e0b\uff1a<\/span><\/div>\n\n\n\n<div class=\"wp-block-argon-github github-info-card card shadow-sm github-info-card-mini is-style-github-info-card-mini\" data-author=\"progschj\" data-project=\"ThreadPool\"><div class=\"github-info-card-header\"><a href=\"https:\/\/github.com\/\" target=\"_blank\" title=\"Github\" rel=\"noopener\"><span><i class=\"fa fa-github\"><\/i>\u00a0GitHub<\/span><\/a><\/div><div class=\"github-info-card-body\"><div class=\"github-info-card-name-a\"><a href=\"https:\/\/github.com\/progschj\/ThreadPool\" target=\"_blank\" rel=\"noopener\"><span class=\"github-info-card-name\">progschj\/ThreadPool<\/span><\/a><\/div><div class=\"github-info-card-description\"><\/div><\/div><div class=\"github-info-card-bottom\"><span class=\"github-info-card-meta github-info-card-meta-stars\"><i class=\"fa fa-star\"><\/i>\u00a0<span class=\"github-info-card-stars\"><\/span><\/span><span class=\"github-info-card-meta github-info-card-meta-forks\"><i class=\"fa fa-code-fork\"><\/i>\u00a0<span class=\"github-info-card-forks\"><\/span><\/span><\/div><\/div>\n\n\n\n<div class=\"wp-block-argon-github github-info-card card shadow-sm github-info-card-mini is-style-github-info-card-mini\" data-author=\"bshoshany\" data-project=\"thread-pool\"><div class=\"github-info-card-header\"><a href=\"https:\/\/github.com\/\" target=\"_blank\" title=\"Github\" rel=\"noopener\"><span><i class=\"fa fa-github\"><\/i>\u00a0GitHub<\/span><\/a><\/div><div class=\"github-info-card-body\"><div class=\"github-info-card-name-a\"><a href=\"https:\/\/github.com\/bshoshany\/thread-pool\" target=\"_blank\" rel=\"noopener\"><span class=\"github-info-card-name\">bshoshany\/thread-pool<\/span><\/a><\/div><div class=\"github-info-card-description\"><\/div><\/div><div class=\"github-info-card-bottom\"><span class=\"github-info-card-meta github-info-card-meta-stars\"><i class=\"fa fa-star\"><\/i>\u00a0<span class=\"github-info-card-stars\"><\/span><\/span><span class=\"github-info-card-meta github-info-card-meta-forks\"><i class=\"fa fa-code-fork\"><\/i>\u00a0<span class=\"github-info-card-forks\"><\/span><\/span><\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">\u7c7b\u89c6\u56fe\u8bbe\u8ba1<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u62df\u5b9e\u73b0\u7684\u529f\u80fd<\/h3>\n\n\n\n<div style=\"margin-bottom: 20px;\" class=\"wp-block-argon-todolist\"><div class=\"shortcode-todo custom-control custom-checkbox\"><input class=\"custom-control-input\" type=\"checkbox\" checked\/><label class=\"custom-control-label\"><span>\u5b9e\u73b0\u62e5\u6709\u5404\u79cd\u666e\u901a\u51fd\u6570\u3001\u533f\u540d\u51fd\u6570\u3001\u4eff\u51fd\u6570\u3001\u7c7b\u6210\u5458\u51fd\u6570\uff0cfunction\/packged_task\u5bf9\u8c61\u53c2\u6570\u5217\u8868\uff0c\u5404\u79cd\u8fd4\u56de\u503c\u7684\u652f\u6301\u3002<\/span><\/label><\/div><div class=\"shortcode-todo custom-control custom-checkbox\"><input class=\"custom-control-input\" type=\"checkbox\" checked\/><label class=\"custom-control-label\"><span>\u7ebf\u7a0b\u6c60\u4e2d\u7684\u7ebf\u7a0b\u5e94\u80fd\u4e92\u76f8\u901a\u4fe1\uff0c\u6216\u8005\u7559\u6709\u901a\u4fe1\u7684\u63a5\u53e3<\/span><\/label><\/div><div class=\"shortcode-todo custom-control custom-checkbox\"><input class=\"custom-control-input\" type=\"checkbox\" checked\/><label class=\"custom-control-label\"><span>\u5e94\u80fd\u4ece\u5916\u90e8\u901a\u77e5\u7ebf\u7a0b\u542f\u505c<\/span><\/label><\/div><div class=\"shortcode-todo custom-control custom-checkbox\"><input class=\"custom-control-input\" type=\"checkbox\" checked\/><label class=\"custom-control-label\"><span>\u5e94\u80fd\u6392\u961f\u5904\u7406\u4efb\u52a1<\/span><\/label><\/div><div class=\"shortcode-todo custom-control custom-checkbox\"><input class=\"custom-control-input\" type=\"checkbox\"\/><label class=\"custom-control-label\"><span>\u5e94\u63d0\u4f9b\u65e5\u5fd7\u8bb0\u5f55\u63a5\u53e3<\/span><\/label><\/div><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">\u7c7b\u8bbe\u8ba1<\/h3>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='http:\/\/blog.heene.tech\/wp-content\/uploads\/2023\/09\/\u5e26\u6d88\u606f\u961f\u5217\u7684\u7ebf\u7a0b\u6c60\u5b9e\u73b0-2-1024x608.png'><img class=\"lazyload lazyload-style-8\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"608\" data-original=\"http:\/\/blog.heene.tech\/wp-content\/uploads\/2023\/09\/\u5e26\u6d88\u606f\u961f\u5217\u7684\u7ebf\u7a0b\u6c60\u5b9e\u73b0-2-1024x608.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-266\" title=\"\u7c7b\u5173\u7cfb\u56fe\"  sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/div><\/figure>\n\n\n\n<p>\u5982\u4e0a\u56fe\u6240\u793a\uff0c\u8be5\u5b9e\u73b0\u5305\u542b\u56db\u4e2a\u7c7b\uff0c\u5206\u522b\u662f\u5b9e\u73b0\u7ebf\u7a0b\u5b89\u5168\u7684\u961f\u5217\u62bd\u7ebf\u57fa\u7c7b\uff0c\u7ee7\u627f\u81ea\u62bd\u8c61\u57fa\u7c7b\u7684\u5b89\u5168\u961f\u5217\u6a21\u677f\u7c7b\uff0c\u7ebf\u7a0b\u6c60\u7c7b\uff0c\u4ee5\u53ca\u7ebf\u7a0b\u4efb\u52a1\u7c7b\u3002\u5728\u5b9e\u9645\u8fd0\u884c\u8fc7\u7a0b\u4e2d\uff0c\u7528\u6237\u9996\u5148\u521b\u5efa\u7ebf\u7a0b\u6c60\u7c7b\uff0c\u5e76\u6307\u5b9a\u4f7f\u7528\u7684\u6700\u5927\u7ebf\u7a0b\u6570\uff08\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u4f7f\u7528\u8bbe\u5907\u652f\u6301\u7684\u6700\u5927\u7ebf\u7a0b\u6570\uff09\u3002\u4e4b\u540e\u521b\u5efa\u4efb\u52a1\u7c7b\uff0c\u5728\u5b9e\u9645\u5de5\u4f5c\u4e2d\uff0c\u5e0c\u671b\u80fd\u901a\u8fc7\u4efb\u52a1\u7c7b\u6765\u521b\u5efa\u548c\u64cd\u4f5c\u6d88\u606f\u961f\u5217\uff0c\u800c\u6d88\u606f\u961f\u5217\u53c8\u7531\u7ebf\u7a0b\u6c60\u7ba1\u7406\uff0c\u56e0\u6b64\u9700\u8981\u5728\u521b\u5efa\u4efb\u52a1\u7c7b\u7684\u65f6\u5019\u5c06\u7ebf\u7a0b\u6c60\u6307\u9488\u4f20\u5165\u3002\u6700\u540e\uff0c\u5c06\u4efb\u52a1\u7c7b\u5b9e\u4f8b\u5316\uff0c\u5e76\u901a\u8fc7\u7ebf\u7a0b\u6c60\u7684addTask\u51fd\u6570\u5c06\u4efb\u52a1\u52a0\u5165\u5230\u7ebf\u7a0b\u6c60\u4e2d\u6267\u884c\u3002\u6b64\u5916\uff0c\u7531\u4e8eaddTask\u4f7f\u7528\u4e86\u53ef\u53d8\u53c2\u6570\u6a21\u677f\u548c\u7c7b\u578b\u8403\u53d6\u6280\u672f\uff0c\u53ef\u4ee5\u6dfb\u52a0\u57fa\u4e8e\u51fd\u6570\u5f62\u5f0f\u3001lambda\u8868\u8fbe\u5f0f\u6216\u8005\u4eff\u51fd\u6570\u5f62\u5f0f\u4efb\u52a1\u3002\u4e8b\u5b9e\u4e0a\uff0c\u4f20\u5165\u7c7b\u5bf9\u8c61\u5c31\u662f\u57fa\u4e8e\u4eff\u51fd\u6570\u5f62\u5f0f\u7684\u4efb\u52a1\uff0c\u6b64\u65f6\uff0c\u9700\u8981\u5c06\u4efb\u52a1\u6267\u884c\u8fc7\u7a0b\u653e\u5728\u9488\u5bf9\u62ec\u53f7\u8fd0\u7b97\u7b26\u7684\u91cd\u8f7d\u51fd\u6570\u4e2d\u3002<\/p>\n\n\n\n<p>\u8fd9\u6837\u7684\u8bbe\u8ba1\uff0c\u53ef\u4ee5\u4f7f\u7c7b\u56fe\u4e0a\u65b9\u4e24\u4e2a\u7c7b\uff08SafeQueueBase\u548cThreadPool\uff09\u4fdd\u6301\u7a33\u5b9a\uff0c\u800c\u4e0b\u65b9\u4e24\u4e2a\u7c7b\uff08SafeQueue\u548cTheadTask\uff09\u53ef\u4ee5\u53d8\u5316\uff0c\u9694\u7edd\u4e86\u7a33\u5b9a\u90e8\u5206\u548c\u53d8\u6362\u90e8\u5206\uff0c\u9075\u5faa\u4e86OOP\u7684\u5f00\u653e\u5c01\u95ed\u539f\u5219\u3001\u4f9d\u8d56\u5012\u7f6e\u539f\u5219\u3001\u5355\u4e00\u804c\u8d23\u539f\u5219\uff0c\u63d0\u5347\u4e86\u7a0b\u5e8f\u7684\u6269\u5c55\u80fd\u529b\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7ebf\u7a0b\u5b89\u5168\u961f\u5217\u5b9e\u73b0<\/h2>\n\n\n\n<p>\u5728C++17\u6807\u51c6\u4e4b\u540e\uff0c\u624d\u6b63\u5f0f\u5f15\u5165\u652f\u6301\u5e76\u53d1\u7684\u5bb9\u5668\uff0c\u7531\u4e8e\u76ee\u524d\u5de5\u4e1a\u4e0a\u5e38\u7528\u7684C++\u7248\u672c\u591a\u4e3aC++11\u4e0eC++14\uff0c\u56e0\u6b64\u9700\u8981\u624b\u52a8\u5b9e\u73b0\u9488\u5bf9\u961f\u5217\u7684\u7ebf\u7a0b\u5b89\u5168\u63a7\u5236\uff0c\u8fd9\u91cc\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u6613\u7684\u5b9e\u73b0\u65b9\u6cd5\uff0c\u4e3b\u8981\u662f\u901a\u8fc7\u4f7f\u7528\u5171\u4eab\u9501\u5b9e\u73b0\u7684\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>\nstruct TypeErasedData {\n    virtual ~TypeErasedData() {}\n};\n\ntemplate &lt;typename T&gt;\nstruct ErasedData : public TypeErasedData {\n    ErasedData(T&amp; value) : data(value) {}\n    T&amp; data;\n};\n\nclass SafeQueueBase\n{\n   public:\n    SafeQueueBase() = default;\n    SafeQueueBase(SafeQueueBase&amp;&amp;) = delete;\n    SafeQueueBase(const SafeQueueBase&amp;) = delete;\n    SafeQueueBase&amp; operator=(SafeQueueBase&amp;&amp;) = delete;\n    SafeQueueBase&amp; operator=(const SafeQueueBase&amp;) = delete;\n\n    bool empty()\n    {\n        std::shared_lock&lt;std::shared_timed_mutex&gt; lck(mux);\n        return primaryEmpty();\n    }\n    size_t size()\n    {\n        std::shared_lock&lt;std::shared_timed_mutex&gt; lck(mux);\n        return primarySize();\n    }\n\n    template &lt;typename T&gt;\n    void pop(T&amp; _data_output)\n    {\n        std::unique_lock&lt;std::shared_timed_mutex&gt; lck(mux);\n        ErasedData&lt;T&gt; data_output(_data_output);\n        primaryPop(&amp;data_output);\n    }\n\n    template &lt;typename T&gt;\n    void push(const T&amp; _data)\n    {\n        std::unique_lock&lt;std::shared_timed_mutex&gt; lck(mux);\n        ErasedData&lt;const T&gt; data(_data);\n        primaryPush(&amp;data);\n    }\n    template &lt;typename T&gt;\n    void see(std::vector&lt;T&gt;&amp; _data_output)\n    {\n        std::shared_lock&lt;std::shared_timed_mutex&gt; lck(mux);\n        ErasedData&lt;std::vector&lt;T&gt;&gt; data_output(_data_output);\n        primarySee(&amp;data_output);\n    }\n\n    virtual ~SafeQueueBase(){};\n\n   protected:\n    virtual void primaryPush(TypeErasedData* _data) = 0;\n    virtual void primaryPop(TypeErasedData* _data) = 0;\n    virtual void primarySee(TypeErasedData* _data) = 0;\n    virtual bool primaryEmpty() = 0;\n    virtual size_t primarySize() = 0;\n\n   private:\n    std::shared_timed_mutex mux;  \/\/ WARN: need to support c++14\n};\n\n<\/code><\/pre>\n\n\n\n<p>\u8be5\u7c7b\u4f5c\u4e3a\u62bd\u8c61\u57fa\u7c7b\uff0c\u63d0\u4f9b\u4e865\u79cd\u64cd\u4f5c\u5bb9\u5668\u7684\u65b9\u6cd5\uff0c\u5728\u5bf9\u5bb9\u5668\u8fdb\u884c\u8bbf\u95ee\u7684\u65b9\u6cd5\u79cd\uff0c\u4f7f\u7528\u53ef\u79fb\u52a8\u5171\u4eab\u4e92\u65a5\u4f53\u6240\u6709\u6743\u5c01\u88c5\u5668\u4fdd\u8bc1\u591a\u7ebf\u7a0b\u8bbf\u95ee\u7684\u6548\u7387\uff1b\u5728\u5bf9\u5bb9\u5668\u8fdb\u884c\u4fee\u6539\u7684\u65b9\u6cd5\u79cd\uff0c\u4f7f\u7528\u53ef\u79fb\u52a8\u4e92\u65a5\u4f53\u6240\u6709\u6743\u5305\u88c5\u5668\u4fdd\u8bc1\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u7684\u5b89\u5168\u6027\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u4e24\u4e2a\u7c7b\uff08TypeErasedData \u548cErasedData\uff09\u5b9e\u73b0\u7c7b\u578b\u64e6\u9664\uff0c\u4fdd\u8bc1\u5728\u7236\u7c7b\u5bf9\u5b50\u7c7b\u5f00\u653e\u7edf\u4e00\u7684\u63a5\u53e3\u3002\u5728\u5177\u4f53\u5b9e\u73b0\u4e2d\uff0c\u9996\u5148\u4f7f\u7528\u4e00\u4e2a\u7a7a\u7684TypeErasedData \u7c7b\u4f5c\u4e3a\u57fa\u7c7b\uff0c\u5e76\u5c06\u6790\u6784\u51fd\u6570\u8bbe\u4e3a\u865a\u51fd\u6570\uff0c\u65b9\u4fbf\u540e\u671f\u5b9e\u73b0\u591a\u6001\uff0c\u4e4b\u540e\u4f7f\u7528\u6d3e\u751f\u7c7bErasedData\u5b9e\u73b0\u5bf9\u591a\u79cd\u6570\u636e\u7c7b\u578b\u7684\u5f15\u7528\u3002<\/p>\n\n\n\n<p>\u7ebf\u7a0b\u5b89\u5168\u961f\u5217\u7236\u7c7b\u63d0\u4f9b\u4e86\u7ebf\u7a0b\u5b89\u5168\u7684\u64cd\u4f5c\u5bb9\u5668\u7684\u65b9\u6cd5\uff0c\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\u4f1a\u8c03\u7528primary\u5f00\u5934\u7684\u7eaf\u865a\u51fd\u6570\uff0c\u4ee5\u5b9e\u73b0\u9488\u5bf9\u67d0\u4e00\u6570\u636e\u7c7b\u578b\u7684\u5177\u4f53\u64cd\u4f5c\uff0c\u5b50\u7c7b\u5219\u9700\u8981\u9010\u4e2a\u8986\u5199\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>template &lt;typename T&gt;\nclass SafeQueue : public SafeQueueBase\n{\n   public:\n    SafeQueue() = default;\n    ~SafeQueue() = default;\n\n    bool primaryEmpty() override\n    {\n        return m_queue.empty();\n    }\n    virtual size_t primarySize() override\n    {\n        return m_queue.size();\n    }\n    virtual void primaryPush(TypeErasedData* _data) override\n    {\n        auto data = dynamic_cast&lt;ErasedData&lt;const T&gt;*&gt;(_data);\n        std::cout&lt;&lt;data&lt;&lt;std::endl;\n        if (data == nullptr) {\n            throw \"Error data type\";\n        }\n        m_queue.emplace(data-&gt;data);  \/\/ if c++11, use push instead of emplace\n    }\n    virtual void primaryPop(TypeErasedData* _data) override\n    {\n        auto data = dynamic_cast&lt;ErasedData&lt;T&gt;*&gt;(_data);\n        if (data == nullptr) {\n            throw \"Error data type\";\n        }\n        data-&gt;data = m_queue.front();\n        m_queue.pop();\n    }\n    virtual void primarySee(TypeErasedData* _data) override\n    {\n        auto data = dynamic_cast&lt;ErasedData&lt;std::vector&lt;T&gt;&gt;*&gt;(_data);\n        if (data == nullptr) {\n            throw \"Error data type\";\n        }\n        size_t size = m_queue.size();\n        for (size_t i = 0; i &lt; size; ++i) {\n            data-&gt;data.emplace_back(m_queue.front());\n            m_queue.pop();\n        }\n    }\n\n   private:\n    std::queue&lt;T&gt; m_queue;\n};\n\n<\/code><\/pre>\n\n\n\n<p>\u5b50\u7c7b\u9996\u5148\u7ee7\u627f\u81ea\u7236\u7c7b\uff0c\u5e76\u5b9e\u73b0\u4e86\u7236\u7c7b\u4e2d\u7684\u5404\u79cd\u63a5\u53e3\u51fd\u6570\u3002\u6ce8\u610f\u6b64\u5904\u4f7f\u7528\u8fd0\u884c\u65f6\u7684\u52a8\u6001\u7c7b\u578b\u8f6c\u6362\uff08dynamic_cast\uff09\uff0c\u8fd9\u662f\u5b9e\u73b0\u7c7b\u578b\u64e6\u9664\u7684\u5173\u952e\uff0c\u5229\u7528\u865a\u8868\uff0c\u901a\u8fc7\u57fa\u7c7b\u627e\u56de\u6d3e\u751f\u7c7b\uff0c\u4ece\u800c\u83b7\u5f97\u5b9e\u9645\u7684\u6570\u636e\u7c7b\u578b\u3002\u5728\u8fdb\u884c\u52a8\u6001\u7c7b\u578b\u8f6c\u6362\u7684\u65f6\u5019\u5982\u679c\u57fa\u7c7b\u4e0d\u80fd\u8f6c\u6362\u4e3a\u6307\u5b9a\u7684\u6d3e\u751f\u7c7b\uff0c\u5219\u4f1a\u8fd4\u56denullptr\uff0c\u4f9d\u6b64\u5224\u65ad\u8f6c\u6362\u662f\u5426\u6210\u529f\u3002\u5982\u679c\u8f6c\u6362\u4e0d\u6210\u529f\uff0c\u5219\u8bf4\u660e\u4f20\u5165\u7684\u6570\u636e\u7c7b\u578b\u4e0e\u5bb9\u5668\u6570\u636e\u7c7b\u578b\u4e0d\u4e00\u81f4\uff0c\u4ece\u800c\u53ef\u4ee5\u5f15\u5165\u5f02\u5e38\u5904\u7406\u7684\u65b9\u6cd5\u3002<\/p>\n\n\n\n<div class=\"wp-block-argon-alert alert\" style=\"background-color:#7889e8\"><span class=\"alert-inner--icon\"><i class=\"fa fa-info-circle\"><\/i><\/span><span class=\"alert-inner--text\">\u4e3a\u4e86\u4fdd\u8bc1\u7236\u7c7b\u5bf9\u5404\u79cd\u6570\u636e\u7c7b\u578b\u7684\u9002\u5e94\u6027\uff0c\u5728\u7236\u7c7b\u4e2d\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528void*\u6765\u652f\u6301\u5404\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u540c\u65f6\u5b50\u7c7b\u5219\u901a\u8fc7\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\uff0c\u5c06void*\u6570\u636e\u8f6c\u6362\u4e3a\u6307\u5b9a\u7684\u6570\u636e\u7c7b\u578b\u3002\u867d\u7136\u8fd9\u6837\u505a\u662f\u6700\u5bb9\u6613\u5b9e\u73b0\u7684\uff0c\u4f46\u662f\u8fd9\u6837\u7684\u5b9e\u73b0\u4e0d\u662f\u6700\u4f18\u96c5\u7684\uff0c\u800c\u4e14\u5728\u7528\u6237\u4f7f\u7528\u7684\u8fc7\u7a0b\u4e2d\uff0c\u9700\u8981\u7528\u6237\u81ea\u5df1\u4fdd\u8bc1\u8f6c\u6362\u4e3avoid*\u4e4b\u524d\u7684\u6570\u636e\u7c7b\u578b\u9700\u8981\u548c\u5b9e\u4f8b\u5316\u6a21\u677f\u7684\u7c7b\u578b\u76f8\u540c\uff0c\u5982\u679c\u4e0d\u540c\uff0c\u7f16\u8bd1\u8fc7\u7a0b\u4e2d\u4e5f\u4e0d\u4f1a\u62a5\u9519\u3002<\/span><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">\u7ebf\u7a0b\u6c60\u5b9e\u73b0<\/h2>\n\n\n\n<p>\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u6c60\u7c7b<strong>ThreadPool<\/strong>. \u8be5\u7c7b\u9700\u8981\u5b9e\u73b0\u4ee5\u4e0b\u529f\u80fd\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u8bbe\u5b9a\u7ebf\u7a0b\u6c60\u6700\u5927\u8fd0\u884c\u7ebf\u7a0b\u6570\u91cf<\/li>\n\n\n\n<li>\u521b\u5efa\u5e76\u7ef4\u62a4\u7ebf\u7a0b\u6c60<\/li>\n\n\n\n<li>\u521b\u5efa\u5e76\u7ef4\u62a4\u6d88\u606f\u961f\u5217<\/li>\n\n\n\n<li>\u6dfb\u52a0\u4efb\u52a1<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>class ThreadPool\n{\n   public:\n    ThreadPool(std::size_t _thread_count);\n    ThreadPool(ThreadPool &amp;&amp;) = delete;\n    ThreadPool(const ThreadPool &amp;) = delete;\n    ThreadPool &amp;operator=(ThreadPool &amp;&amp;) = delete;\n    ThreadPool &amp;operator=(const ThreadPool &amp;) = delete;\n    ~ThreadPool();\n\n    template &lt;typename T&gt;\n    void addQueue();\n\n    template &lt;typename T&gt;\n    std::shared_ptr&lt;SafeQueue&lt;T&gt;&gt; getQueue();\n\n    template &lt;typename Func, typename... Args&gt;\n    auto addTask(Func &amp;&amp;f, Args &amp;&amp;...args) -&gt; std::future&lt;typename std::result_of&lt;Func(Args...)&gt;::type&gt;;\n\n   private:\n    using TaskType = typename std::function&lt;void()&gt;;\n    std::mutex mux;\n    void startBuildPool();\n    std::atomic&lt;bool&gt; m_running_state{false};\n    std::size_t m_thread_count = 0;\n    std::vector&lt;std::shared_ptr&lt;std::thread&gt;&gt; m_threads;\n    SafeQueue&lt;TaskType&gt; m_task_queue;\n    std::vector&lt;std::shared_ptr&lt;SafeQueueBase&gt;&gt; m_message_queue;\n    std::condition_variable m_cv;\n};\n<\/code><\/pre>\n\n\n\n<p>\u4ee5\u4e0a\u662f\u7ebf\u7a0b\u6c60\u7c7b\u7684\u5185\u5bb9\u548c\u51fd\u6570\u58f0\u660e\uff0c\u503c\u5f97\u6ce8\u610f\u7684\u662f\u6240\u6709\u62f7\u8d1d\u3001\u79fb\u52a8\u7684\u6784\u9020\u51fd\u6570\u548c\u8d4b\u503c\u8fd0\u7b97\u7b26\u90fd\u58f0\u660e\u4e3a\u5220\u9664\uff0c\u8fd9\u662f\u4e3a\u4e86\u9632\u6b62\u5728\u62f7\u8d1d\u548c\u79fb\u52a8\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u6f5c\u5728\u7684\u9519\u8bef\u3002\u5176\u4e2d\uff0cmux\u662f\u63a7\u5236\u4efb\u52a1\u5206\u914d\u7684\u4e92\u65a5\u91cf\u3001m_running_state\u662f\u63a7\u5236\u7ebf\u7a0b\u6c60\u8fd0\u884c\u72b6\u6001\u7684\u539f\u5b50\u53d8\u91cf\u3001m_threads\u5b58\u653e\u5404\u4e2a\u7ebf\u7a0b\u7684\u667a\u80fd\u6307\u9488\u3001m_task_queue\u662f\u5b58\u653e\u4efb\u52a1\u7684\u961f\u5217\u3001m_message_queue\u5b58\u653e\u7740\u5404\u79cd\u6570\u636e\u7c7b\u578b\u7684\u6d88\u606f\u961f\u5217\u3001m_cv\u4f5c\u4e3a\u4fe1\u53f7\u91cf\u8c03\u8282\u5404\u90e8\u5206\u6709\u5e8f\u8fd0\u884c\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u51e0\u4e2a\u51fd\u6570\u7684\u5177\u4f53\u5b9e\u73b0\uff0c\u5b8c\u6574\u7684\u4ee3\u7801\u9644\u4e8e\u6587\u672b\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6784\u9020\u51fd\u6570<\/h3>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>inline ThreadPool::ThreadPool(std::size_t _thread_count)\n{\n    auto sys_max_threads = std::thread::hardware_concurrency();  \/\/ get system max threads number\n    if (_thread_count &lt;= 0 || _thread_count &gt;= sys_max_threads) {\n        m_thread_count = sys_max_threads;\n        std::cerr &lt;&lt; \"WARNING: use capable thread count:\" &lt;&lt; m_thread_count &lt;&lt; std::endl;\n    } else {\n        m_thread_count = _thread_count;\n    }\n\n    m_threads.reserve(m_thread_count);\n    m_running_state = true;\n    startBuildPool();\n}\n<\/code><\/pre>\n\n\n\n<p>\u5728\u6784\u9020\u51fd\u6570\u4e2d\uff0c\u4e3b\u8981\u8bbe\u5b9a\u4e86\u7ebf\u7a0b\u6c60\u6700\u5927\u8fd0\u884c\u7684\u7ebf\u7a0b\u6570\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5bf9\u4e8e\u7528\u6237\u8f93\u5165\u7684\u7ebf\u7a0b\u6570\uff0c\u5e94\u5224\u65ad\u5176\u662f\u5426\u8d85\u8fc7\u786c\u4ef6\u652f\u6301\u7684\u6700\u5927\u7ebf\u7a0b\u6570\uff0c\u8fd9\u91cc\u4f7f\u7528\u4e86std::thread::hardware_concurrency();\u83b7\u53d6\u786c\u4ef6\u6700\u5927\u7ebf\u7a0b\u6570\u3002\u5728\u8f93\u5165\u7684\u7ebf\u7a0b\u6570\u8d85\u8fc7\u786c\u4ef6\u652f\u6301\u7684\u6700\u5927\u7ebf\u7a0b\u6570\u6216\u8005\u8f93\u5165\u7ebf\u7a0b\u6570\u5c0f\u4e8e0\u65f6\u4f7f\u7528\u6700\u5927\u7684\u7ebf\u7a0b\u6570\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6790\u6784\u51fd\u6570<\/h3>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>inline ThreadPool::~ThreadPool()\n{\n    m_running_state = false;\n    m_cv.notify_all();  \/\/ break all thread in block state\n    for (auto &amp;thread : m_threads) {\n        if (thread-&gt;joinable()) {\n            thread-&gt;join();\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>\u5728\u6790\u6784\u65f6\uff0c\u5e94\u901a\u77e5\u6240\u6709\u7b49\u5f85\u7ebf\u7a0b\u53d6\u6d88\u7b49\u5f85\u72b6\u6001\uff0c\u4f7f\u7528m_cv.notify_all();\u5b9e\u73b0\uff0c\u4e4b\u540e\u5c06\u6240\u6709\u53ef\u5408\u5e76\u7684\u5b50\u7ebf\u7a0b\u5168\u90e8\u5408\u5e76\u5230\u4e3b\u7ebf\u7a0b\u4e2d\uff0c\u4e5f\u5c31\u662f\u4f9d\u6b21\u5173\u95ed\u5404\u4e2a\u5b50\u7ebf\u7a0b\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4efb\u52a1\u6dfb\u52a0\u51fd\u6570<\/h3>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>template &lt;typename Func, typename... Args&gt;\ninline auto ThreadPool::addTask(Func &amp;&amp;f, Args &amp;&amp;...args) -&gt; std::future&lt;typename std::result_of&lt;Func(Args...)&gt;::type&gt;\n{\n    using return_type = typename std::result_of&lt;Func(Args...)&gt;::type;\n    auto task = std::make_shared&lt;std::packaged_task&lt;return_type()&gt;&gt;(std::bind(std::forward&lt;Func&gt;(f), std::forward&lt;Args&gt;(args)...));\n    TaskType thread_task = &#91;task]() { (*task)(); };\n    m_task_queue.push(thread_task);\n    m_cv.notify_one();\n    return task-&gt;get_future();\n}\n<\/code><\/pre>\n\n\n\n<p>\u4efb\u52a1\u6dfb\u52a0\u51fd\u6570\u662f\u7ebf\u7a0b\u6c60\u5b9e\u73b0\u7684\u6838\u5fc3\uff0c\u4e0b\u9762\u9010\u70b9\u89e3\u91ca\u5982\u4f55\u8bbe\u8ba1\u4efb\u52a1\u6dfb\u52a0\u51fd\u6570\u4f7f\u4e4b\u80fd\u517c\u5bb9\u6709\u7740\u5404\u79cd\u8fd4\u56de\u503c\u7c7b\u578b\u3001\u5404\u79cd\u6570\u76ee\u8f93\u5165\u53c2\u6570\u7684\u51fd\u6570\u3001lambda\u8868\u8fbe\u5f0f\u3001\u4eff\u51fd\u6570\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u4f7f\u7528\u53ef\u53d8\u53c2\u6570\u6a21\u677f\u6355\u83b7\u51fd\u6570\u3001lambda\u8868\u8fbe\u5f0f\u3001\u4eff\u51fd\u6570\u4ee5\u53ca\u5b83\u4eec\u7684\u53c2\u6570\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528std::result_of::type\u7c7b\u578b\u8403\u53d6\u5668\uff0c\u83b7\u5f97\u51fd\u6570\u3001lambda\u8868\u8fbe\u5f0f\u3001\u4eff\u51fd\u6570\u7684\u8fd4\u56de\u503c\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528bind\u5c06\u51fd\u6570\u4e0e\u53c2\u6570\u8fdb\u884c\u7ed1\u5b9a\uff0c\u5728\u7ed1\u5b9a\u65f6\u4e3a\u4e86\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u62f7\u8d1d\u5f00\u652f\uff0c\u4f7f\u7528\u4e86\u5b8c\u7f8e\u8f6c\u53d1\u5c06\u51fd\u6570\u53c2\u6570\u8f6c\u53d1\u5230\u7ed1\u5b9a\u5bf9\u8c61\u4e2d\u3002<\/li>\n\n\n\n<li>\u6253\u5305\u6210packaged_task\u5bf9\u8c61\uff0c\u65b9\u4fbf\u7ba1\u7406promise\u548cfuture\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528\u667a\u80fd\u6307\u9488\u521b\u5efapackaged_task\u5bf9\u8c61\u5e76\u81ea\u52a8\u7ba1\u7406\u5185\u5b58\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528\u4e00\u4e2alambda\u8868\u8fbe\u5f0f\uff0c\u5c06task\u5c01\u88c5\u6210\u4e00\u4e2a\u6ca1\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u6307\u9488\uff0c\u6ce8\u610f\u8fd9\u91cc\u7684TaskType \u539f\u578b\u662ftypename std::function; \u56e0\u4e3a\u540e\u671f\u901a\u8fc7task\u7684get_future()\u65b9\u6cd5\u83b7\u53d6\u7ebf\u7a0b\u6267\u884c\u7ed3\u679c\uff0c\u8fd9\u91cc\u5c31\u4e0d\u9700\u8981\u4f7f\u7528\u8fd4\u56de\u503c\u4e86\uff0c\u800c\u4e14\u6ca1\u6709\u8fd4\u56de\u503c\u7684\u4efb\u52a1\u5bf9\u8c61\uff08\u51c6\u786e\u6765\u8bf4\u662f\u5177\u6709\u7edf\u4e00\u7684void\u8fd4\u56de\u503c\u7684\u4efb\u52a1\u5bf9\u8c61\uff09\u4f7f\u5f97\u5728\u5bb9\u5668\u4e2d\u5b58\u50a8\u4efb\u52a1\u548c\u8c03\u7528\u6267\u884c\u4efb\u52a1\u66f4\u52a0\u65b9\u4fbf\u3002<\/li>\n\n\n\n<li>\u5c06\u51fd\u6570\u6307\u9488\u653e\u5165\u4efb\u52a1\u961f\u5217\u4e2d\uff0c\u5e76\u901a\u77e5\u4e00\u4e2a\u7a7a\u95f2\u7ebf\u7a0b\u5f00\u59cb\u6267\u884c\u4efb\u52a1\u3002<\/li>\n\n\n\n<li>\u4efb\u52a1\u6267\u884c\u5b8c\u540e\u83b7\u53d6\u6267\u884c\u7ed3\u679c\uff0c\u8be5\u8fc7\u7a0b\u662f\u5f02\u6b65\u7684\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u6784\u5efa\u7ebf\u7a0b\u6c60\u5e76\u5206\u914d\u4efb\u52a1<\/h3>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code> inline void ThreadPool::startBuildPool()\n{\n    for (size_t i = 0; i &lt; m_thread_count; ++i) {\n        auto t = &#91;this, i]() {\n            while (this-&gt;m_running_state) {\n                TaskType task;\n                std::unique_lock&lt;std::mutex&gt; lck(mux);\n                if (this-&gt;m_task_queue.empty()) {\n                    m_cv.wait(lck);\n                }\n                \/\/ need to check again when thread pool destructed can also make wait() pass by notice_all\n                \/\/ and when the condition variable is waiting, it well unlock lck, this may cause many threads are waiting at the same time\n                if (this-&gt;m_running_state == false || m_task_queue.empty()) {\n                    std::this_thread::sleep_for(std::chrono::milliseconds(2));\n                    continue;\n                }\n                this-&gt;m_task_queue.pop(task);\n                lck.unlock();\n                try {\n                    task();  \/\/ run task;\n                } catch (...) {\n                }\n            }\n        };\n        m_threads.emplace_back(std::make_shared&lt;std::thread&gt;(std::thread(t)));\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>\u6784\u5efa\u7ebf\u7a0b\u6c60\u7684\u51fd\u6570\u5185\u90e8\u6709\u4e00\u4e2a\u5927\u5faa\u73af\uff0c\u5728\u6267\u884c\u8fc7\u7a0b\u4e2d\uff0c\u7a0b\u5e8f\u5c06\u7acb\u5373\u521b\u5efa<strong>m_thread_count<\/strong>\u4e2a\u7ebf\u7a0b\u5e76\u5c06\u7ebf\u7a0b\u79fb\u5165\u5230\u7ebf\u7a0b\u6c60\u4e2d\uff0c\u8be5\u51fd\u6570\u6267\u884c\u5b8c\u5c31\u7acb\u5373\u9000\u51fa\u4e86\uff0c\u5269\u4f59\u7684\u5de5\u4f5c\u5c06\u6709\u7531<strong>lambda<\/strong>\u8868\u8fbe\u5f0f<strong>t<\/strong>\u5b8c\u6210\u3002<\/p>\n\n\n\n<p>\u521b\u5efa\u5b8c\u7684\u6bcf\u4e2a\u5b50\u7ebf\u7a0b\u90fd\u4f1a\u8fd0\u884c<strong>t<\/strong>\u51fd\u6570\uff0c\u800c\u53ea\u6709\u5728<strong>m_running_state<\/strong>\u4e3a<strong>false<\/strong>\u65f6\uff0c\u5b50\u7ebf\u7a0b\u624d\u4f1a\u9000\u51fa\uff0c\u5426\u5219\u5c06\u65e0\u9650\u5faa\u73af\uff0c\u6bcf\u4e00\u4e2a\u5faa\u73af\u90fd\u8fd0\u884c\u4e86\u4e00\u4e2a\u4efb\u52a1\u3002\u4e0b\u9762\u8be6\u7ec6\u770b\u6bcf\u4e00\u6b21\u5faa\u73af\u65f6\u5019\u7684\u6267\u884c\u8fc7\u7a0b\u3002\u9996\u5148\uff0c\u4e3a\u4e86\u9632\u6b62\u5b50\u7ebf\u7a0b\u83b7\u53d6\u4efb\u52a1\u8fc7\u7a0b\u4e2d\u4e0e\u5176\u4ed6\u5b50\u7ebf\u7a0b\u5f62\u6210\u7adf\u6001\uff0c\u9700\u8981\u4e3a\u83b7\u53d6\u4efb\u52a1\u8fc7\u7a0b\u52a0\u9501\uff0c\u8fd9\u91cc\u4f7f\u7528\u4e86\u57fa\u4e8e\u4e92\u65a5\u9501\u3002\u62ff\u5230\u4e86\u4e92\u65a5\u9501\u4e4b\u540e\u5b50\u7ebf\u7a0b\u4f1a\u5224\u65ad\u4efb\u52a1\u961f\u5217\u4e2d\u662f\u5426\u4e3a\u7a7a\uff0c\u5982\u679c\u4e3a\u7a7a\uff0c\u5219\u7ebf\u7a0b\u8fdb\u5165\u963b\u585e\u72b6\u6001\uff0c\u4e0d\u6d88\u8017\u8d44\u6e90\u3002<strong>\u6ce8\u610f\u8be5\u8fc7\u7a0b\uff0c\u4f7f\u7528\u6761\u4ef6\u53d8\u91cf\u7684wait()\u65b9\u6cd5\u65f6\uff0c\u9996\u5148\u6761\u4ef6\u53d8\u91cf\u4f1a\u83b7\u53d6\u9501\uff0c\u4e4b\u540e\u518d\u91ca\u653e\u9501\uff0c\u5e76\u8fdb\u5165\u963b\u585e\u72b6\u6001\uff0c\u8fd9\u6837\u53ef\u4ee5\u8ba9\u7ebf\u7a0b\u6c60\u4e2d\u6240\u6709\u7ebf\u7a0b\u540c\u65f6\u8fdb\u5165\u963b\u585e\u72b6\u6001\u3002<\/strong><\/p>\n\n\n\n<p>\u4e4b\u540e\uff0c\u5f53\u4efb\u52a1\u961f\u5217\u4e2d\u51fa\u73b0\u4efb\u52a1\u65f6\uff0c\u5c06\u5524\u8d77\u5176\u4e2d\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u4e4b\u540e\u91cd\u65b0\u52a0\u9501\uff0c\u8fdb\u5165\u540e\u9762\u7684\u6d41\u7a0b\u3002\u5524\u8d77\u7ebf\u7a0b\u7684\u4fe1\u53f7\u9664\u4e86\u6709\u65b0\u4efb\u52a1\u6dfb\u52a0\u5230\u961f\u5217\u4ee5\u5916\uff0c\u8fd8\u6709\u4e00\u79cd\u53ef\u80fd\uff0c\u5c31\u662f\u5916\u90e8\u547d\u4ee4\u6240\u6709\u5b50\u7ebf\u7a0b\u7a0b\u9000\u51fa\uff0c\u8fd9\u56de\u5bfc\u81f4\u6240\u6709\u7ebf\u7a0b\u7684\u963b\u585e\u72b6\u6001\u53d6\u6d88\uff0c\u800c\u6b64\u65f6\u4efb\u52a1\u961f\u5217\u4e2d\u4e0d\u4e00\u5b9a\u8fd8\u6709\u5f85\u6267\u884c\u7684\u4efb\u52a1\uff0c\u56e0\u6b64\u9700\u8981\u6dfb\u52a0\u4e00\u4e2a\u989d\u5916\u7684\u5224\u65ad\uff0c\u5982\u679c\u7ebf\u7a0b\u6c60\u8fd0\u884c\u72b6\u6001\u4e3afalse\u6216\u8005\u4efb\u52a1\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u8df3\u8fc7\u6b64\u8f6e\u5faa\u73af\u3002<\/p>\n\n\n\n<p>\u5728\u786e\u5b9e\u662f\u7531\u65b0\u6dfb\u52a0\u8fdb\u4efb\u52a1\u961f\u5217\u7684\u4fe1\u53f7\u5524\u8d77\u7ebf\u7a0b\u65f6\uff0c\u5728\u4efb\u52a1\u961f\u5217\u4e2d\u5f39\u51fa\u6700\u5148\u52a0\u5165\u7684\u4efb\u52a1\uff0c\u5e76\u5f00\u59cb\u6267\u884c\u4efb\u52a1\u3002<strong>\u6ce8\u610f\u5728\u6267\u884c\u4efb\u52a1\u4e4b\u524d\u4e00\u5b9a\u8981\u5148\u91ca\u653e\u6389\u4e92\u65a5\u9501\uff0c\u5982\u679c\u4e0d\u91ca\u653e\uff0c\u5176\u4ed6\u5b50\u7ebf\u7a0b\u5c06\u65e0\u6cd5\u5728\u8be5\u4efb\u52a1\u8fd0\u884c\u65f6\u5bf9\u4efb\u52a1\u961f\u5217\u8fdb\u884c\u64cd\u4f5c\u3002<\/strong>\u6b64\u5916\uff0c\u4e3a\u4e86\u9632\u6b62\u5176\u4e2d\u67d0\u4e00\u4e2a\u5b50\u7ebf\u7a0b\u5728\u6267\u884c\u4efb\u52a1\u7684\u8fc7\u7a0b\u4e2d\u51fa\u9519\u4ece\u800c\u9020\u6210\u6240\u6709\u7ebf\u7a0b\u5d29\u6e83\u7684\u60c5\u51b5\u51fa\u73b0\uff0c\u9700\u8981\u5728\u6bcf\u4e00\u4e2a\u5b50\u7ebf\u7a0b\u6267\u884c\u4efb\u52a1\u7684\u65f6\u5019\u4f7f\u7528\u9519\u8bef\u6355\u83b7\u8bed\u53e5\uff0c<strong>&#8230;<\/strong>\u8868\u793a\u6355\u83b7\u6240\u6709\u9519\u8bef\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u60c5\u51b5\u7ec6\u5316\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u8fd0\u884c\u5b9e\u4f8b<\/h2>\n\n\n\n<p>\u4e0b\u9762\u7ed9\u51fa3\u79cd\u8fd0\u884c\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u4f7f\u7528\u672c\u6587\u5b9e\u73b0\u7ebf\u7a0b\u6c60\u7684\u57fa\u672c\u6d41\u7a0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>#include &lt;chrono&gt;\n#include &lt;future&gt;\n#include &lt;memory&gt;\n#include &lt;string&gt;\n#include &lt;thread&gt;\n#include &lt;vector&gt;\n\n#include \"safe_queue_base.h\"\n#include \"thread_pool.h\"\n\nusing namespace std;\n\nvoid test_multithreads()\n{\n    ThreadPool tp(12);\n    auto f = &#91;](int a, int b, int idx) {\n        cout&lt;&lt;\"in sub thread: \"&lt;&lt;idx&lt;&lt;endl;\n        return a+b+idx; };\n    vector&lt;future&lt;int&gt;&gt; res(16);\n    for (size_t i = 0; i &lt; res.size(); ++i) {\n        res&#91;i] = tp.addTask(f, 1, 1, i);\n    }\n    this_thread::sleep_for(2s);\n    for (size_t i = 0; i &lt; res.size(); ++i) {\n        cout &lt;&lt; \"res: \" &lt;&lt; res&#91;i].get() &lt;&lt; endl;\n    }\n}\n\nvoid test_message_queue()\n{\n    ThreadPool tp(8);\n    tp.addQueue&lt;int&gt;();\n    tp.addQueue&lt;double&gt;();\n\n    auto f = &#91;](ThreadPool* ptr, int s_time) {\n        auto int_queue = ptr-&gt;getQueue&lt;int&gt;();\n        auto double_queue = ptr-&gt;getQueue&lt;double&gt;();\n        if (int_queue == nullptr || double_queue == nullptr) {\n            throw \"without int\/double queue\";\n        }\n        if (s_time == 1) {  \/\/ write something\n            for (int i = 0; i &lt; 10; ++i) {\n                int_queue-&gt;push(i);\n                double d = (1 + i) \/ 2.0;\n                double_queue-&gt;push(d);\n            }\n        }\n        if (s_time == 0) {\n            this_thread::sleep_for(1s);\n            auto s1 = int_queue-&gt;size();\n            while (s1 != int_queue-&gt;size()) {\n                std::this_thread::sleep_for(10ms);\n                s1 = int_queue-&gt;size();\n            }\n\n            vector&lt;double&gt; all_d;\n            double_queue-&gt;see(all_d);\n            for (size_t i = 0; i &lt; s1; ++i) {\n                int i_value;\n                int_queue-&gt;pop(i_value);\n                cout &lt;&lt; \"idx:\" &lt;&lt; i &lt;&lt; \" int value: \" &lt;&lt; i_value &lt;&lt; \" double value:\" &lt;&lt; all_d&#91;i] &lt;&lt; endl;\n            }\n        }\n    };\n    tp.addTask(f, &amp;tp, 1);\n    tp.addTask(f, &amp;tp, 0);\n    this_thread::sleep_for(3s);\n    \/* this_thread::sleep_for(5s); *\/\n}\n\nclass MyThreadTask\n{\n    shared_ptr&lt;ThreadPool&gt; m_thread_pool;\n\n   public:\n    MyThreadTask()= default;\n    ~MyThreadTask() = default;\n    auto operator()(int x, int y)\n    {\n        return x * x + y;\n    }\n};\n\nvoid test_class()\n{\n    ThreadPool tp(-1);\n    MyThreadTask t;\n    auto res = tp.addTask(t,2,3);\n    cout&lt;&lt;res.get()&lt;&lt;endl;\n\n\n}\n\nint main(int argc, char* argv&#91;])\n{\n    \/* test_multithreads(); *\/\n    \/* test_message_queue(); *\/\n    test_class();\n\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u9644\u4ef6\uff1a\u5b8c\u6574\u7a0b\u5e8f\u4ee3\u7801<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">safe_queue_base.h<\/h3>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>#ifndef SAFE_QUEUE_BASE_H\n#define SAFE_QUEUE_BASE_H\n#include &lt;functional&gt;\n#include &lt;iostream&gt;\n#include &lt;mutex&gt;\n#include &lt;queue&gt;\n#include &lt;shared_mutex&gt;\n#include &lt;vector&gt;\n\nstruct TypeErasedData {\n    virtual ~TypeErasedData() {}\n};\n\ntemplate &lt;typename T&gt;\nstruct ErasedData : public TypeErasedData {\n    ErasedData(T&amp; value) : data(value) {}\n    T&amp; data;\n};\n\nclass SafeQueueBase\n{\n   public:\n    SafeQueueBase() = default;\n    SafeQueueBase(SafeQueueBase&amp;&amp;) = delete;\n    SafeQueueBase(const SafeQueueBase&amp;) = delete;\n    SafeQueueBase&amp; operator=(SafeQueueBase&amp;&amp;) = delete;\n    SafeQueueBase&amp; operator=(const SafeQueueBase&amp;) = delete;\n\n    bool empty()\n    {\n        std::shared_lock&lt;std::shared_timed_mutex&gt; lck(mux);\n        return primaryEmpty();\n    }\n    size_t size()\n    {\n        std::shared_lock&lt;std::shared_timed_mutex&gt; lck(mux);\n        return primarySize();\n    }\n\n    template &lt;typename T&gt;\n    void pop(T&amp; _data_output)\n    {\n        std::unique_lock&lt;std::shared_timed_mutex&gt; lck(mux);\n        ErasedData&lt;T&gt; data_output(_data_output);\n        primaryPop(&amp;data_output);\n    }\n\n    template &lt;typename T&gt;\n    void push(const T&amp; _data)\n    {\n        std::unique_lock&lt;std::shared_timed_mutex&gt; lck(mux);\n        ErasedData&lt;const T&gt; data(_data);\n        primaryPush(&amp;data);\n    }\n    template &lt;typename T&gt;\n    void see(std::vector&lt;T&gt;&amp; _data_output)\n    {\n        std::shared_lock&lt;std::shared_timed_mutex&gt; lck(mux);\n        ErasedData&lt;std::vector&lt;T&gt;&gt; data_output(_data_output);\n        primarySee(&amp;data_output);\n    }\n\n    virtual ~SafeQueueBase(){};\n\n   protected:\n    virtual void primaryPush(TypeErasedData* _data) = 0;\n    virtual void primaryPop(TypeErasedData* _data) = 0;\n    virtual void primarySee(TypeErasedData* _data) = 0;\n    virtual bool primaryEmpty() = 0;\n    virtual size_t primarySize() = 0;\n\n   private:\n    std::shared_timed_mutex mux;  \/\/ WARN: need to support c++14\n};\n\ntemplate &lt;typename T&gt;\nclass SafeQueue : public SafeQueueBase\n{\n   public:\n    SafeQueue() = default;\n    ~SafeQueue() = default;\n\n    bool primaryEmpty() override\n    {\n        return m_queue.empty();\n    }\n    virtual size_t primarySize() override\n    {\n        return m_queue.size();\n    }\n    virtual void primaryPush(TypeErasedData* _data) override\n    {\n        auto data = dynamic_cast&lt;ErasedData&lt;const T&gt;*&gt;(_data);\n        std::cout&lt;&lt;data&lt;&lt;std::endl;\n        if (data == nullptr) {\n            throw \"Error data type\";\n        }\n        m_queue.emplace(data-&gt;data);  \/\/ if c++11, use push instead of emplace\n    }\n    virtual void primaryPop(TypeErasedData* _data) override\n    {\n        auto data = dynamic_cast&lt;ErasedData&lt;T&gt;*&gt;(_data);\n        if (data == nullptr) {\n            throw \"Error data type\";\n        }\n        data-&gt;data = m_queue.front();\n        m_queue.pop();\n    }\n    virtual void primarySee(TypeErasedData* _data) override\n    {\n        auto data = dynamic_cast&lt;ErasedData&lt;std::vector&lt;T&gt;&gt;*&gt;(_data);\n        if (data == nullptr) {\n            throw \"Error data type\";\n        }\n        size_t size = m_queue.size();\n        for (size_t i = 0; i &lt; size; ++i) {\n            data-&gt;data.emplace_back(m_queue.front());\n            m_queue.pop();\n        }\n    }\n\n   private:\n    std::queue&lt;T&gt; m_queue;\n};\n\n#endif  \/\/ SAFE_QUEUE_BASE_H<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">thread_pool.h<\/h3>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>#ifndef THREAD_POOL_H\n#define THREAD_POOL_H\n#include &lt;algorithm&gt;\n#include &lt;atomic&gt;\n#include &lt;chrono&gt;\n#include &lt;future&gt;\n#include &lt;iostream&gt;\n#include &lt;memory&gt;\n#include &lt;mutex&gt;\n#include &lt;thread&gt;\n#include &lt;vector&gt;\n#include &lt;string&gt;\n\n\/* #define DEBUG *\/\n#include \"safe_queue_base.h\"\n\nclass <em>ThreadPool<\/em>\n{\n&nbsp;&nbsp;&nbsp;public:\n&nbsp;&nbsp;&nbsp;&nbsp;ThreadPool(std::size_t _thread_count);\n&nbsp;&nbsp;&nbsp;&nbsp;ThreadPool(ThreadPool &amp;&amp;) = delete;\n&nbsp;&nbsp;&nbsp;&nbsp;ThreadPool(const ThreadPool &amp;) = delete;\n&nbsp;&nbsp;&nbsp;&nbsp;ThreadPool &amp;operator=(ThreadPool &amp;&amp;) = delete;\n&nbsp;&nbsp;&nbsp;&nbsp;ThreadPool &amp;operator=(const ThreadPool &amp;) = delete;\n&nbsp;&nbsp;&nbsp;&nbsp;~ThreadPool();\n\n&nbsp;&nbsp;&nbsp;&nbsp;template &lt;typename T&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;void addQueue();\n\n&nbsp;&nbsp;&nbsp;&nbsp;template &lt;typename T&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;std::shared_ptr&lt;SafeQueue&lt;T&gt;&gt; getQueue();\n\n&nbsp;&nbsp;&nbsp;&nbsp;template &lt;typename Func, typename... Args&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;auto addTask(Func &amp;&amp;f, Args &amp;&amp;...args) -&gt; std::future&lt;typename std::result_of&lt;Func(Args...)&gt;::type&gt;;\n\n&nbsp;&nbsp;&nbsp;private:\n&nbsp;&nbsp;&nbsp;&nbsp;using TaskType = typename std::function&lt;void()&gt;;\n&nbsp;&nbsp;&nbsp;&nbsp;std::mutex mux;\n&nbsp;&nbsp;&nbsp;&nbsp;void startBuildPool();\n&nbsp;&nbsp;&nbsp;&nbsp;std::atomic&lt;bool&gt; m_running_state{false};\n&nbsp;&nbsp;&nbsp;&nbsp;std::size_t m_thread_count = 0;\n&nbsp;&nbsp;&nbsp;&nbsp;std::vector&lt;std::shared_ptr&lt;std::thread&gt;&gt; m_threads;\n&nbsp;&nbsp;&nbsp;&nbsp;SafeQueue&lt;TaskType&gt; m_task_queue;\n&nbsp;&nbsp;&nbsp;&nbsp;std::vector&lt;std::shared_ptr&lt;SafeQueueBase&gt;&gt; m_message_queue;\n&nbsp;&nbsp;&nbsp;&nbsp;std::condition_variable m_cv;\n};\n\n\ninline ThreadPool::ThreadPool(std::size_t _thread_count)\n{\n&nbsp;&nbsp;&nbsp;&nbsp;auto sys_max_threads = std::thread::hardware_concurrency();&nbsp;&nbsp;\/\/ get system max threads number\n&nbsp;&nbsp;&nbsp;&nbsp;if (_thread_count &lt;= 0 || _thread_count &gt;= sys_max_threads) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_thread_count = sys_max_threads;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cerr &lt;&lt; \"WARNING: use capable thread count:\" &lt;&lt; m_thread_count &lt;&lt; std::endl;\n&nbsp;&nbsp;&nbsp;&nbsp;} else {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_thread_count = _thread_count;\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;m_threads.reserve(m_thread_count);\n&nbsp;&nbsp;&nbsp;&nbsp;m_running_state = true;\n&nbsp;&nbsp;&nbsp;&nbsp;startBuildPool();\n}\n\ninline ThreadPool::~ThreadPool()\n{\n&nbsp;&nbsp;&nbsp;&nbsp;m_running_state = false;\n&nbsp;&nbsp;&nbsp;&nbsp;m_cv.notify_all();&nbsp;&nbsp;\/\/ break all thread in block state\n&nbsp;&nbsp;&nbsp;&nbsp;for (auto &amp;thread : m_threads) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (thread-&gt;joinable()) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;thread-&gt;join();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;}\n}\n\n\ntemplate &lt;typename T&gt;\ninline void ThreadPool::addQueue()\n{\n&nbsp;&nbsp;&nbsp;&nbsp;std::shared_ptr&lt;SafeQueueBase&gt; queue = std::make_shared&lt;SafeQueue&lt;T&gt;&gt;();\n&nbsp;&nbsp;&nbsp;&nbsp;m_message_queue.push_back(std::move(queue));\n}\n\ntemplate &lt;typename T&gt;\ninline std::shared_ptr&lt;SafeQueue&lt;T&gt;&gt; ThreadPool::getQueue()\n{\n&nbsp;&nbsp;&nbsp;&nbsp;for (const auto &amp;e : m_message_queue) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ use dynamic_pointer_cast to transform shared_ptr from base class to subclass\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (auto ptr = std::dynamic_pointer_cast&lt;SafeQueue&lt;T&gt;&gt;(e)) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return ptr;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;return nullptr;\n}\n\ntemplate &lt;typename Func, typename... Args&gt;\ninline auto ThreadPool::addTask(Func &amp;&amp;f, Args &amp;&amp;...args) -&gt; std::future&lt;typename std::result_of&lt;Func(Args...)&gt;::type&gt;\n{\n&nbsp;&nbsp;&nbsp;&nbsp;using return_type = typename std::result_of&lt;Func(Args...)&gt;::type;\n&nbsp;&nbsp;&nbsp;&nbsp;auto task = std::make_shared&lt;std::packaged_task&lt;return_type()&gt;&gt;(std::bind(std::forward&lt;Func&gt;(f), std::forward&lt;Args&gt;(args)...));\n&nbsp;&nbsp;&nbsp;&nbsp;TaskType thread_task = &#91;task]() { (*task)(); };&nbsp;&nbsp;\/\/ \u4e0d\u9700\u8981\u79fb\u52a8\u4e86\uff0c\u4f7f\u7528 shared_ptr\n&nbsp;&nbsp;&nbsp;&nbsp;m_task_queue.push(thread_task);\n&nbsp;&nbsp;&nbsp;&nbsp;m_cv.notify_one();\n&nbsp;&nbsp;&nbsp;&nbsp;return task-&gt;get_future();\n}\n\n&nbsp;inline void ThreadPool::startBuildPool()\n{\n&nbsp;&nbsp;&nbsp;&nbsp;for (size_t i = 0; i &lt; m_thread_count; ++i) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;auto t = &#91;this, i]() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while (this-&gt;m_running_state) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TaskType task;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::unique_lock&lt;std::mutex&gt; lck(mux);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (this-&gt;m_task_queue.empty()) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_cv.wait(lck);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ need to check again when thread pool destructed can also make wait() pass by notice_all\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ and when the condition variable is waiting, it well unlock lck, this may cause many threads are waiting at the same time\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (this-&gt;m_running_state == false || m_task_queue.empty()) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::this_thread::sleep_for(std::chrono::milliseconds(2));\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;continue;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this-&gt;m_task_queue.pop(task);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lck.unlock();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;task();&nbsp;&nbsp;\/\/ run task;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} catch (...) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;};\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_threads.emplace_back(std::make_shared&lt;std::thread&gt;(std::thread(t)));\n&nbsp;&nbsp;&nbsp;&nbsp;}\n}\n#endif&nbsp;&nbsp;\/\/ THREAD_POOL_<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7c7b\u89c6\u56fe\u8bbe\u8ba1 \u62df\u5b9e\u73b0\u7684\u529f\u80fd \u7c7b\u8bbe\u8ba1 \u5982\u4e0a\u56fe\u6240\u793a\uff0c\u8be5\u5b9e\u73b0\u5305\u542b\u56db\u4e2a\u7c7b\uff0c\u5206\u522b\u662f\u5b9e\u73b0\u7ebf\u7a0b\u5b89\u5168\u7684\u961f\u5217\u62bd\u7ebf\u57fa\u7c7b\uff0c\u7ee7\u627f\u81ea\u62bd\u8c61\u57fa [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":254,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[10,11,12,13],"class_list":["post-243","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-9","tag-10","tag-11","tag-12","tag-13"],"_links":{"self":[{"href":"https:\/\/blog.heene.tech\/index.php\/wp-json\/wp\/v2\/posts\/243","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.heene.tech\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.heene.tech\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.heene.tech\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.heene.tech\/index.php\/wp-json\/wp\/v2\/comments?post=243"}],"version-history":[{"count":0,"href":"https:\/\/blog.heene.tech\/index.php\/wp-json\/wp\/v2\/posts\/243\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.heene.tech\/index.php\/wp-json\/wp\/v2\/media\/254"}],"wp:attachment":[{"href":"https:\/\/blog.heene.tech\/index.php\/wp-json\/wp\/v2\/media?parent=243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.heene.tech\/index.php\/wp-json\/wp\/v2\/categories?post=243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.heene.tech\/index.php\/wp-json\/wp\/v2\/tags?post=243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}