{"id":110,"date":"2025-10-30T01:14:50","date_gmt":"2025-10-29T17:14:50","guid":{"rendered":"http:\/\/daoxiang.xyz\/?p=110"},"modified":"2025-10-30T01:14:50","modified_gmt":"2025-10-29T17:14:50","slug":"c%e5%b9%b6%e8%a1%8c%e7%bc%96%e7%a8%8b-%e7%ae%80%e5%8d%95%e7%ba%bf%e7%a8%8b%e6%b1%a0%e7%9a%84%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/daoxiang.xyz\/index.php\/2025\/10\/30\/c%e5%b9%b6%e8%a1%8c%e7%bc%96%e7%a8%8b-%e7%ae%80%e5%8d%95%e7%ba%bf%e7%a8%8b%e6%b1%a0%e7%9a%84%e5%ae%9e%e7%8e%b0\/","title":{"rendered":"C++\u5e76\u884c\u7f16\u7a0b\u2014\u2014\u7b80\u5355\u7ebf\u7a0b\u6c60\u7684\u5b9e\u73b0"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u6700\u8fd1\u5728\u4e3a\u4e00\u4e2a\u5c0f\u4e1c\u897f\u4f5c\u70b9\u94fa\u57ab\uff0c\u5b66\u4e86\u70b9\u5e76\u53d1\u7f16\u7a0b\uff0c\u641e\u4e86\u4e2a\u7ebf\u7a0b\u6c60\u51fa\u6765\uff0c\u5b66\u5230\u4e86\u70b9\u5c0f\u4e1c\u897f\uff0c\u6d45\u8bb0\u5f55\u4e00\u4e0b<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9879\u76ee\u5730\u5740<a href=\"https:\/\/github.com\/daoxiang0520\/threadpool\/\" target=\"_blank\"  rel=\"nofollow\" >daoxiang0520\/threadpool<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\uff08\u5176\u5b9e\u5b8c\u5168\u662f\u6284\u7684<a href=\"https:\/\/zhuanlan.zhihu.com\/p\/367309864\" target=\"_blank\"  rel=\"nofollow\" >(15 \u5c01\u79c1\u4fe1 \/ 17 \u6761\u6d88\u606f) \u57fa\u4e8eC++11\u5b9e\u73b0\u7ebf\u7a0b\u6c60 - \u77e5\u4e4e<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1.\u5e76\u53d1\u77e5\u8bc6<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">c++\u7edd\u5927\u90e8\u5206\u5e76\u53d1\u7f16\u7a0b\u5185\u5bb9\u90fd\u662fc++11\u540e\u51fa\u73b0\uff0c\u5305\u62ecthread\uff0cmutex\u7b49\u7b49\u5de5\u5177\uff0c\u6709\u4e86\u8fd9\u4e9b\u7c7b\u4e4b\u540e\uff0c\u6700\u597d\u7684\u5c31\u662f\u4ee3\u7801\u5bf9\u7cfb\u7edf\u517c\u5bb9\u6027\u5bbd\u6cdb\u8bb8\u591a\uff0c\u4e0d\u7528\u53bb\u67e5windows\u7684LPTHREAD\u548clinux pthread\u7b49\u7b49\u7528\u6cd5<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">(1)std::thread<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u5178\u578b\u7684\u7ebf\u7a0b\u7c7b\uff0c\u4e3a\u76f8\u5e94\u51fd\u6570\u521b\u9020\u4e00\u4e2a\u7ebf\u7a0b\u7c7b\u7528\u4ee5\u8fd0\u884c\u8be5\u51fd\u6570\uff0c\u5e76\u5bf9\u5176\u8fdb\u884c\u64cd\u4f5c<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">(2)std::mutex<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e92\u65a5\u9501\uff0c\u7528\u4ee5\u63a7\u5236\u8d44\u6e90\u8bbf\u95ee\u5b89\u5168\uff0c\u907f\u514d\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\u591a\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u6216\u6539\u53d8\u540c\u4e00\u8d44\u6e90\u5e26\u6765\u7684\u6df7\u4e71<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u7136\u4e92\u65a5\u9501\u6709\u5f88\u591a\u79cd\uff0c\u6bd4\u5982\u9012\u5f52\u4e92\u65a5\u9501\u7b49\u7b49<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u65e5\u5e38\u4e3b\u8981\u4f7f\u7528\u4e24\u79cdRAII\u7684\u81ea\u52a8\u7ba1\u7406\u9501\u5de5\u5177<strong>std::unique_mutex<\/strong>\u548c<strong>std::lock_guard<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5176\u4e2dunique_mutex\u53ef\u624b\u52a8\u89e3\u9501\uff0clock_guard\u4e0d\u53ef\u624b\u52a8\u89e3\u9501<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3a\u4e86\u53ea\u5728\u4e00\u5b9a\u8303\u56f4\u4e0a\u9501\uff0c\u4e00\u822c\u4f1a\u7528{}\u6765\u89c4\u5b9a\u5176\u4f5c\u7528\u8303\u56f4<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">(3)std::function<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">function\u662f\u4e00\u4e2a\u51fd\u6570\u7c7b\uff0c\u4ed6\u80fd\u5b58\u50a8\u5c01\u88c5\u4e00\u7c7b\u51fd\u6570\u3002\u8b6c\u5982std::function&lt;int(int,bool)&gt;\u80fd\u591f\u5b58\u50a8int fun(int a,bool b)\u5f62\u5f0f\u7684\u51fd\u6570\uff0c\u4e14\u53ef\u968f\u65f6\u8c03\u7528\u3002\u672c\u6765\u6211\u4ee5\u4e3a\u7528function\u52a0\u5165\u7ebf\u7a0b\u6c60\u4fbf\u53ef\u89e3\u51b3\u4e00\u5207\uff0c\u540e\u6765\u53d1\u73b0function\u53ea\u80fd\u53d6\u4e00\u7c7b\u7279\u5b9a\u7684\u51fd\u6570\uff0c\u4fbf\u6539\u7528\u53ef\u53d8\u53c2\u6570\u6a21\u677f<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">(4)std::future,std::promise,std::packaged_task<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">packaged_task\u5f88\u7c7b\u4f3c\u4e8efunction(\u5176\u5b9e\u5e76\u4e0d\u600e\u4e48\u7c7b\u4f3c)\uff0c\u4ed6\u80fd\u5c01\u88c5\u4e00\u4e2a\u5f02\u6b65\u4efb\u52a1\uff0c\u5e76\u53ef\u5b58\u50a8\u8fd4\u56de\u503c<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u800cpromise\u53ef\u7528\u4e8e\u7ebf\u7a0b\u95f4\u4f20\u9012\u503c\uff0c\u5305\u62ec\u4f20\u9012\u51fd\u6570\uff0c\u5bf9\u8c61\u7b49\u7b49\u7b49\u7b49<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">future\u5219\u7528\u4e8e\u4ece\u5f02\u6b65\u4efb\u52a1\u4e2d\u83b7\u53d6\u6240\u5171\u4eab\u6216\u8fd4\u56de\u7684\u503c,\u901a\u8fc7promise\u6216\u8005packagedtask\u4fbf\u53ef\u5728\u7ebf\u7a0b\u95f4\u4f20\u9012\u6570\u636e\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">(5)std::condition_variable<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6761\u4ef6\u53d8\u91cf\uff0c\u7528\u4e8e\u7ebf\u7a0b\u7684\u6302\u8d77\u548c\u5524\u9192\u3002\u5f53\u7ebf\u7a0b\u95f2\u7f6e\u65f6\uff0c\u53ef\u91ca\u653e\u4e92\u65a5\u9501\u5e76\u6302\u8d77\u7ebf\u7a0b\uff0c\u76f4\u5230\u6ee1\u8db3\u6761\u4ef6\uff0c\u5524\u9192\u7ebf\u7a0b\u6267\u884c\u4efb\u52a1\uff0c\u6b64\u65f6mutex\u91cd\u65b0\u4e0a\u9501\u3002\u6761\u4ef6\u53d8\u91cf\u4f7f\u7528\u65f6\u4e0e\u4e92\u65a5\u9501\u7ed1\u5b9a\uff0c\u6765\u63a7\u5236\u8d44\u6e90\u7684\u9ad8\u6548\u8bbf\u95ee\u548c\u6709\u5e8f\u4fee\u6b63<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2.\u7ebf\u7a0b\u6c60\u539f\u7406\u4e0e\u5b9e\u73b0<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">(1)\u539f\u7406<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f5c\u4e3a\u4e00\u4e2a\u6c60\u5316\u7ed3\u6784\uff0c\u7ebf\u7a0b\u6c60\u540c\u6837\u662f\u4e00\u6b21\u6027\u7533\u8bf7\u4e00\u5b9a\u6570\u91cf\u7684\u7ebf\u7a0b\u5b58\u50a8\u8d77\u6765\uff0c\u7b49\u5230\u6709\u9700\u8981\u7684\u65f6\u5019\u518d\u6267\u884c\u4efb\u52a1\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u63d0\u4ea4\u4efb\u52a1\u65f6\uff0c\u5c06\u4efb\u52a1\u5c01\u88c5\u540e\u8c03\u7528\u7a7a\u95f2\u7ebf\u7a0b\u6267\u884c<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u542c\u7740\u5f88\u7b80\u5355\u5427<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u786e\u5b9e\u5982\u6b64\uff0c\u7ebf\u7a0b\u6c60\u7684\u7801\u91cf\u8fdc\u8fdc\u4f4e\u4e8e\u5185\u5b58\u6c60\uff0c\u4f46\u7ec6\u8282\u7ec6\u5316\u4e0a\u6bd4\u8f83\u590d\u6742\uff0c\u5c24\u5176\u662f\u52fe\u69fd\u7684c++11\u4e71\u4e03\u516b\u7cdf\u7684\u8bed\u6cd5<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0d\u8fc7\u4ecd\u7136\u4e00\u6b65\u4e00\u6b65\u6765<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">(2)\u5b9e\u73b0<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">A.\u5b89\u5168\u961f\u5217\uff0cMulityQueue\u7c7b<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u540d\u5b57\u968f\u4fbf\u8d77\u7684\uff0c\u65e0\u6240\u8c13<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u51fa\u4e8e\u591a\u7ebf\u7a0b\u8bbf\u95ee\u7684\u9700\u8981\uff0c\u6211\u4eec\u9700\u8981\u4e00\u4e2a\u6709\u4e92\u65a5\u9501\u6765\u7ef4\u62a4\u961f\u5217\u8d44\u6e90\uff0c\u53ea\u9700\u8981\u5728\u6bcf\u4e2a\u64cd\u4f5c\u524d\u7ed9STL\u57fa\u672c\u961f\u5217\u4e0a\u9501\u5373\u53ef<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">template &lt;typename T&gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">class MulityQueue{<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; public:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; MulityQueue()=default;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; ~MulityQueue()=default;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; bool pop(){<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; std::unique_lock&lt;std::mutex&gt; locker(mtx);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(q.empty())return 0;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; q.pop();<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 1;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; }<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; void add(const T&amp; pusher){<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; std::unique_lock&lt;std::mutex&gt; locker(mtx);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; q.emplace(pusher);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; }<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; const T&amp; top(){<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; std::unique_lock&lt;std::mutex&gt; locker(mtx);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return q.front();<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; }<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; bool empty(){<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; std::unique_lock&lt;std::mutex&gt; locker(mtx);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return q.empty();<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; }<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; int size(){<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; std::unique_lock&lt;std::mutex&gt; locker(mtx);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return q.size();<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; }<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; private:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; std::mutex mtx;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; std::queue&lt;T&gt; q;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">};<\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">\u7531\u4e8eunique_lock\u7684RAII\u7279\u6027\uff0c\u76f4\u63a5\u5728\u51fd\u6570\u4f53\u5185\u58f0\u660e\u5373\u4e0a\u9501<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">B.\u7ebf\u7a0b\u6c60\uff0cThreadPool\u7c7b<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u9996\u5148\u8003\u8651\u6211\u4eec\u9700\u8981\u4ec0\u4e48<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u57fa\u672c\u7684\uff0c\u7ebf\u7a0b\u6570\u7ec4\u5b58\u50a8\u76f8\u5e94\u7684\u7ebf\u7a0b\uff0c\u4ee5\u53ca\u5f85\u6267\u884c\u7684\u4efb\u52a1\u961f\u5217<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u63a5\u7740\u662f\u4e92\u65a5\u91cf\uff0c\u9501\u5b9a\u4e34\u754c\u533a\u8d44\u6e90\uff1b\u6761\u4ef6\u53d8\u91cf\uff0c\u6302\u8d77\u65e0\u7528\u7ebf\u7a0b\u4ee5\u53ca\u5524\u9192\u7ebf\u7a0b\u4f7f\u7528\uff0c\u4ee5\u53ca\u76d1\u6d4b\u8fd0\u884c\u72b6\u6001\u7684bool\u53d8\u91cf<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9664\u6b64\u4e4b\u5916\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u7ebf\u7a0b\u6267\u884c\u7684\u4e3b\u51fd\u6570\uff0c\u4ee5\u6301\u7eed\u7ebf\u7a0b\u7684\u8fd0\u884c<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u800c\u5bf9\u5916\u7684\u63a5\u53e3\uff0c\u9664\u4e86\u6784\u9020\u548c\u6790\u6784\uff0c\u9700\u8981\u4e00\u4e2a\u505c\u6b62\u51fd\u6570\u548c\u6700\u91cd\u8981\u7684\u4efb\u52a1\u63d0\u4ea4\u51fd\u6570<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5230\u6b64\u4e00\u4e2a\u7b80\u5355\u7684\u7ebf\u7a0b\u6c60\u7ed3\u6784\u5c31\u5b8c\u6210\u4e86<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">class ThreadPool{<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; public:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; ThreadPool(int size=10);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; ~ThreadPool(){stop();};<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; template&lt;typename T,typename... Args&gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; auto submit(T &amp;&amp;f,Args &amp;&amp;...args)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -&gt;std::future&lt;std::invoke_result_t&lt;T,Args...&gt;&gt;;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; void stop();<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; private:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; std::mutex mtx;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; bool is_running;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; MulityQueue&lt;std::function&lt;void()&gt; &gt; Tasks;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; std::vector&lt;std::thread&gt; Threads;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; void KeepGetting();<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; std::condition_variable conv;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">};<\/p>\n<\/blockquote>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">\u63a5\u4e0b\u6765\u4e00\u4e2a\u4e2a\u5206\u6790<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>void KeepGetting()<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9996\u5148\u4e3a\u4e86\u5b9e\u73b0\u7ebf\u7a0b\u5f85\u673a\uff0c\u4e0d\u80fd\u8ba9\u7ebf\u7a0b\u7ebf\u6027\u6267\u884c\u540e\u76f4\u63a5return\uff0c\u5426\u5219\u7ebf\u7a0b\u76f4\u63a5\u5173\u95ed<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u56e0\u6b64\u6211\u4eec\u5728\u7ebf\u7a0b\u4e2d\u6301\u7eed\u5faa\u73afwhile(1)\u5c1d\u8bd5\u5411\u4efb\u52a1\u961f\u5217\u83b7\u53d6\u4efb\u52a1\u6267\u884c<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">void ThreadPool::KeepGetting(){<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; while(1){<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; std::function&lt;void()&gt; f;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; std::unique_lock&lt;std::mutex&gt; lock(mtx);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; conv.wait(lock,[this]{return !is_running||!Tasks.empty();});<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(!is_running&amp;&amp;Tasks.empty())return;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f=Tasks.top();<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Tasks.pop();<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; }<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; f();<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; }<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">\u597d\u7684\uff0c\u8fd9\u91cc\u6d89\u53ca\u4e00\u4e9b\u5510\u8bd7\u8bed\u6cd5<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9996\u5148\u8fd9\u91cc\u591a\u51fa\u6765\u7684\u5927\u62ec\u53f7\u662f\u7528\u6765\u9650\u5b9a\u4e92\u65a5\u9501\u7684\u4f5c\u7528\u57df\uff0c\u9650\u5b9a\u9501\u5728\u8fd9\u4e2a\u57df\u5185\u751f\u6548\uff0c\u57df\u5916\u4e0d\u751f\u6548\u3002\u4e0d\u7528\u8003\u8651\u4e3a\u4ec0\u4e48mutex\u4e0d\u4e0a\u9501\uff0cunique_lock\u4f1a\u5e2e\u4f60\u5b89\u6392\u4e00\u5207\u7684\uff0c\u9664\u975e\u771f\u7684\u6709\u7279\u522b\u9700\u6c42\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u63a5\u7740\u662f\u6761\u4ef6\u53d8\u91cf\u7684wait\u6210\u5458<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>template&lt; class Predicate &gt; void wait( std::unique_lock&lt;std::mutex&gt;&amp; lock, Predicate pred );<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u51fd\u6570\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3a\u4e92\u65a5\u9501\uff0c\u540e\u4e00\u4e2a\u662f\u8c13\u8bcd\u6210\u5206<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u963b\u585e\u7ebf\u7a0b\uff0c\u76f4\u5230\u6761\u4ef6\u53d8\u91cf\u5524\u9192\uff08notify_one\u6216notify_all\uff09,\u518d\u7ee7\u7eed\u8fd0\u884c\uff1b\u800c\u4e14\u5524\u9192\u540e\u82e5\u8c13\u8bcd\u6210\u5206(pred)\u4ecd\u4e3afalse\uff0c\u5219\u7ee7\u7eed\u963b\u585e\u7ebf\u7a0b<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u518d\u60f3\u4e00\u4e0b\u6211\u4eec\u9700\u8981\u505a\u4ec0\u4e48\u3002\u6211\u4eec\u5f53\u7136\u53ef\u4ee5while(1)\u68c0\u6d4b\u4efb\u52a1\u961f\u5217\uff0c\u82e5\u961f\u5217\u975e\u7a7a\u5219\u53d6\u51fa\u961f\u9996\u6267\u884c\uff0c\u4e92\u65a5\u9501\u53ef\u4ee5\u4fdd\u8bc1\u64cd\u4f5c\u7684\u5b89\u5168<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f46\u662f\u8fd9\u6837\u7684\u505a\u6cd5\u672a\u514d\u592a\u8fc7\u7e41\u7410\uff0c\u8bf7\u6c42\u8fc7\u4e8e\u9891\u7e41\uff0c\u964d\u4f4e\u6548\u7387<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e8e\u662f\u6211\u4eec\u5f15\u5165\u6761\u4ef6\u53d8\u91cf\u6765\u63a7\u5236\u7ebf\u7a0b\u7684\u963b\u585e\uff0c\u5e76\u4e14\u68c0\u6d4b\u7ebf\u7a0b\u6c60\u662f\u5426\u8fd0\u884c\u4ee5\u53ca\u4efb\u52a1\u961f\u5217\u662f\u5426\u7a7a\u4f5c\u4e3a\u8c13\u8bcd<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\uff08\u5f53\u7136\u8c13\u8bcd\u90e8\u5206\u4e5f\u53ef\u4ee5\u7528<strong>while(!pred)wait(lock);<\/strong>\u6765\u4ee3\u66ff\uff0c\u6548\u679c\u76f8\u540c<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f46\u662f\u8fd9\u91cc\u7559\u4e2a\u5c0f\u60ac\u5ff5\uff0c\u4e3a\u4ec0\u4e48f\u4e00\u5b9a\u662fvoid()\u7c7b\u578b\u7684\u5462\uff1f\u63d0\u4ea4\u4e0a\u53bb\u7684\u51fd\u6570\u4e0d\u4e00\u5b9a\u90fd\u662f\u8fd9\u4e2a\u7c7b\u578b<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f46\u5982\u679c\u7c7b\u578b\u4e0d\u540c\uff0c\u5982\u4f55\u653e\u5165\u961f\u5217\u8c03\u7528\u5462\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6240\u4ee5\u4e3a\u4e86\u7edf\u4e00\uff0c\u6211\u4eec\u4f1a\u5728\u63a5\u4e0b\u6765\u7684submit\u51fd\u6570\u4e2d\u5c06\u4efb\u52a1\u5c01\u88c5\u6210\u4e00\u4e2avoid()\u51fd\u6570\uff0c\u4e14\u80fd\u987a\u5229\u8c03\u7528<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">template&lt;typename T,typename... Args>auto submit(T &amp;&amp;f,Args &amp;&amp;...args)->std::future&lt;std::invoke_result_t&lt;T,Args...>>;<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u662f\u4e0d\u662f\u770b\u5f97\u602a\u602a\u7684\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5148\u4ece\u58f0\u660e\u8bf4\u8d77<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1.\u73b0\u5728\u6211\u4eec\u60f3\u8981\u4f20\u5165\u51fd\u6570\u4ee5\u53ca\u51fd\u6570\u7684\u53c2\u6570\uff0c\u7136\u800c\u51fd\u6570\u672c\u8eab\u7684\u7c7b\u578b\u4ee5\u53ca\u6709\u591a\u5c11\u53c2\u6570\u5e76\u4e0d\u662f\u80fd\u591f\u786e\u5b9a\u7684\uff0c\u56e0\u6b64\u6211\u4eec\u4f7f\u7528<strong>\u53ef\u53d8\u53c2\u6570\u6a21\u677f<\/strong>\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0cc++11\u4e2d\u7684typename...\u53ef\u4ee5\u89c4\u5b9a\u4e00\u4e2a\u53c2\u6570\u6570\u91cf\u53ef\u53d8\u7684\u7c7b\u6a21\u677f\uff0cstd::thread\u4fbf\u7528\u4e86\u8fd9\u79cd\u6280\u672f\u4f20\u53c2<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2.submit\u7684\u53c2\u6570:T &amp;&amp;f\u4e0eArgs &amp;&amp;...args<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u77e5\u9053\uff0c&amp;\u8868\u793a\u5f15\u7528\uff0c\u90a3\u4e48&amp;&amp;\u662f\u4ec0\u4e48\u5462\uff1f\u6211\u4eec\u597d\u50cf\u53ea\u5728\u6761\u4ef6\u8bed\u53e5\u4e2d\u770b\u5230\u8fc7\u8fd9\u4e2a\u201c\u4e0e\u201d\u8fd0\u7b97\u7b26\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u91cc&amp;&amp;\u7528\u6cd5\u8868\u793a\u53f3\u503c\u5f15\u7528\uff0c\u4f20\u7edf\u7684&amp;\u5219\u662f\u5de6\u503c\u5f15\u7528\u3002\u53f3\u503c\u5f15\u7528\u80fd\u591f\u907f\u514d\u989d\u5916\u7684\u7a7a\u95f4\u5f00\u9500\u4e0e\u53ef\u80fd\u5bfc\u81f4\u7684\u6570\u636e\u6539\u53d8<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3.auto func()->(type)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">c++11\u7684\u7279\u6027<strong>\u5c3e\u8fd4\u56de\u7c7b\u578b\u63a8\u5bfc<\/strong>\uff0c\u5c06\u8fd4\u56de\u7c7b\u578b\u540e\u7f6e\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5e73\u65f6\u7684\u51fd\u6570\u58f0\u660e\u662f\u5c06\u51fd\u6570\u7c7b\u578b\u76f4\u63a5\u58f0\u660e\uff0c\u7136\u800c\u6211\u4eec\u5e76\u4e0d\u77e5\u9053\u63a5\u53d7\u7684\u51fd\u6570\u8fd4\u56de\u7c7b\u578b\u4f1a\u662f\u591a\u5c11(\u6a21\u677f\u4e2d\u7684T\u7c7b\u578b\u6b64\u65f6\u662f\u51fd\u6570\u6307\u9488)\uff0c\u5229\u7528\u8fd9\u4e2a\u80fd\u5c06\u51fd\u6570\u8fd4\u56de\u7c7b\u578b\u540e\u7f6e\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">4.std::invoke_result_t&lt;T,Args...><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">std::future\u7684\u7528\u6cd5\u524d\u9762\u5df2\u7ecf\u9648\u8ff0\u5b8c\u6bd5\uff0c\u5373\u80fd\u591f\u83b7\u53d6\u5f02\u6b65\u4efb\u52a1\u4e2d\u8fd4\u56de\u7684\u6570\u636e\uff0c\u4f46\u662f\u4ecd\u7136\u662f\u524d\u9762\u7684\u95ee\u9898\uff0c\u5373\u8fd4\u56de\u7c7b\u578b\u600e\u4e48\u83b7\u5f97\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5148\u4ecb\u7ecd\u4e00\u4e0bc++11\u4e2d\u6709\u4e00\u4e2a\u5f88\u65b9\u4fbf\u7684\u5de5\u5177:decltype\u3002\u8fd9\u4e2a\u5173\u952e\u5b57\u4e0eauto\u5f88\u76f8\u4f3c\uff0c\u90fd\u80fd\u591f\u8fdb\u884c\u7f16\u8bd1\u65f6\u7c7b\u578b\u63a8\u5bfc\uff0c\u4e0d\u540c\u7684\u662f\uff0cdecltype\u652f\u6301\u63a8\u5bfc\u8868\u8fbe\u5f0f\u7684\u503c\uff0c\u5373decltype(\u8868\u8fbe\u5f0f)\uff0c\u800c\u4e14\uff0c\u66f4\u52a0\u4e0d\u540c\u7684\u4e00\u70b9\u662f\uff0cdecltype\u63a8\u5bfc\u51fa\u7684\u80fd\u591f\u4f5c\u4e3a\u7c7b\u578b\u58f0\u660e\uff0c\u800c\u4e0d\u662f\u63a8\u5230\u521d\u59cb\u5316\u8868\u8fbe\u5f0f\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u7528\u4f8b\u5dee\u522b\uff0c\u4e8c\u8005\u5dee\u522b\u5728\u4e8ek\u7684\u521d\u59cb\u503c<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">auto k=4; &lt;->decltype(4) k<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6240\u4ee5\u4e8b\u5b9e\u4e0a\u6211\u4eec\u53ef\u4ee5\u5199decltype(T(args...))\uff0c\u4f46\u662f\u4f1a\u6709\u4e00\u4e9b\u5c0f\u95ee\u9898<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"716\" height=\"244\"   class=\"lazyload\" data-src=\"http:\/\/daoxiang.xyz\/wp-content\/uploads\/2025\/10\/image.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  alt=\"\" class=\"wp-image-125\" style=\"width:840px;height:auto\" srcset=\"https:\/\/daoxiang.xyz\/wp-content\/uploads\/2025\/10\/image.png 716w, https:\/\/daoxiang.xyz\/wp-content\/uploads\/2025\/10\/image-300x102.png 300w\" sizes=\"auto, (max-width: 716px) 100vw, 716px\" \/><\/figure >\n<noscript><img loading=\"lazy\" decoding=\"async\" width=\"716\" height=\"244\" src=\"http:\/\/daoxiang.xyz\/wp-content\/uploads\/2025\/10\/image.png\" alt=\"\" class=\"wp-image-125\" style=\"width:840px;height:auto\" srcset=\"https:\/\/daoxiang.xyz\/wp-content\/uploads\/2025\/10\/image.png 716w, https:\/\/daoxiang.xyz\/wp-content\/uploads\/2025\/10\/image-300x102.png 300w\" sizes=\"auto, (max-width: 716px) 100vw, 716px\" \/><\/figure><\/noscript>\n\n\n\n<p class=\"wp-block-paragraph\">AI\u7ed9\u51fa\u7684\u8bf4\u6cd5<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7b80\u5355\u6765\u8bf4\uff0cstd::invoke_result_t\u8fd4\u56de\u503c\u66f4\u7cbe\u786e\u4e00\u4e9b\uff0c\u4e14\u80fd\u591f\u907f\u514d\u51fd\u6570\u7b7e\u540d\uff0c\u51fd\u6570\u6307\u9488\u5f15\u7528\u4e0e\u6210\u5458\u51fd\u6570\u7b49\u95ee\u9898<\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e8e\u662f\u6211\u4eec\u4f7f\u7528c++17\u7684std::invoke_result_t\u6765\u63a8\u5bfc\u8fd4\u56de\u7c7b\u578b\uff08\u8c8c\u4f3c\u8fd8\u6709\u4e00\u4e2a\u5de5\u5177std::declval\u53ef\u7528\uff0c\u53ef\u4ee5\u4e86\u89e3\u4e00\u4e0b\uff09\u3002\u8be5\u7c7b\u578b\u662f\u7531c++11\u7684result_t(\u7531\u4e8e\u5b89\u5168\u95ee\u9898\uff0cc++14\u88ab\u5e9f\u6b62\uff0cc++17\u5f7b\u5e95\u7981\u7528\u6539\u4e3ainvoke_result_t)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u81f3\u6b64\uff0c\u58f0\u660e\u90e8\u5206\u7ed3\u675f\uff0c\u6211\u4eec\u505a\u5230\u80fd\u8fd4\u56de\u6b63\u786e\u503c\u4ee5\u53ca\u6b63\u786e\u4f20\u53c2\uff0c\u4e0b\u9762\u662f\u5b9e\u73b0\u90e8\u5206<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">template&lt;typename T,typename... Args&gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">auto ThreadPool::submit(T &amp;&amp;f,Args &amp;&amp;...args)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; -&gt;std::future&lt;std::invoke_result_t&lt;T,Args...&gt;&gt;{<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; std::function&lt;std::invoke_result_t&lt;T,Args...&gt;()&gt; F=std::bind(std::forward&lt;T&gt;(f),std::forward&lt;Args&gt;(args)...);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; auto task_=std::make_shared&lt;std::packaged_task&lt;std::invoke_result_t&lt;T,Args...&gt;()&gt;&gt;(F);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; std::function&lt;void()&gt; wrapped_task=<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; [task_]{<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; (*task_)();<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; };<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; std::unique_lock&lt;std::mutex&gt; lk(mtx);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; if(!is_running)throw std::runtime_error(\"ThreadPool Has Stopped\");<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; Tasks.add(wrapped_task);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; }conv.notify_one();<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; return task_-&gt;get_future();<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">\u8be5\u8bf4\u4e0d\u8bf4\uff0c\u8fd9\u4e9b\u8bed\u6cd5\u5b9e\u5728\u662f\u592a\u590d\u6742\u4e86\u2026\u2026\u4e00\u70b9\u70b9\u8bf4\u5427<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6574\u4f53\u601d\u8def\uff1a\u4e00\u70b9\u70b9\u5c01\u88c5\u4efb\u52a1\u51fd\u6570\u4e3a\u7edf\u4e00\u7ed3\u6784\u5b89\u5168\u653e\u5165\u961f\u5217\uff0c\u547c\u53ebthread\u8fd0\u884c<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1.std::function&lt;std::invoke_result_t&lt;T,Args...>()> F=std::bind(std::forward&lt;T>(f),std::forward&lt;Args>(args)...);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u597d\u7684\uff0cinvoke_result_t\u6211\u4eec\u5df2\u7ecf\u8bf4\u660e\u8fc7\u4e86\uff0cfunction\u5bfc\u5f15\u65f6\u4e5f\u8bf4\u8fc7\u4e86\uff0c\u95ee\u9898\u662f\u8fd9\u4e2a\u7c7b\u578b\u662f\u4e2a\u65e0\u53c2\u51fd\u6570\uff0c\u53ef\u662ff\u7684\u53c2\u6570\u4e0d\u56fa\u5b9a\uff0c\u600e\u4e48\u505a\u5230\u7684\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">std::bind<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7b80\u800c\u8a00\u4e4b\uff0cstd::bind\u80fd\u628a\u4e00\u4e2a\u51fd\u6570\u7684\u53c2\u6570\u9884\u5148\u7ed1\u5b9a\u597d\uff0c\u8fd9\u6837\u51fd\u6570\u8fd0\u884c\u65f6\u53ea\u9700\u8981\u5269\u4e0b\u7684\u53c2\u6570\u5c31\u53ef\u4ee5\uff0c\u53ef\u4ee5\u7406\u89e3\u4e3a\u9884\u8bbe\u503c\uff0cbind\u80fd\u8fd4\u56de\u4e00\u4e2a\u65b0\u51fd\u6570\uff0c\u9664\u4e86\u53c2\u6570\u6570\u91cf\u4ee5\u5916\u529f\u80fd\u4e0e\u539f\u51fd\u6570\u4e00\u81f4<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6709\u70b9\u62bd\u8c61\uff0c\u4e3e\u4e2a\u4f8b\u5b50\u5427<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">int func(int a,int b){...}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">int main(){<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">int c=1,d;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">std::cin>>d;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">std::function&lt;int(int)> newfun=std::bind(func,c);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">std::cout&lt;&lt;newfun(d);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n<\/blockquote>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u4e0a\uff0c\u5c06func\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e0e\u4e00\u4e2a\u53d8\u91cf\u7ed1\u5b9a\uff0c\u65b0\u51fd\u6570\u5c31\u53ea\u9700\u8981\u4e00\u4e2a\u4f20\u5165\u53c2\u6570\uff0c\u8fd9\u79cd\u505a\u6cd5\u5bf9\u6cdb\u7528\u6027\u5f3a\u7684\u51fd\u6570\u5728\u4e13\u7528\u533a\u57df\u7684\u4f7f\u7528\u5f88\u65b9\u4fbf<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u56de\u8fc7\u5934\u6765\uff0c\u6211\u4eec\u5df2\u7ecf\u5c06f\u5229\u7528std::bind\u5c01\u88c5\u4e3a\u4e00\u4e2a\u96f6\u53c2\u51fd\u6570<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">auto task_=std::make_shared&lt;std::packaged_task&lt;std::invoke_result_t&lt;T,Args...>()>>(F);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u63a5\u4e0b\u6765\uff0c\u7528\u667a\u80fd\u6307\u9488\u5c01\u88c5\u4efb\u52a1<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u4f55\u5c01\u88c5\uff1f\u6211\u4eec\u660e\u663e\u611f\u89c9\u5230\u5305\u62ec\u4e0b\u9762\u8fd9\u4e00\u6b65\u8fdb\u4e00\u6b65\u7684\u5c01\u88c5\u662f\u6ca1\u4ec0\u4e48\u4f5c\u7528\u7684\uff0c\u7eaf\u7cb9\u662f\u53e0\u6d0b\u8471\u5c42\u5c42\u5c01\u5305<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5176\u5b9e\u6211\u4eec\u7684\u60f3\u6cd5\u662f\u5bf9\u7684\uff0c\u786e\u5b9e\u6ca1\u4ec0\u4e48\u7528\uff0c\u5355\u7eaf\u589e\u52a0\u4ee3\u7801\u91cf<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f46\u662f\u8fd8\u8bb0\u5f97\u6211\u4eec\u7684\u8fd4\u56de\u503c\u5417\uff1f\u867d\u7136\u80fd\u81ea\u52a8\u63a8\u5bfc\u7c7b\u578b\uff0c\u4f46\u6211\u4eec\u8fd4\u56de\u7684\u4ecd\u662fstd::future,\u4e5f\u56e0\u6b64\u4e0d\u53ef\u907f\u514d\u5730\u9700\u8981std::packaged_task\u8fd9\u4e00\u5f02\u6b65\u4efb\u52a1\u7c7b\u578b\u6765\u901a\u8fc7get_future()\u8fd4\u56de<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f46\u662f\uff0cstd::packaged_task\u4e0d\u662f\u4e00\u4e2a\u53ef\u6267\u884c\u7684\u51fd\u6570\uff0c\u56e0\u6b64\u6211\u4eec\u8fd8\u9700\u8981\u5c01\u88c5\u5728\u51fd\u6570\u4f53\u91cc\uff0c\u4e8e\u662f\u7528std::make_shared\u521b\u5efa\u6307\u5411\u5df2\u5c01\u88c5\u7684\u51fd\u6570F\u7684\u51fd\u6570\u6307\u9488<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u7136\uff0c\u4e3a\u4ec0\u4e48\u9700\u8981\u8fd9\u4e2a\u6307\u9488\u5462\uff1f\u8fd8\u8bb0\u5f97\u6211\u4eec\u521a\u5f00\u59cb\u8bf4\u7684\u5417\uff0c\u8981\u5c06\u51fd\u6570\u7c7b\u578b\u7edf\u4e00\u4e3avoid()\u7c7b\u578b\uff0c\u624d\u80fd\u65b9\u4fbf\u8c03\u7528\u548c\u5b58\u50a8\uff0c\u4e8e\u662f\u6709<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; std::function&lt;void()&gt; wrapped_task=<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; [task_]{<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; (*task_)();<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; };<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">lamdba\u8868\u8fbe\u5f0f\uff0c\u5373\u533f\u540d\u51fd\u6570\uff0c\u7b80\u5355\u5730\u521b\u9020\u4e00\u4e2avoid()\u65e0\u8fd4\u56de\u503c\u51fd\u6570\uff0c\u901a\u8fc7[task_]\u6355\u6349\u51fd\u6570\u6307\u9488\u5728lamdba\u51fd\u6570\u4f53\u5185\u6267\u884c\uff0c\u8fd9\u6837\u5c31\u80fd\u5c01\u88c5\u4e3afunction&lt;void()>\u7c7b\u578b<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5230\u6b64\u6211\u4eec\u5c01\u88c5\u5b8c\u6bd5\uff0c\u63a5\u4e0b\u6765\u4fbf\u662f\u63d2\u5165\u4efb\u52a1\u961f\u5217\uff0c\u5524\u9192\u7ebf\u7a0b\u6267\u884c<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">{<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; std::unique_lock&lt;std::mutex&gt; lk(mtx);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; if(!is_running)throw std::runtime_error(\"ThreadPool Has Stopped\");<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; Tasks.add(wrapped_task);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; }conv.notify_one();<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; return task_-&gt;get_future();<\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">\u90a3\u4e2a\u770b\u8d77\u6765\u83ab\u540d\u5176\u5999\u7684\u5927\u62ec\u53f7\u662f\u7528\u6765\u9650\u5b9a\u4e92\u65a5\u9501\u8303\u56f4\uff0c\u6267\u884c\u540e\u53ca\u65f6\u91ca\u653e\u65b9\u4fbf\u7ebf\u7a0b\u4f7f\u7528<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6b64\u65f6\u5c01\u88c5\u540e\u7684wrapped_task\u4fbf\u80fd\u987a\u5229\u5730\u63d2\u5165\u4efb\u52a1\u961f\u5217\uff0c\u63d2\u5165\u961f\u5217\u540enotify_one\u5524\u9192\u4e00\u4e2a\u7ebf\u7a0b\u6765\u6267\u884c\u4efb\u52a1\uff0c\u6700\u540e\u7528get_future()\u83b7\u53d6\u8fd4\u56de\u503c<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u81f3\u6b64\uff0c\u5927\u529f\u544a\u6210\uff01<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4.\u603b\u7ed3\u4e0e\u4f7f\u7528<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528\u7684\u8bdd\uff0c\u65e2\u7136\u8bf4\u5b8c\u4e86\uff0c\u8c03\u7528\u63a5\u53e3\u5c31\u884c\u4e86<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7b97\u662f\u6162\u6162\u78e8\uff08\u6284\uff09\u8fc7\u6765\u7684\u4e00\u4e2a\u5c0f\u4e1c\u897f\u5427\uff0c\u6211\u628a\u5b83\u7528\u5230\u7684webserver\u4e0a\u63d0\u5347\u6027\u80fd<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8be5\u8bf4\u4e0d\u8bf4c++\u4e00\u4e9b\u65b0\u6807\u51c6\u90fd\u633a\u597d\u7528\u7684\uff0c\u53ef\u60dc\u8d8a\u6765\u8d8a\u6ca1\u4eba\u641e\u4e86<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u524d\u6b21\u7684memorypool\uff0c\u6211\u6ca1\u6562\u7528\uff0c\u5b89\u5168\u6027\u95ee\u9898\u6211\u6ca1\u628a\u63e1<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u597d\u4e86\uff0c\u5b8c\u7ed3\u6492\u82b1\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u5728\u4e3a\u4e00\u4e2a\u5c0f\u4e1c\u897f\u4f5c\u70b9\u94fa\u57ab\uff0c\u5b66\u4e86\u70b9\u5e76\u53d1\u7f16\u7a0b\uff0c\u641e\u4e86\u4e2a\u7ebf\u7a0b\u6c60\u51fa\u6765\uff0c\u5b66\u5230\u4e86\u70b9\u5c0f\u4e1c\u897f\uff0c\u6d45\u8bb0\u5f55\u4e00\u4e0b \u9879\u76ee\u5730\u5740daoxiang0520\/thr &#8230;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-110","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/daoxiang.xyz\/index.php\/wp-json\/wp\/v2\/posts\/110","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/daoxiang.xyz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/daoxiang.xyz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/daoxiang.xyz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/daoxiang.xyz\/index.php\/wp-json\/wp\/v2\/comments?post=110"}],"version-history":[{"count":15,"href":"https:\/\/daoxiang.xyz\/index.php\/wp-json\/wp\/v2\/posts\/110\/revisions"}],"predecessor-version":[{"id":130,"href":"https:\/\/daoxiang.xyz\/index.php\/wp-json\/wp\/v2\/posts\/110\/revisions\/130"}],"wp:attachment":[{"href":"https:\/\/daoxiang.xyz\/index.php\/wp-json\/wp\/v2\/media?parent=110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/daoxiang.xyz\/index.php\/wp-json\/wp\/v2\/categories?post=110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/daoxiang.xyz\/index.php\/wp-json\/wp\/v2\/tags?post=110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}