{"id":326,"date":"2025-09-13T15:46:53","date_gmt":"2025-09-13T07:46:53","guid":{"rendered":"http:\/\/www.tgwttt.xyz\/?p=326"},"modified":"2025-11-24T16:36:58","modified_gmt":"2025-11-24T08:36:58","slug":"%e6%b7%b1%e5%85%a5%e6%b5%85%e5%87%ba%e7%ba%bf%e7%a8%8b%e6%b1%a0","status":"publish","type":"post","link":"https:\/\/www.tgwttt.xyz\/?p=326","title":{"rendered":"\u6df1\u5165\u6d45\u51fa\u7ebf\u7a0b\u6c60"},"content":{"rendered":"\n<p>\u4ee3\u7801\u4ed3\u5e93\uff1a<a href=\"https:\/\/gitee.com\/tgwTTT\/linux-learning-dai\/tree\/master\/ThreadPool2\">https:\/\/gitee.com\/tgwTTT\/linux-learning-dai\/tree\/master\/ThreadPool2<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u5f15\u8a00<\/strong><\/h4>\n\n\n\n<p>\u5728\u73b0\u4ee3\u8f6f\u4ef6\u5f00\u53d1\u4e2d\uff0c\u591a\u7ebf\u7a0b\u7f16\u7a0b\u662f\u63d0\u5347\u7a0b\u5e8f\u6027\u80fd\u7684\u91cd\u8981\u624b\u6bb5\u3002\u7136\u800c\uff0c\u9891\u7e41\u521b\u5efa\u548c\u9500\u6bc1\u7ebf\u7a0b\u4f1a\u5e26\u6765\u663e\u8457\u7684\u7cfb\u7edf\u5f00\u9500\u3002\u7ebf\u7a0b\u6c60\uff08Thread Pool\uff09\u4f5c\u4e3a\u4e00\u79cd\u9ad8\u6548\u7684\u5e76\u53d1\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u901a\u8fc7\u9884\u5148\u521b\u5efa\u5e76\u7ba1\u7406\u4e00\u7ec4\u7ebf\u7a0b\uff0c\u5b9e\u73b0\u4e86\u7ebf\u7a0b\u7684\u590d\u7528\uff0c\u4ece\u800c\u89e3\u51b3\u4e86\u8fd9\u4e00\u95ee\u9898\u3002\u672c\u6587\u5c06\u7cfb\u7edf\u6027\u5730\u4ecb\u7ecd\u7ebf\u7a0b\u6c60\u7684\u57fa\u672c\u6982\u5ff5\u3001\u6838\u5fc3\u7ec4\u4ef6\uff0c\u5e76\u901a\u8fc7\u5173\u952e\u4ee3\u7801\u7247\u6bb5\u89e3\u6790\u5176\u5de5\u4f5c\u539f\u7406\uff0c\u5e2e\u52a9\u521d\u5b66\u8005\u5efa\u7acb\u6e05\u6670\u7684\u7406\u89e3\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u4e00\u3001\u7ebf\u7a0b\u6c60\u7684\u57fa\u672c\u6982\u5ff5<\/strong><\/h4>\n\n\n\n<p>\u7ebf\u7a0b\u6c60\u662f\u4e00\u79cd\u7ba1\u7406\u591a\u4e2a\u7ebf\u7a0b\u6267\u884c\u4efb\u52a1\u7684\u673a\u5236\u3002\u5176\u6838\u5fc3\u601d\u60f3\u662f\u8d44\u6e90\u6c60\u5316\uff1a\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u521b\u5efa\u56fa\u5b9a\u6570\u91cf\u7684\u7ebf\u7a0b\uff0c\u5e76\u5c06\u5b83\u4eec\u7f6e\u4e8e\u7b49\u5f85\u72b6\u6001\u3002\u5f53\u6709\u4efb\u52a1\u9700\u8981\u6267\u884c\u65f6\uff0c\u7ebf\u7a0b\u6c60\u4ece\u6c60\u4e2d\u5206\u914d\u4e00\u4e2a\u7a7a\u95f2\u7ebf\u7a0b\u6765\u5904\u7406\u4efb\u52a1\uff0c\u4efb\u52a1\u5b8c\u6210\u540e\u7ebf\u7a0b\u8fd4\u56de\u6c60\u4e2d\uff0c\u7b49\u5f85\u4e0b\u4e00\u4e2a\u4efb\u52a1\u3002\u8fd9\u79cd\u65b9\u5f0f\u907f\u514d\u4e86\u7ebf\u7a0b\u7684\u91cd\u590d\u521b\u5efa\u4e0e\u9500\u6bc1\uff0c\u663e\u8457\u964d\u4f4e\u4e86\u7cfb\u7edf\u5f00\u9500\u3002<\/p>\n\n\n\n<p>\u7ebf\u7a0b\u6c60\u4e3b\u8981\u89e3\u51b3\u4e86\u4ee5\u4e0b\u95ee\u9898\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u6027\u80fd\u74f6\u9888\uff1a\u7ebf\u7a0b\u7684\u521b\u5efa\u548c\u9500\u6bc1\u6d89\u53ca\u7cfb\u7edf\u8c03\u7528\uff0c\u6d88\u8017CPU\u548c\u5185\u5b58\u8d44\u6e90\u3002<\/li>\n\n\n\n<li>\u8d44\u6e90\u5931\u63a7\uff1a\u65e0\u9650\u5236\u5730\u521b\u5efa\u7ebf\u7a0b\u53ef\u80fd\u5bfc\u81f4\u7cfb\u7edf\u8d44\u6e90\u8017\u5c3d\u3002<\/li>\n\n\n\n<li>\u7f16\u7a0b\u590d\u6742\u6027\uff1a\u624b\u52a8\u7ba1\u7406\u7ebf\u7a0b\u7684\u751f\u547d\u5468\u671f\u5bb9\u6613\u51fa\u9519\u3002<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u4e8c\u3001\u6838\u5fc3\u7ec4\u4ef6\u4e0e\u5de5\u4f5c\u6d41\u7a0b<\/strong><\/h4>\n\n\n\n<p>\u4e00\u4e2a\u5178\u578b\u7684\u7ebf\u7a0b\u6c60\u5305\u542b\u56db\u4e2a\u6838\u5fc3\u7ec4\u4ef6\uff1a\u7ebf\u7a0b\u96c6\u5408\u3001\u4efb\u52a1\u961f\u5217\u3001\u4e92\u65a5\u9501\u548c\u6761\u4ef6\u53d8\u91cf\u3002\u5b83\u4eec\u534f\u540c\u5de5\u4f5c\uff0c\u786e\u4fdd\u4efb\u52a1\u88ab\u5b89\u5168\u3001\u9ad8\u6548\u5730\u5904\u7406\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">1. \u4e92\u65a5\u9501\uff08Mutex\uff09\u4e0e\u6761\u4ef6\u53d8\u91cf\uff08Condition Variable\uff09<\/h5>\n\n\n\n<p>\u4e92\u65a5\u9501\u7528\u4e8e\u4fdd\u62a4\u5171\u4eab\u8d44\u6e90\uff0c\u9632\u6b62\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee\u5bfc\u81f4\u6570\u636e\u7ade\u4e89\u3002\u6761\u4ef6\u53d8\u91cf\u5219\u7528\u4e8e\u7ebf\u7a0b\u95f4\u7684\u540c\u6b65\uff0c\u5b9e\u73b0\u201c\u7b49\u5f85-\u901a\u77e5\u201d\u673a\u5236\u3002\u4e8c\u8005\u7ed3\u5408\u4f7f\u7528\uff0c\u662f\u6784\u5efa\u7ebf\u7a0b\u6c60\u7684\u57fa\u7840\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u4e92\u65a5\u9501\u7c7b\uff0c\u7528\u4e8e\u4fdd\u62a4\u5171\u4eab\u6570\u636e\nclass Mutex {\npublic:\n    Mutex() { pthread_mutex_init(&_mutex, nullptr); }\n    ~Mutex() { pthread_mutex_destroy(&_mutex); }\n    void Lock() { pthread_mutex_lock(&_mutex); }\n    void Unlock() { pthread_mutex_unlock(&_mutex); }\n    pthread_mutex_t* GetPthreadMutex() { return &_mutex; }\nprivate:\n    pthread_mutex_t _mutex;\n};\n\n\/\/ \u6761\u4ef6\u53d8\u91cf\u7c7b\uff0c\u7528\u4e8e\u7ebf\u7a0b\u540c\u6b65\nclass Cond {\npublic:\n    Cond() { pthread_cond_init(&_cond, nullptr); }\n    ~Cond() { pthread_cond_destroy(&_cond); }\n    void Wait(Mutex* mutex) { \n        pthread_cond_wait(&_cond, mutex->GetPthreadMutex()); \n    }\n    void Signal() { pthread_cond_signal(&_cond); }\n    void Broadcast() { pthread_cond_broadcast(&_cond); }\nprivate:\n    pthread_cond_t _cond;\n};<\/code><\/pre>\n\n\n\n<p>2. \u4efb\u52a1\u961f\u5217\u4e0e\u7ebf\u7a0b\u5904\u7406\u903b\u8f91<\/p>\n\n\n\n<p>\u4efb\u52a1\u961f\u5217\u662f\u4e00\u4e2a\u7ebf\u7a0b\u5b89\u5168\u7684\u961f\u5217\uff0c\u7528\u4e8e\u5b58\u653e\u5f85\u5904\u7406\u7684\u4efb\u52a1\u3002\u5de5\u4f5c\u7ebf\u7a0b\u4ece\u961f\u5217\u4e2d\u53d6\u51fa\u4efb\u52a1\u5e76\u6267\u884c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void HandlerTask() {\n    Task task;\n    while (true) {\n        LockGuard lock(_mutex);\n        \/\/ \u4f7f\u7528while\u5faa\u73af\u9632\u6b62\u865a\u5047\u5524\u9192\n        while (_taskq.empty() && _isrunning) {\n            _cond.Wait(&_mutex);\n        }\n        \/\/ \u68c0\u67e5\u7ebf\u7a0b\u6c60\u662f\u5426\u505c\u6b62\n        if (!_isrunning && _taskq.empty()) {\n            break;\n        }\n        \/\/ \u53d6\u51fa\u4efb\u52a1\n        task = std::move(_taskq.front());\n        _taskq.pop();\n    } \/\/ LockGuard\u6790\u6784\uff0c\u81ea\u52a8\u91ca\u653e\u9501\n    \/\/ \u5728\u9501\u5916\u6267\u884c\u4efb\u52a1\uff0c\u907f\u514d\u963b\u585e\u5176\u4ed6\u7ebf\u7a0b\n    task();\n}<\/code><\/pre>\n\n\n\n<p>3. \u4efb\u52a1\u63d0\u4ea4\u63a5\u53e3<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>bool Enqueue(const Task& t) {\n    LockGuard lock(_mutex);\n    if (!_isrunning) {\n        return false; \/\/ \u7ebf\u7a0b\u6c60\u5df2\u505c\u6b62\n    }\n    _taskq.push(t);\n    _cond.Signal(); \/\/ \u5524\u9192\u4e00\u4e2a\u7b49\u5f85\u7684\u7ebf\u7a0b\n    return true;\n}<\/code><\/pre>\n\n\n\n<p>\u4eca\u5929\u7684\u66f4\u65b0\u5c31\u5230\u8fd9\u91cc\uff0c\u5982\u6709\u9519\u8bef\u6b22\u8fce\u6307\u51fa\uff01\uff01<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee3\u7801\u4ed3\u5e93\uff1ahttps:\/\/gitee.com\/tgwTTT\/linux-learning-dai\/tree\/m&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[4,5],"tags":[],"class_list":["post-326","post","type-post","status-publish","format-standard","hentry","category-c","category-linux"],"_links":{"self":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/326","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=326"}],"version-history":[{"count":4,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/326\/revisions"}],"predecessor-version":[{"id":839,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/326\/revisions\/839"}],"wp:attachment":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=326"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=326"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=326"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}