{"id":164,"date":"2025-05-27T20:01:32","date_gmt":"2025-05-27T12:01:32","guid":{"rendered":"https:\/\/look123.top\/?p=164"},"modified":"2025-05-27T20:03:53","modified_gmt":"2025-05-27T12:03:53","slug":"%e7%ba%bf%e7%a8%8b%e6%b1%a0","status":"publish","type":"post","link":"https:\/\/look123.top\/index.php\/2025\/05\/27\/%e7%ba%bf%e7%a8%8b%e6%b1%a0\/","title":{"rendered":"\u7ebf\u7a0b\u6c60"},"content":{"rendered":"\n<p>\u7ebf\u7a0b\u6c60\uff08Thread Pool\uff09\u662f\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\u7684\u4e00\u79cd\u91cd\u8981\u6280\u672f\uff0c\u5b83\u901a\u8fc7\u91cd\u590d\u5229\u7528\u4e00\u7ec4\u5df2\u521b\u5efa\u7684\u7ebf\u7a0b\u6765\u6267\u884c\u591a\u4e2a\u4efb\u52a1\uff0c\u907f\u514d\u4e86\u9891\u7e41\u521b\u5efa\u548c\u9500\u6bc1\u7ebf\u7a0b\u7684\u5f00\u9500\u3002\u4f7f\u7528\u7ebf\u7a0b\u6c60\u5e26\u6765\u4ee5\u4e0b\u51e0\u4e2a\u4e3b\u8981\u597d\u5904\uff1a<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u63d0\u9ad8\u6027\u80fd\uff0c\u964d\u4f4e\u8d44\u6e90\u6d88\u8017<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7ebf\u7a0b\u7684\u521b\u5efa\u548c\u9500\u6bc1\u662f\u6709\u6210\u672c\u7684\uff0c\u5c24\u5176\u662f\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u9891\u7e41\u521b\u5efa\u7ebf\u7a0b\u4f1a\u5f71\u54cd\u7cfb\u7edf\u6027\u80fd\u3002<\/li>\n\n\n\n<li>\u7ebf\u7a0b\u6c60\u901a\u8fc7\u590d\u7528\u5df2\u6709\u7ebf\u7a0b\uff0c\u907f\u514d\u4e86\u9891\u7e41\u521b\u5efa\u548c\u9500\u6bc1\uff0c\u663e\u8457\u63d0\u5347\u6267\u884c\u6548\u7387\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u66f4\u597d\u7684\u8d44\u6e90\u7ba1\u7406\u548c\u63a7\u5236<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u53ef\u4ee5\u901a\u8fc7\u7ebf\u7a0b\u6c60\u9650\u5236\u5e76\u53d1\u7ebf\u7a0b\u7684\u6570\u91cf\uff0c\u9632\u6b62\u7cfb\u7edf\u56e0\u521b\u5efa\u8fc7\u591a\u7ebf\u7a0b\u800c\u5bfc\u81f4\u5185\u5b58\u6ea2\u51fa\u6216\u7cfb\u7edf\u5d29\u6e83\u3002<\/li>\n\n\n\n<li>\u6bd4\u5982\uff1a\u901a\u8fc7\u56fa\u5b9a\u5927\u5c0f\u7684\u7ebf\u7a0b\u6c60\u9650\u5236\u6700\u5927\u5e76\u53d1\u6570\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u4efb\u52a1\u8c03\u5ea6\u66f4\u52a0\u7075\u6d3b<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7ebf\u7a0b\u6c60\u4e00\u822c\u652f\u6301\u4efb\u52a1\u961f\u5217\uff08\u5982\u963b\u585e\u961f\u5217\uff09\uff0c\u53ef\u4ee5\u5c06\u4efb\u52a1\u6392\u961f\u7b49\u5f85\u6267\u884c\uff0c\u5b9e\u73b0\u4efb\u52a1\u7684\u6709\u5e8f\u8c03\u5ea6\u3002<\/li>\n\n\n\n<li>\u53ef\u652f\u6301\u5b9a\u65f6\u4efb\u52a1\u3001\u5468\u671f\u6027\u4efb\u52a1\u7b49\u6269\u5c55\u529f\u80fd\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u589e\u5f3a\u7cfb\u7edf\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u7ef4\u62a4\u6027<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7edf\u4e00\u7ebf\u7a0b\u7684\u521b\u5efa\u3001\u9500\u6bc1\u548c\u8c03\u5ea6\uff0c\u4ee3\u7801\u66f4\u6e05\u6670\u3001\u7ed3\u6784\u66f4\u53ef\u63a7\u3002<\/li>\n\n\n\n<li>\u5bf9\u4e8e\u51fa\u73b0\u7684\u5f02\u5e38\u7ebf\u7a0b\u6216\u4efb\u52a1\u5931\u8d25\uff0c\u53ef\u4ee5\u96c6\u4e2d\u5904\u7406\uff0c\u63d0\u9ad8\u7cfb\u7edf\u5065\u58ee\u6027\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>\u63d0\u9ad8\u54cd\u5e94\u901f\u5ea6<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u67d0\u4e9b\u4efb\u52a1\u5230\u8fbe\u65f6\uff0c\u65e0\u9700\u7b49\u5f85\u65b0\u7ebf\u7a0b\u7684\u521b\u5efa\uff0c\u7ebf\u7a0b\u6c60\u4e2d\u5df2\u6709\u7ebf\u7a0b\u53ef\u4ee5\u76f4\u63a5\u5904\u7406\uff0c\u63d0\u9ad8\u54cd\u5e94\u901f\u5ea6\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">6. <strong>\u4fbf\u4e8e\u8fdb\u884c\u76d1\u63a7\u548c\u8c03\u4f18<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5927\u591a\u6570\u7ebf\u7a0b\u6c60\u5b9e\u73b0\u63d0\u4f9b\u4e86\u4efb\u52a1\u961f\u5217\u957f\u5ea6\u3001\u7ebf\u7a0b\u5b58\u6d3b\u6570\u3001\u6267\u884c\u4efb\u52a1\u6570\u7b49\u6307\u6807\uff0c\u4fbf\u4e8e\u76d1\u63a7\u4e0e\u5206\u6790\u3002<\/li>\n\n\n\n<li>\u53ef\u4ee5\u52a8\u6001\u8c03\u6574\u7ebf\u7a0b\u6c60\u5927\u5c0f\uff0c\u4ee5\u9002\u5e94\u4e0d\u540c\u8d1f\u8f7d\u3002<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>\u4f7f\u7528 C++11 \u5b9e\u73b0\u7684\u7b80\u5355\u7ebf\u7a0b\u6c60\u7c7b <code>ThreadPool<\/code>\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream>\n#include &lt;vector>\n#include &lt;queue>\n#include &lt;thread>\n#include &lt;mutex>\n#include &lt;condition_variable>\n#include &lt;future>\n#include &lt;functional>\n#include &lt;atomic>\n\nclass ThreadPool {\npublic:\n    ThreadPool(size_t threads);\n    ~ThreadPool();\n\n    \/\/ \u63d0\u4ea4\u4efb\u52a1\uff0c\u8fd4\u56de future\n    template&lt;class F, class... Args>\n    auto enqueue(F&amp;&amp; f, Args&amp;&amp;... args)\n        -> std::future&lt;typename std::invoke_result&lt;F, Args...>::type>;\n\nprivate:\n    std::vector&lt;std::thread> workers;\n    std::queue&lt;std::function&lt;void()>> tasks;\n\n    std::mutex queue_mutex;\n    std::condition_variable condition;\n    std::atomic&lt;bool> stop;\n};\n\n\/\/ \u6784\u9020\u51fd\u6570\nThreadPool::ThreadPool(size_t threads) : stop(false) {\n    for (size_t i = 0; i &lt; threads; ++i) {\n        workers.emplace_back(&#91;this] {\n            while (!stop) {\n                std::function&lt;void()> task;\n                {\n                    std::unique_lock&lt;std::mutex> lock(this->queue_mutex);\n                    this->condition.wait(lock, &#91;this] {\n                        return this->stop || !this->tasks.empty();\n                    });\n                    if (this->stop &amp;&amp; this->tasks.empty())\n                        return;\n                    task = std::move(this->tasks.front());\n                    this->tasks.pop();\n                }\n                task();\n            }\n        });\n    }\n}\n\n\/\/ \u63d0\u4ea4\u4efb\u52a1\ntemplate&lt;class F, class... Args>\nauto ThreadPool::enqueue(F&amp;&amp; f, Args&amp;&amp;... args)\n    -> std::future&lt;typename std::invoke_result&lt;F, Args...>::type>\n{\n    using return_type = typename std::invoke_result&lt;F, Args...>::type;\n\n    auto task = std::make_shared&lt;std::packaged_task&lt;return_type()>>(\n        std::bind(std::forward&lt;F>(f), std::forward&lt;Args>(args)...)\n    );\n\n    std::future&lt;return_type> res = task->get_future();\n    {\n        std::unique_lock&lt;std::mutex> lock(queue_mutex);\n        if (stop)\n            throw std::runtime_error(\"enqueue on stopped ThreadPool\");\n\n        tasks.emplace(&#91;task]() { (*task)(); });\n    }\n    condition.notify_one();\n    return res;\n}\n\n\/\/ \u6790\u6784\u51fd\u6570\nThreadPool::~ThreadPool() {\n    stop = true;\n    condition.notify_all();\n    for (std::thread &amp;worker : workers)\n        worker.join();\n}\n<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>\u4f7f\u7528\u4f8b\u5b50\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>#include &lt;chrono>\n\nint main() {\n    ThreadPool pool(4); \/\/ \u521b\u5efa4\u4e2a\u7ebf\u7a0b\u7684\u7ebf\u7a0b\u6c60\n\n    std::vector&lt;std::future&lt;int>> results;\n\n    for (int i = 0; i &lt; 8; ++i) {\n        results.emplace_back(\n            pool.enqueue(&#91;i] {\n                std::this_thread::sleep_for(std::chrono::milliseconds(100));\n                std::cout &lt;&lt; \"Task \" &lt;&lt; i &lt;&lt; \" done.\\n\";\n                return i * i;\n            })\n        );\n    }\n\n    for (auto&amp;&amp; result : results)\n        std::cout &lt;&lt; \"Result: \" &lt;&lt; result.get() &lt;&lt; '\\n';\n\n    return 0;\n}<\/strong><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7ebf\u7a0b\u6c60\uff08Thread Pool\uff09\u662f\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\u7684\u4e00\u79cd\u91cd\u8981\u6280\u672f\uff0c\u5b83\u901a\u8fc7\u91cd\u590d\u5229\u7528\u4e00\u7ec4\u5df2\u521b\u5efa\u7684\u7ebf\u7a0b\u6765\u6267\u884c\u591a\u4e2a\u4efb\u52a1\uff0c\u907f &#8230; <a title=\"\u7ebf\u7a0b\u6c60\" class=\"read-more\" href=\"https:\/\/look123.top\/index.php\/2025\/05\/27\/%e7%ba%bf%e7%a8%8b%e6%b1%a0\/\" aria-label=\"\u9605\u8bfb \u7ebf\u7a0b\u6c60\">\u9605\u8bfb\u66f4\u591a<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-164","post","type-post","status-publish","format-standard","hentry","category-cpp_qt"],"_links":{"self":[{"href":"https:\/\/look123.top\/index.php\/wp-json\/wp\/v2\/posts\/164","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/look123.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/look123.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/look123.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/look123.top\/index.php\/wp-json\/wp\/v2\/comments?post=164"}],"version-history":[{"count":5,"href":"https:\/\/look123.top\/index.php\/wp-json\/wp\/v2\/posts\/164\/revisions"}],"predecessor-version":[{"id":170,"href":"https:\/\/look123.top\/index.php\/wp-json\/wp\/v2\/posts\/164\/revisions\/170"}],"wp:attachment":[{"href":"https:\/\/look123.top\/index.php\/wp-json\/wp\/v2\/media?parent=164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/look123.top\/index.php\/wp-json\/wp\/v2\/categories?post=164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/look123.top\/index.php\/wp-json\/wp\/v2\/tags?post=164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}