{"id":423,"date":"2025-12-20T15:55:52","date_gmt":"2025-12-20T07:55:52","guid":{"rendered":"https:\/\/whaledream.xyz\/?p=423"},"modified":"2025-12-20T15:55:52","modified_gmt":"2025-12-20T07:55:52","slug":"c%e7%ba%bf%e7%a8%8b%e6%b1%a0","status":"publish","type":"post","link":"https:\/\/whaledream.xyz\/index.php\/2025\/12\/20\/c%e7%ba%bf%e7%a8%8b%e6%b1%a0\/","title":{"rendered":"C++\u7ebf\u7a0b\u6c60"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u7ebf\u7a0b<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u7ebf\u7a0b\u7684\u5b9a\u4e49\u5728\u767e\u5ea6\u4e0a\u7684\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n\n\n<div class=\"para_SkYG9 summary_Iw34L MARK_MODULE\" data-tag=\"paragraph\" data-uuid=\"gnx3cuv7wm\" data-idx=\"\"><em><span class=\"text_Vh97M bold_sgAbq J-lemma-content-lemma-text\" data-text=\"true\">\u7ebf\u7a0b<\/span><span class=\"text_Vh97M J-lemma-content-lemma-text\" data-text=\"true\">\uff08\u82f1\u8bed\uff1athread\uff09\u662f<\/span><span class=\"text_Vh97M J-lemma-content-lemma-text\" data-text=\"true\"><a class=\"innerLink_ISIiu\" href=\"https:\/\/baike.baidu.com\/item\/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F\/192?fromModule=lemma_inlink\" target=\"_blank\" rel=\"noopener\" data-from-module=\"summary\">\u64cd\u4f5c\u7cfb\u7edf<\/a><\/span><span class=\"text_Vh97M J-lemma-content-lemma-text\" data-text=\"true\">\u80fd\u591f\u8fdb\u884c\u8fd0\u7b97\u8c03\u5ea6\u7684\u6700\u5c0f\u5355\u4f4d\u3002\u5b83\u88ab\u5305\u542b\u5728<\/span><span class=\"text_Vh97M J-lemma-content-lemma-text\" data-text=\"true\"><a class=\"innerLink_ISIiu\" href=\"https:\/\/baike.baidu.com\/item\/%E8%BF%9B%E7%A8%8B\/382503?fromModule=lemma_inlink\" target=\"_blank\" rel=\"noopener\" data-from-module=\"summary\">\u8fdb\u7a0b<\/a><\/span><span class=\"text_Vh97M J-lemma-content-lemma-text\" data-text=\"true\">\u4e4b\u4e2d\uff0c\u662f<\/span><span class=\"text_Vh97M J-lemma-content-lemma-text\" data-text=\"true\"><a class=\"innerLink_ISIiu\" href=\"https:\/\/baike.baidu.com\/item\/%E8%BF%9B%E7%A8%8B\/0?fromModule=lemma_inlink\" target=\"_blank\" rel=\"noopener\" data-from-module=\"summary\">\u8fdb\u7a0b<\/a><\/span><span class=\"text_Vh97M J-lemma-content-lemma-text\" data-text=\"true\">\u4e2d\u7684\u5b9e\u9645\u8fd0\u4f5c\u5355\u4f4d\u3002\u4e00\u6761\u7ebf\u7a0b\u6307\u7684\u662f<\/span><span class=\"text_Vh97M J-lemma-content-lemma-text\" data-text=\"true\"><a class=\"innerLink_ISIiu\" href=\"https:\/\/baike.baidu.com\/item\/%E8%BF%9B%E7%A8%8B\/0?fromModule=lemma_inlink\" target=\"_blank\" rel=\"noopener\" data-from-module=\"summary\">\u8fdb\u7a0b<\/a><\/span><span class=\"text_Vh97M J-lemma-content-lemma-text\" data-text=\"true\">\u4e2d\u4e00\u4e2a\u5355\u4e00\u987a\u5e8f\u7684\u63a7\u5236\u6d41\uff0c\u4e00\u4e2a\u8fdb\u7a0b\u4e2d\u53ef\u4ee5\u5e76\u53d1\u591a\u4e2a\u7ebf\u7a0b\uff0c\u6bcf\u6761\u7ebf\u7a0b\u5e76\u884c\u6267\u884c\u4e0d\u540c\u7684\u4efb\u52a1\u3002\u5728<\/span><span class=\"text_Vh97M J-lemma-content-lemma-text\" data-text=\"true\">Unix System V<\/span><span class=\"text_Vh97M J-lemma-content-lemma-text\" data-text=\"true\">\u53ca<\/span><span class=\"text_Vh97M J-lemma-content-lemma-text\" data-text=\"true\"><a class=\"innerLink_ISIiu\" href=\"https:\/\/baike.baidu.com\/item\/SunOS\/0?fromModule=lemma_inlink\" target=\"_blank\" rel=\"noopener\" data-from-module=\"summary\">SunOS<\/a><\/span><span class=\"text_Vh97M J-lemma-content-lemma-text\" data-text=\"true\">\u4e2d\u4e5f\u88ab\u79f0\u4e3a\u8f7b\u91cf\u8fdb\u7a0b\uff08lightweight processes\uff09\uff0c\u4f46\u8f7b\u91cf\u8fdb\u7a0b\u66f4\u591a\u6307\u5185\u6838\u7ebf\u7a0b\uff08kernel thread\uff09\uff0c\u800c\u628a\u7528\u6237\u7ebf\u7a0b\uff08user thread\uff09\u79f0\u4e3a\u7ebf\u7a0b\u3002<\/span><\/em><\/div>\n\n\n<p class=\"wp-block-paragraph\">\u4e00\u4e2a\u8fdb\u7a0b\u53ef\u4ee5\u7406\u89e3\u4e3a\u4e00\u4e2a\u8fd0\u884c\u8f6f\u4ef6\uff0c\u5176\u81f3\u5c11\u5305\u542b\u4e00\u4e2a\u4e3b\u7ebf\u7a0b\u3002\u5982\u679c\u5c06\u6240\u6709\u4efb\u52a1\u90fd\u653e\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u8fd0\u884c\uff0c\u90a3\u4e48\u6267\u884c\u65f6\u4f1a\u53d8\u5f97\u5f02\u5e38\u5361\u987f\u3002\u4e3a\u4e86\u51cf\u5c11\u4efb\u52a1\u7684\u5de5\u4f5c\u538b\u529b\uff0c\u5c31\u4f1a\u5f15\u5165\u7ebf\u7a0b\u6c60\u7684\u6982\u5ff5\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7ebf\u7a0b\u6c60<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ec0\u4e48\u662f\u7ebf\u7a0b\u6c60\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7ebf\u7a0b\u6c60\uff08Thread Pool\uff09\u662f\u4e00\u79cd\u5e76\u53d1\u7f16\u7a0b\u4e2d\u5e38\u7528\u7684\u6280\u672f\uff0c\u7528\u4e8e\u7ba1\u7406\u548c\u91cd\u7528\u7ebf\u7a0b\u3002\u5b83\u7531\u7ebf\u7a0b\u6c60\u7ba1\u7406\u5668\u3001\u5de5\u4f5c\u961f\u5217\u548c\u7ebf\u7a0b\u6c60\u7ebf\u7a0b\u7ec4\u6210\u3002\u7528\u6237\u53ef\u81ea\u5b9a\u4e49\u5c06\u4efb\u52a1\u52a0\u5165\u6307\u5b9a\u7684\u7ebf\u7a0b\u4e2d\uff0c\u6216\u8005\u81ea\u52a8\u5c06\u4efb\u52a1\u653e\u7f6e\u4e8e\u7a7a\u95f2\u7684\u7ebf\u7a0b\u4e2d\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7ebf\u7a0b\u6c60\u4e3b\u8981\u7531\u4e09\u90e8\u5206\u7ec4\u6210\uff1a<\/p>\n\n\n<ul>\n<li><strong>\u7ebf\u7a0b\u6c60\u7ba1\u7406\u5668\uff08ThreadPoolExecutor\uff09<\/strong>\uff1a\u8d1f\u8d23\u521b\u5efa\u3001\u7ba1\u7406\u548c\u63a7\u5236\u7ebf\u7a0b\u6c60\u3002\u5b83\u8d1f\u8d23\u7ebf\u7a0b\u7684\u521b\u5efa\u3001\u9500\u6bc1\u548c\u7ba1\u7406\uff0c\u4ee5\u53ca\u7ebf\u7a0b\u6c60\u7684\u72b6\u6001\u76d1\u63a7\u548c\u8c03\u5ea6\u4efb\u52a1\u3002<\/li>\n<li><strong>\u5de5\u4f5c\u961f\u5217\uff08BlockingQueue\uff09<\/strong>\uff1a\u7528\u4e8e\u5b58\u50a8\u5f85\u6267\u884c\u7684\u4efb\u52a1\u3002\u5f53\u7ebf\u7a0b\u6c60\u4e2d\u7684\u7ebf\u7a0b\u90fd\u5728\u6267\u884c\u4efb\u52a1\u65f6\uff0c\u65b0\u7684\u4efb\u52a1\u4f1a\u88ab\u653e\u5165\u5de5\u4f5c\u961f\u5217\u4e2d\u7b49\u5f85\u6267\u884c\u3002<\/li>\n<li><strong>\u7ebf\u7a0b\u6c60\u7ebf\u7a0b\uff08Worker Thread\uff09<\/strong>\uff1a\u5b9e\u9645\u6267\u884c\u4efb\u52a1\u7684\u7ebf\u7a0b\u3002\u7ebf\u7a0b\u6c60\u4e2d\u4f1a\u7ef4\u62a4\u4e00\u7ec4\u7ebf\u7a0b\uff0c\u8fd9\u4e9b\u7ebf\u7a0b\u53ef\u4ee5\u88ab\u91cd\u590d\u4f7f\u7528\uff0c\u4ece\u800c\u907f\u514d\u4e86\u9891\u7e41\u521b\u5efa\u548c\u9500\u6bc1\u7ebf\u7a0b\u7684\u5f00\u9500\u3002<br \/><br \/><\/li>\n<\/ul>\n\n\n<h2 class=\"wp-block-heading\">\u7ebf\u7a0b\u6c60\u7ba1\u7406\u5668<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u521b\u5efa\u7ebf\u7a0b\u6c60\u7ba1\u7406\u5668<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ThreadPool::ThreadPool(size_t numThreads) :\nnumThreads_(numThreads)\n{\n    std::cout &lt;&lt; \"Initializing ThreadPool with \" &lt;&lt; numThreads_ &lt;&lt; \" threads.\" &lt;&lt; std::endl;\n    for(int i = 0; i &lt; numThreads_; ++i) {\n        ThreadQueue* t = new ThreadQueue();\n        std::cout &lt;&lt; \"Created ThreadQueue \" &lt;&lt; i &lt;&lt; std::endl;\n        thread_queues_.push_back(t);\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u6784\u9020\u51fd\u6570\u4e2d\u521b\u5efa\u9700\u8981\u7684\u7ebf\u7a0b\u961f\u5217\uff0c\u901a\u8fc7\u52a8\u6001\u6570\u7ec4\u5bb9\u5668\u52a8\u6001\u6570\u7ec4\u5bb9\u5668\uff08std::vector\uff09\u4fdd\u5b58\u8d77\u6765\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5176\u5b9e\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6STL\u5bb9\u5668\u4fdd\u5b58\u7ebf\u7a0b\uff0c\u6bd4\u5982\u5b57\u5178\uff08std::map\uff09\uff0c\u6839\u636e\u9700\u6c42\u8fdb\u884c\u9009\u62e9\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u7ebf\u7a0b\u6c60\u7ba1\u7406\u5668\u4e2d\u53ef\u4ee5\u540c\u65f6\u7ba1\u7406\u7ebf\u7a0b\u961f\u5217\u7684\u542f\u52a8\uff0c\u505c\u6b62\u548c\u5185\u5b58\u9500\u6bc1\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u5de5\u4f5c<\/strong>\u7ebf\u7a0b<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5de5\u4f5c\u7ebf\u7a0b\u4e2d\u4f1a\u5305\u542b\u5de5\u4f5c\u961f\u5217\u548c\u7ebf\u7a0b\u4e24\u90e8\u5206\u7ec4\u6210\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u5de5\u4f5c\u7ebf\u7a0b\u7684\u4e2d\u521b\u5efa\u7ebf\u7a0b\uff0c\u5b9a\u4e49\u4e00\u4e2arun\u51fd\u6570\u7ed1\u5b9a\u5230\u7ebf\u7a0b\u4e2d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>thread_ = new std::thread(std::bind(&amp;ThreadQueue::run, this));<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u7ebf\u7a0b\u961f\u5217\u4e2d\u5982\u4f55\u80fd\u9ad8\u6548\u7684\u6267\u884c\u4efb\u52a1\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528\u6761\u4ef6\u53d8\u91cf\uff08std::condition_variable\uff09\u8ba9\u65e0\u4efb\u52a1\u7684\u7ebf\u7a0b\u8fdb\u5165\u7b49\u5f85\u72b6\u6001\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>\u6761\u4ef6\u53d8\u91cf\u662f\u4e00\u79cd\u7ebf\u7a0b\u540c\u6b65\u673a\u5236\uff0c\u7528\u4e8e\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\u5b9e\u73b0\u7ebf\u7a0b\u95f4\u7684\u7b49\u5f85\u548c\u901a\u77e5\u673a\u5236\u3002\u5b83\u901a\u5e38\u4e0e\u4e92\u65a5\u9501\u914d\u5408\u4f7f\u7528\u3002<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4efb\u52a1\u961f\u5217\u4e2d\u6ca1\u6709\u4efb\u52a1\u6216\u8005\u7ebf\u7a0b\u6c60\u7ba1\u7406\u5668\u4e0b\u53d1\u505c\u6b62\u547d\u4ee4\uff0c\u90a3\u4e48\u7ebf\u7a0b\u5c31\u4f1a\u8fdb\u5165\u7b49\u5f85\u72b6\u6001\u3002\u76f4\u5230\u6709\u4efb\u52a1\u6dfb\u52a0\u65f6\u4e0b\u53d1\u4efb\u52a1\u65f6\u518d\u6b21\u5524\u9192\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6dfb\u52a0\u4efb\u52a1\u5e76\u4e0b\u53d1\u5524\u9192\u7ebf\u7a0b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void ThreadQueue::addTask(const std::function&lt;void()> &amp;task)\n{   \n    std::lock_guard&lt;std::mutex> lock(mtx_);\n    taskQueue_.push(task);\n    std::cout &lt;&lt; \"Task added to the queue.\" &lt;&lt; std::endl;\n    cond_.notify_one();  \/\/ \u901a\u77e5\u5de5\u4f5c\u7ebf\u7a0b\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4efb\u52a1\u7ebf\u7a0b\u5177\u4f53\u6267\u884c\u8fc7\u7a0b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void ThreadQueue::run()\n{\n    while (true)\n    {\n        std::function&lt;void()> task;\n        std::unique_lock&lt;std::mutex> lock(mtx_);\n        cond_.wait_for(lock, std::chrono::milliseconds(1000), &#91;&amp;]{ \n            return !taskQueue_.empty() || is_stopped_; });\n        if (is_stopped_) {\n            break;\n        }\n        if (!taskQueue_.empty()){\n            \/\/ lock.lock();\n            std::cout &lt;&lt; \"id: \" &lt;&lt; thread_->get_id() &lt;&lt; \" Executing task...\" &lt;&lt; std::endl;\n            \/\/ auto task = taskQueue_.front();\n            task = std::move(taskQueue_.front());\n            taskQueue_.pop();\n            task(); \/\/ Execute the task\n            \/\/ lock.unlock();\n        } else {\n            std::this_thread::sleep_for(std::chrono::milliseconds(100));\n        }\n    }\n    \n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7ebf\u7a0b \u7ebf\u7a0b\u7684\u5b9a\u4e49\u5728\u767e\u5ea6\u4e0a\u7684\u5b9a\u4e49\u5982\u4e0b\uff1a \u7ebf\u7a0b\uff08\u82f1\u8bed\uff1athread\uff09\u662f\u64cd\u4f5c\u7cfb\u7edf\u80fd\u591f\u8fdb\u884c\u8fd0\u7b97\u8c03\u5ea6\u7684\u6700\u5c0f\u5355\u4f4d\u3002\u5b83\u88ab\u5305\u542b\u5728\u8fdb\u7a0b\u4e4b\u4e2d\uff0c\u662f\u8fdb\u7a0b\u4e2d\u7684\u5b9e\u9645\u8fd0\u4f5c\u5355\u4f4d\u3002\u4e00\u6761\u7ebf\u7a0b\u6307\u7684\u662f\u8fdb\u7a0b\u4e2d\u4e00\u4e2a\u5355\u4e00\u987a\u5e8f\u7684\u63a7\u5236\u6d41\uff0c\u4e00\u4e2a\u8fdb\u7a0b\u4e2d\u53ef\u4ee5\u5e76\u53d1\u591a\u4e2a\u7ebf\u7a0b\uff0c\u6bcf\u6761\u7ebf\u7a0b\u5e76\u884c\u6267\u884c\u4e0d\u540c\u7684\u4efb\u52a1\u3002\u5728Unix System V\u53caSunOS\u4e2d\u4e5f\u88ab\u79f0\u4e3a\u8f7b\u91cf\u8fdb\u7a0b\uff08lightweight processes\uff09\uff0c\u4f46\u8f7b\u91cf\u8fdb\u7a0b\u66f4\u591a\u6307\u5185\u6838\u7ebf\u7a0b\uff08kernel threa [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":428,"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_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[5],"tags":[],"class_list":["post-423","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c"],"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/whaledream.xyz\/wp-content\/uploads\/2025\/12\/b501c583cc4e2a2d9f66c616e1a68835523027954.jpg","_links":{"self":[{"href":"https:\/\/whaledream.xyz\/index.php\/wp-json\/wp\/v2\/posts\/423","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/whaledream.xyz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/whaledream.xyz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/whaledream.xyz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/whaledream.xyz\/index.php\/wp-json\/wp\/v2\/comments?post=423"}],"version-history":[{"count":0,"href":"https:\/\/whaledream.xyz\/index.php\/wp-json\/wp\/v2\/posts\/423\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/whaledream.xyz\/index.php\/wp-json\/wp\/v2\/media\/428"}],"wp:attachment":[{"href":"https:\/\/whaledream.xyz\/index.php\/wp-json\/wp\/v2\/media?parent=423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/whaledream.xyz\/index.php\/wp-json\/wp\/v2\/categories?post=423"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/whaledream.xyz\/index.php\/wp-json\/wp\/v2\/tags?post=423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}