{"id":81764,"date":"2026-07-25T04:18:49","date_gmt":"2026-07-24T20:18:49","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/81764.html"},"modified":"2026-07-25T04:18:49","modified_gmt":"2026-07-24T20:18:49","slug":"%e4%bb%8e%e9%9b%b6%e6%9e%84%e5%bb%ba%e9%ab%98%e6%80%a7%e8%83%bd-reactor-%e6%9c%8d%e5%8a%a1%e5%99%a8%ef%bc%9a","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/81764.html","title":{"rendered":"\u4ece\u96f6\u6784\u5efa\u9ad8\u6027\u80fd Reactor \u670d\u52a1\u5668\uff1a"},"content":{"rendered":"<h3>\u5f15\u8a00<\/h3>\n<p>\u5728\u9ad8\u5e76\u53d1\u7f51\u7edc\u7f16\u7a0b\u9886\u57df&#xff0c;Reactor \u6a21\u578b\u51ed\u501f\u5176\u9ad8\u6548\u7684\u4e8b\u4ef6\u9a71\u52a8\u673a\u5236&#xff0c;\u6210\u4e3a\u6784\u5efa\u9ad8\u6027\u80fd\u670d\u52a1\u5668\u7684\u9996\u9009\u65b9\u6848\u3002\u672c\u6587\u5c06\u4ece\u96f6\u5f00\u59cb&#xff0c;\u8be6\u7ec6\u5256\u6790\u4e00\u4e2a\u57fa\u4e8e\u00a0one loop per thread\u00a0\u67b6\u6784\u7684 C&#043;&#043; TCP Echo \u670d\u52a1\u5668\u7684\u5b9e\u73b0\u3002\u8be5\u670d\u52a1\u5668\u53c2\u8003\u4e86 muduo \u7f51\u7edc\u5e93\u7684\u8bbe\u8ba1\u601d\u60f3&#xff0c;\u652f\u6301\u591a\u6838\u6269\u5c55\u3001\u975e\u963b\u585e I\/O\u3001ET \u6a21\u5f0f&#xff0c;\u5e76\u91c7\u7528\u5f02\u6b65\u65e5\u5fd7\u548c\u65e0\u9501\u961f\u5217\u8fdb\u884c\u4f18\u5316\u3002\u6700\u7ec8\u5b9e\u73b0\u4e86\u4e00\u4e2a\u53ef\u652f\u6491\u6570\u6570\u4e07\u5e76\u53d1\u8fde\u63a5(\u53d6\u51b3\u4e8e\u786c\u4ef6\u914d\u7f6e)\u7684\u9ad8\u6027\u80fd Echo \u670d\u52a1\u3002<\/p>\n<p>\u8be5\u670d\u52a1\u5668\u4e3aAeroChat(\u9ad8\u6027\u80fd\u804a\u5929\u5ba4)\u7684\u7f51\u7edc\u90e8\u5206&#xff0c;\u6211\u5265\u79bb\u51fa\u6765\u8fdb\u884c\u4e86\u538b\u6d4b\u3002\u9879\u76ee\u4ee3\u7801\u5f00\u6e90\u5728<span class=\"link-card-box\"><span class=\"link-title\">AeroChat\/benchmark at master \u00b7 ywx914705\/AeroChat<\/span><span class=\"link-link\"><img decoding=\"async\" class=\"link-link-icon\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260724201847-6a63c8a791499.png\" \/>https:\/\/github.com\/ywx914705\/AeroChat\/tree\/master\/benchmark<\/span><\/span>&#xff0c;\u8fd9\u4e2a\u9879\u76ee\u53ea\u5305\u542b\u4e86\u7f51\u7edc\u5c42\u6b21&#xff0c;\u4e0d\u6d89\u53ca\u4efb\u4f55\u4e1a\u52a1\u5c42\u6b21\u3002<\/p>\n<h2 style=\"background-color:transparent\">\u4e00\u3001\u670d\u52a1\u5668\u67b6\u6784&#xff1a;<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"809\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260724201847-6a63c8a797f12.png\" width=\"1755\" \/><\/p>\n<p>\u670d\u52a1\u5668\u91c7\u7528\u00a0\u4e3b\u4ece Reactor \u591a\u7ebf\u7a0b\u6a21\u578b&#xff1a;<\/p>\n<p>\u4e3bReactor&#xff1a;\u4e00\u4e2aEventLoop\u7ebf\u7a0b,\u8d1f\u8d23\u76d1\u542clistenFd_\u4e0a\u7684\u65b0\u8fde\u63a5\u4e8b\u4ef6\u3002<\/p>\n<p>\u4ece\/\u5b50Reactor:\u591a\u4e2aEventLoop,\u6bcf\u4e2a\u7ebf\u7a0b\u8d1f\u8d23\u8fd0\u884c\u4e00\u4e2aEventLoop&#xff0c;\u8d1f\u8d23\u5df2\u8fde\u63a5\u7684socket\u7684I\/O\u8bfb\u5199\u3002<\/p>\n<p>\u901a\u8fc7\u8d1f\u8f7d\u5747\u8861\u7684\u65b9\u5f0f&#xff0c;\u65b0\u8fde\u63a5\u901a\u8fc7round-robin\u7b97\u6cd5\u5206\u914d\u5230\u67d0\u4e2aSubReactor\/\u5b50Reactor&#xff0c;\u8fde\u63a5\u4e00\u65e6\u5206\u914d\u4fbf\u56fa\u5b9a\u5728\u8be5\u7ebf\u7a0b&#xff0c;\u8fd9\u6837\u505a\u7684\u597d\u5904\u5c31\u662f\u4e0d\u9700\u8981\u9501\u540c\u6b65&#xff0c;\u5929\u7136\u7ebf\u7a0b\u5b89\u5168&#xff01;<\/p>\n<p>\u8de8\u7ebf\u7a0b\u4efb\u52a1:\u901a\u8fc7RunInLoop\/queueInLoop&#043;eventfd\u5b9e\u73b0\u7ebf\u7a0b\u5b89\u5168\u7684\u4efb\u52a1\u6295\u9012\u3002<\/p>\n<h2>\u4e8c\u3001\u6838\u5fc3\u7ec4\u4ef6\u89e3\u6790:<\/h2>\n<p>\u7531\u4e8e\u7bc7\u5e45\u6709\u9650&#xff0c;\u4ee5\u4e0b\u6240\u6709\u7c7b\u53ea\u5217\u4e3e\u51fa\u90e8\u5206\u6e90\u7801&#xff0c;\u8be6\u7ec6\u6e90\u7801\u53ef\u53bb\u8fd9\u91cc\u67e5\u770b&#xff1a;<span class=\"link-card-box\"><span class=\"link-title\">AeroChat\/benchmark at master \u00b7 ywx914705\/AeroChat<\/span><span class=\"link-link\"><img decoding=\"async\" class=\"link-link-icon\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260724201847-6a63c8a791499.png\" \/>https:\/\/github.com\/ywx914705\/AeroChat\/tree\/master\/benchmark<\/span><\/span><\/p>\n<h3 style=\"background-color:transparent\">1\u3001noncopyable\u7c7b:<\/h3>\n<p>#pragma once<br \/>\n\/\/ \u7981\u6b62\u62f7\u8d1d\u7684\u57fa\u7c7b&#xff08;\u53c2\u8003\u4e86Muduo\u6838\u5fc3\u8bbe\u8ba1&#xff0c;\u907f\u514d\u6d45\u62f7\u8d1d\u8d44\u6e90\u9519\u8bef&#xff09;<br \/>\n\/*<br \/>\n\u4f5c\u4e3a\u4e00\u4e2a\u57fa\u7c7b,\u4efb\u4f55\u7ee7\u627f\u81eanoncopyable\u7684\u7c7b\u90fd\u65e0\u6cd5\u88ab\u62f7\u8d1d\u6784\u9020\u4e0e\u62f7\u8d1d\u590d\u5236,\u8fd9\u662f\u4e3a\u4e86\u9632\u6b62\u90a3\u4e9b\u62e5\u6709\u8d44\u6e90\u7684<br \/>\n\u5bf9\u8c61\u88ab\u9519\u8bef\u62f7\u8d1d,\u8fdb\u800c\u53d1\u751f\u6790\u6784\u65f6\u91cd\u590d\u91ca\u653e\u7684\u9519\u8bef<br \/>\n\u6b21\u670d\u52a1\u5668\u4e2d\u5927\u90e8\u5206\u7684\u7c7b\u90fd\u57fa\u5c42\u81eanoncopyable(\u4e0d\u5f97\u62f7\u8d1d)<br \/>\n*\/<br \/>\nclass noncopyable {<br \/>\nprotected:<br \/>\n    noncopyable() &#061; default;<br \/>\n    ~noncopyable() &#061; default;<br \/>\n    noncopyable(const noncopyable&amp;) &#061; delete;<br \/>\n    noncopyable&amp; operator&#061;(const noncopyable&amp;) &#061; delete;<br \/>\n}; <\/p>\n<h3>2\u3001Thread\u7c7b:<\/h3>\n<p>Thread\u7c7b\u7684\u4e3b\u8981\u5de5\u4f5c\u5c31\u662f\u4f7f\u7528c&#043;&#043;11\u7684thread\u5e93\u5bf9linux\u539f\u751f\u63a5\u53e3\u8fdb\u884c\u5c01\u88c5:<\/p>\n<p>\/*<br \/>\n\u8be5\u6587\u4ef6\u672c\u8d28\u4e0a\u5c31\u662f\u5bf9linux posix\u7ebf\u7a0b\u7684\u5c01\u88c5(pthread_create\u521b\u5efa\u51fa\u6765\u7684\u7ebf\u7a0b)<br \/>\n\u8fd9\u4e2a\u7c7b\u5728\u6574\u4e2a\u9879\u76ee\u4e2d\u4f5c\u4e3a\u57fa\u7840\u7ec4\u4ef6,\u88abEventLoopThread\u7b49\u4e0a\u5c42\u7c7b\u4f7f\u7528<br \/>\nlinux\u4e2dposix\u5e38\u7528\u51fd\u6570:<br \/>\npthread_create()   pthread_join()<br \/>\n\u6838\u5fc3\u4f18\u52bf:\u652f\u6301CPU\u4eb2\u548c\u6027,\u53ef\u4ee5\u5c06\u7ebf\u7a0b\u7ed1\u5b9a\u5230\u6307\u5b9aCPU\u6838\u5fc3,\u4f18\u5316\u6027\u80fd<br \/>\n\u4e0d\u53ef\u62f7\u8d1d:\u8be5AeroChat\u4e2d\u7edd\u5927\u591a\u6570\u7684\u7c7b\u90fd\u7ee7\u627f\u81eanocopyable,\u4e0d\u652f\u6301\u62f7\u8d1d&#xff0c;\u9632\u6b62\u591a\u4e2a\u5bf9\u8c61\u7ba1\u7406\u540c\u4e00\u4e2a\u7ebf\u7a0b<br \/>\n,\u907f\u514d\u8d44\u6e90\u88ab\u91cd\u590d\u91ca\u653e<br \/>\n\u8fd9\u4e2a\u7c7b\u7684\u53ef\u8bfb\u6027\u5f88\u9ad8,\u57fa\u672c\u4e0a\u770b\u5230\u51fd\u6570\/\u53d8\u91cf\u540d\u5c31\u80fd\u77e5\u9053\u5b83\u7684\u529f\u80fd\/\u4f5c\u7528<br \/>\n*\/<br \/>\n#pragma once<br \/>\n#include &#034;noncopyable.hpp&#034;<br \/>\n#include &lt;pthread.h&gt;<br \/>\n#include &lt;functional&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;atomic&gt;<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &lt;sched.h&gt;\/\/\u7ed1CPU\u6838\u5fc3\u6240\u9700\u5934\u6587\u4ef6<\/p>\n<p>class Thread : noncopyable {<br \/>\npublic:<br \/>\n    using ThreadFunc &#061; std::function&lt;void()&gt;;<\/p>\n<p>    explicit Thread(ThreadFunc func, const std::string&amp; name &#061; &#034;Thread&#034;);<br \/>\n    ~Thread();<\/p>\n<p>    void start();<br \/>\n    void join();\/\/\u5e95\u5c42\u8c03\u7528pthread_join()<br \/>\n    void bindCurrentThreadToCore(int coreId);<br \/>\n    \/\/\u76ee\u7684\u662f\u8ba9\u5f53\u524d\u8c03\u7528\u5b83\u7684\u7ebf\u7a0b\u7ed1\u5b9a\u5230\u6307\u5b9a\u6838\u5fc3\u3002\u901a\u5e38\u6211\u4eec\u5728\u5b50\u7ebf\u7a0b\u5185\u90e8\u8c03\u7528\u5b83&#xff0c;<br \/>\n    \/\/\u5c06\u81ea\u5df1\u7ed1\u5b9a\u5230\u9884\u5148\u8bbe\u5b9a\u7684\u6838\u5fc3\u3002<\/p>\n<p>    void setCoreAffinity(int coreId) { coreId_ &#061; coreId; }<br \/>\n    \/\/\u8bbe\u7f6e\u6210\u5458coureId_,\u8bb0\u5f55\u5e0c\u671b\u7ed1\u5b9a\u7684\u7ebf\u7a0b\u6838\u5fc3\u53f7,\u5b9e\u9645\u7ed1\u5b9a\u53d1\u751f\u5728\u7ebf\u7a0b\u542f\u52a8\u540e<\/p>\n<p>    pthread_t tid() const { return tid_; }<br \/>\n    const std::string&amp; name() const { return name_; }<br \/>\n    static int numCreated() { return numCreated_.load(); }<\/p>\n<p>private:<br \/>\n    static void* runInThread(void* arg);<\/p>\n<p>    ThreadFunc func_;\/\/\u7ebf\u7a0b\u51fd\u6570<br \/>\n    std::string name_;\/\/\u7ebf\u7a0b\u540d\u5b57,\u7528\u4e8e\u65e5\u5fd7\u6807\u8bc6<br \/>\n    pthread_t tid_;\/\/\u7ebf\u7a0bID<br \/>\n    bool started_;\/\/bool\u7c7b\u578b,\u8868\u793a\u7ebf\u7a0b\u662f\u5426\u542f\u52a8 ture:\u542f\u52a8\u4e86 false:\u6ca1\u6709\u542f\u52a8<br \/>\n    bool joined_;\/\/\u662f\u5426\u8c03\u7528join(\u662f\u5426\u8c03\u7528linux\u4e2d\u7684pthrea_join()\u51fd\u6570)<br \/>\n    static std::atomic_int numCreated_;\/\/\u5df2\u7ecf\u521b\u5efa\u7ebf\u7a0b\u7684\u6570\u91cf,\u9759\u6001\u539f\u5b50\u53d8\u91cf<\/p>\n<p>    int coreId_ &#061; -1;\/\/\u671f\u671b\u7ed1\u5b9a\u7684 CPU \u6838\u5fc3\u7f16\u53f7&#xff0c;-1 \u8868\u793a\u4e0d\u7ed1\u5b9a<br \/>\n}; <\/p>\n<h3>3\u3001Channel(I\/O \u4e8b\u4ef6\u5206\u53d1\u5668)&#xff1a;<\/h3>\n<p>Channel\u5305\u542b\u4e86\u5bf9fd\u7684\u6240\u6709\u5c01\u88c5&#xff0c;\u4e00\u4e2achannel\u5bf9\u5e94\u4e00\u4e2afd\u3002<\/p>\n<p>Channel\u7684\u6210\u5458\u53d8\u91cf:<\/p>\n<p>EventLoop* loop_;\/\/\u6240\u5c5e\u7684\u4e8b\u4ef6\u5faa\u73af<br \/>\n    const int fd_;\/\/\u6587\u4ef6\u63cf\u8ff0\u7b26<br \/>\n    uint32_t events_;\/\/\u5f53\u524d\u5173\u6ce8\u7684\u4e8b\u4ef6(\u5982EPOLLIN\/EPOLLOUT\/EPOLLET)<br \/>\n    uint32_t revents_;\/\/epoll_wait\u6240\u8fd4\u56de\u5b9e\u9645\u53d1\u751f\u7684\u4e8b\u4ef6<br \/>\n    int index_;\/\/\u5728Poller\u4e2d\u7684\u72b6\u6001(kNew\/kAdded\/kDeleted)<br \/>\n     \/\/\u8bfb\u3001\u5199\u3001\u5173\u95ed\u3001\u9519\u8bef\u5bf9\u5e94\u7684\u4e8b\u4ef6\u56de\u8c03  \u56de\u8c03\u51fd\u6570:\u7528\u6237\u6ce8\u518c\u7684\u4e1a\u52a1\u5904\u7406\u51fd\u6570,\u5f53\u5bf9\u5e94\u4e8b\u4ef6\u53d1\u751f\u65f6\u88ab\u8c03\u7528<br \/>\n    EventCallback readCallback_;<br \/>\n    EventCallback writeCallback_;<br \/>\n    EventCallback closeCallback_;<br \/>\n    EventCallback errorCallback_; <\/p>\n<p>Channel\u7684\u6210\u5458\u65b9\u6cd5:<\/p>\n<p>void Channel::handleEvent() {  \/\/\u8be5\u51fd\u6570\u7531EventLoop::loop()\u6536\u5230epoll\u4e8b\u4ef6\u540e\u8fdb\u884c\u8c03\u7528,\u4e14\u6267\u884c\u5728\u6240\u5c5e\u7684<br \/>\n\/\/EventLoop\u7ebf\u7a0b\u4e2d,\u4fdd\u8bc1\u4e86\u7ebf\u7a0b\u5b89\u5168<br \/>\n    if (revents_ &amp; (EPOLLRDHUP | EPOLLHUP)) { \/\/\u5982\u679c\u5bf9\u7aef\u5173\u95ed\u8fde\u63a5\u6216\u8005\u6302\u8d77,\u6b64\u65f6\u8fde\u63a5\u4e0d\u53ef\u7528,\u76f4\u63a5\u5173\u95ed<br \/>\n        LOG_WARN(&#034;[Channel] FD &#034; &#043; std::to_string(fd_) &#043; &#034; \u5ba2\u6237\u7aef\u5173\u95ed\u8fde\u63a5&#034;);<br \/>\n        if (closeCallback_) closeCallback_();<br \/>\n        return;<br \/>\n    }<br \/>\n    if (revents_ &amp; EPOLLERR) {<br \/>\n        LOG_ERROR(&#034;[Channel] FD &#034; &#043; std::to_string(fd_) &#043; &#034; \u53d1\u751f\u9519\u8bef&#034;);<br \/>\n        if (errorCallback_) errorCallback_();<br \/>\n    }<br \/>\n    if (revents_ &amp; (EPOLLIN | EPOLLPRI)) {<br \/>\n        if (readCallback_) readCallback_();<br \/>\n    }<br \/>\n    if (revents_ &amp; EPOLLOUT) {<br \/>\n        if (writeCallback_) writeCallback_();<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/\u901a\u77e5EventLoop\u8fdb\u884c\u66f4\u65b0        Channel\u4e0d\u76f4\u63a5\u64cd\u4f5cepoll,\u800c\u662f\u901a\u8fc7EventLoop\u7edf\u4e00\u7ba1\u7406<br \/>\nvoid Channel::update() {<br \/>\n    loop_-&gt;updateChannel(this);<br \/>\n}<\/p>\n<p>\u4e3a\u4ec0\u4e48\u8981\u6709Channel?<\/p>\n<p>Channel \u4e0d\u76f4\u63a5\u64cd\u4f5c epoll&#xff0c;\u800c\u662f\u901a\u8fc7\u00a0EventLoop\u00a0\u95f4\u63a5\u8c03\u7528\u00a0Poller&#xff0c;\u5b9e\u73b0\u89e3\u8026\u3002\u5982\u679c\u5bf9Poller\u548cEventLoop\u4e0d\u662f\u5f88\u4e86\u89e3\u53ef\u4ee5\u7ee7\u7eed\u5f80\u4e0b\u770b<\/p>\n<h3>4\u3001Poller&#xff1a;<\/h3>\n<p>Poller.hpp<\/p>\n<p>\/*<br \/>\nPoller:\u6bcf\u4e2aEventLoop\u62e5\u6709\u4e00\u4e2aPoller\u5bf9\u8c61(\u4e00\u4e00\u5bf9\u5e94),\u8d1f\u8d23\u5b9e\u9645\u7684epoll\u64cd\u4f5c,EventLoop\u901a\u8fc7updateChannel<br \/>\n\u5c06Channel\u7684\u53d8\u5316\u540c\u6b65\u5230Poller,\u5e76\u5728\u6bcf\u6b21\u5faa\u73af\u4e2d\u8c03\u7528poll\u83b7\u53d6\u6d3b\u8dc3\u7684Channel,\u7136\u540e\u8c03\u7528\u5b83\u4eec\u7684handleEvent<br \/>\n*\/<br \/>\n#pragma once<br \/>\n#include &#034;noncopyable.hpp&#034;<br \/>\n#include &lt;sys\/epoll.h&gt;<br \/>\n#include &lt;unordered_map&gt;<br \/>\n#include &lt;vector&gt;<br \/>\n\/\/ \u524d\u7f6e\u58f0\u660e<br \/>\nclass EventLoop;<br \/>\nclass Channel;<\/p>\n<p>class Poller : noncopyable {<br \/>\npublic:<br \/>\n  explicit Poller(EventLoop *loop);<br \/>\n  ~Poller() &#061; default;<\/p>\n<p>  void poll(int timeoutMs, std::vector&lt;Channel *&gt; *activeChannels);<br \/>\n  void updateChannel(Channel *channel);<br \/>\n  void removeChannel(Channel *channel);<\/p>\n<p>private:<br \/>\n  void fillActiveChannels(int numEvents,<br \/>\n                          std::vector&lt;Channel *&gt; *activeChannels) const;<\/p>\n<p>  EventLoop *ownerLoop_; \/\/ \u5bf9\u5e94\u7684EventLoop<br \/>\n  int epollfd_;          \/\/ epoll_create1()\u521b\u5efa\u51fa\u6765\u7684df<br \/>\n  std::unordered_map&lt;int, Channel *&gt; channels_; \/\/ fd\u4e0eChannel\u7684\u6620\u5c04\u5173\u7cfb<br \/>\n  std::vector&lt;struct epoll_event&gt; events_; \/\/ \u7528\u4e8e epoll_wait \u7684\u4e8b\u4ef6\u6570\u7ec4<br \/>\n}; <\/p>\n<p>Poller\u53ea\u6709\u56db\u4e2a\u6210\u5458\u53d8\u91cf&#xff0c;\u5206\u522b\u662fownerLoop_\u3001epoolfd\u3001channels\u3001events_&#xff0c;\u90a3\u4e48\u5206\u522b\u90fd\u662f\u4ec0\u4e48\u610f\u601d\u5462&#xff1f;<\/p>\n<p>1\u3001ownerLoop_\u8868\u793a\u7684\u662fEventLoop\u5bf9\u5e94\u7684\u6307\u9488&#xff0c;\u4e00\u4e2aPoller\u5bf9\u5e94\u4e00\u4e2aEventLoop,Poller\u548cEventLoop\u662f\u4e00\u4e00\u5bf9\u5e94\u7684\u5173\u7cfb&#xff0c;\u6240\u4ee5Poller\u5fc5\u987b\u5f97\u6709EventLoop\u7684\u6307\u9488&#xff0c;\u95f4\u63a5\u5730\u6807\u5fd7\u4e86\u8be5Poller\u662f\u4e0e\u54ea\u4e00\u4e2aEventLoop\u5bf9\u5e94\u7684\u3002<\/p>\n<p>2\u3001epollfd&#xff0c;\u8fd9\u4e2a\u53ea\u8981\u662f\u7528\u8fc7epoll\u7684\u670b\u53cb\u90fd\u5e94\u8be5\u5f88\u719f\u6089&#xff0c;epoll\u6709\u4e09\u4e2a\u5e38\u7528\u7684\u63a5\u53e3&#xff0c;\u5206\u522b\u662fepoll_create(int size)&#xff0c;epoll_ctl()&#xff0c;epoll_wait()&#xff0c;\u800c\u8fd9\u4e2aepollfd\u5c31\u662f\u7531epoll_create\/epoll_create1\u6240\u8fd4\u56de\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e5f\u5c31\u662ffd&#xff0c;epoll_create1\u662f\u65b0\u63a5\u53e3,\u53c2\u6570\u4e3aint flag&#xff0c;\u8fd9\u4e2aflag\u7528\u6765\u63a7\u5236\u884c\u4e3a&#xff0c;\u800c\u539f\u63a5\u53e3\u7684size\u5df2\u7ecf\u88ab\u5185\u6838\u6240\u5ffd\u7565\u3002<\/p>\n<p>3\u3001unordered\u662fSTL\u7684\u5e38\u7528\u5bb9\u5668&#xff0c;\u662f\u4e00\u4e2aKV\u7684\u5b58\u50a8\u7ed3\u6784&#xff0c;\u5e95\u5c42\u662f\u54c8\u5e0c\u8868&#xff0c;\u4e3a\u4ec0\u4e48\u8981\u7528\u8fd9\u4e2a\u5462&#xff1f;\u7528\u6765\u5b58\u50a8\u6240\u6709\u7684\u952e\u503c\u5bf9&#xff0c;\u4e0a\u9762\u8bf4\u4e86&#xff0c;Channel\u662f\u5bf9fd\u7684\u4e00\u4e2a\u5c01\u88c5&#xff0c;fd\u4e0echannel\u662f\u4e00\u4e00\u5bf9\u5e94\u7684\u5173\u7cfb&#xff0c;\u4e00\u4e2afd\u5bf9\u5e94\u4e00\u4e2aChannel&#xff0c;\u800c\u8fd9\u4e2achannels_\u5b58\u50a8\u4e86\u6240\u6709\u7684\u952e\u503c\u5bf9\u4e5f\u5c31\u662f\u6620\u5c04\u5173\u7cfb\u3002\u540e\u7eed\u5c31\u53ef\u4ee5\u901a\u8fc7\u67e5\u627echannels_\u627e\u5230\u5bf9\u5e94fd\u6240\u5bf9\u5e94\u7684Channel\u4e86\u3002<\/p>\n<p>4\u3001events_&#xff0c;\u7528\u4e8e\u5b58\u50a8epoll_wait\u5c31\u7eea\u4e8b\u4ef6\u7684\u6570\u7ec4&#xff0c;EventLoop\u4f1a\u904d\u5386\u8fd9\u4e2a\u6570\u7ec4&#xff0c;\u627e\u5230\u5df2\u7ecf\u5c31\u7eea\u7684\u4e8b\u4ef6&#xff0c;\u7136\u540e\u901a\u8fc7channels_\u627e\u5230\u5bf9\u5e94\u7684Channel&#xff0c;\u7136\u540e\u8c03\u7528\u5176\u56de\u8c03\u51fd\u6570\u5904\u7406\u8bfb\u5199\u7b49\u903b\u8f91<\/p>\n<h3>5\u3001EventLoop(\u4e8b\u4ef6\u5faa\u73af):<\/h3>\n<p>\u670d\u52a1\u5668\u7684\u6838\u5fc3&#xff0c;\u8be5\u670d\u52a1\u5668\u91c7\u7528\u7684\u662f\u4e3b\u4eceReactor&#xff0c;\u6bcf\u4e2aReactor\u90fd\u6709\u4e00\u4e2aEventLoop\u90fd\u5728\u8fd0\u884cloop()\u3002<\/p>\n<hr \/>\n<p>\u4f20\u7edf\u670d\u52a1\u5668\u7684\u672c\u8d28\u90fd\u662f\u5728\u6267\u884c\u4e00\u4e2a while \u5faa\u73af&#xff0c;\u4e0d\u65ad\u76d1\u542c\u3001\u5206\u53d1\u5e76\u5904\u7406 IO \u4e8b\u4ef6&#xff0c;\u800c\u8fd9\u91cc\u7684 EventLoop \u5c31\u76f8\u5f53\u4e8e\u5bf9\u8fd9\u5957\u5faa\u73af\u7684\u9ad8\u5ea6\u5c01\u88c5\u3002\u6240\u8c13\u4e3b\u4ece Reactor \u672c\u8d28\u5c31\u662f&#xff1a;MainReactor \u6267\u884c handleAccept() \u63a5\u6536\u65b0\u8fde\u63a5&#xff0c;\u76f8\u5f53\u4e8e\u4f20\u7edf\u670d\u52a1\u5668\u4e2d\u8c03\u7528 accept() \u8fd9\u4e2a\u51fd\u6570\u3002\u7136\u800c&#xff0c;<span style=\"color:#fe2c24\">\u4e0e\u5355\u7ebf\u7a0b\u670d\u52a1\u5668\u4e0d\u540c\u7684\u662f&#xff0c;MainReactor \u5e76\u4e0d\u8d1f\u8d23\u540e\u7eed\u7684\u8bfb\u5199 IO&#xff0c;\u800c\u662f\u5c06\u65b0\u5efa\u7acb\u7684\u8fde\u63a5\u901a\u8fc7\u8d1f\u8f7d\u5747\u8861\u7b56\u7565&#xff0c;\u5206\u53d1\u7ed9\u67d0\u4e2a SubReactor \u53bb\u76d1\u542c\u4e0e\u7ba1\u7406&#xff0c;\u5728\u8fd9\u4e2a\u670d\u52a1\u5668\u91cc\u5c31\u662f\u8c03\u7528handleNewConnection()\u51fd\u6570&#xff0c;\u8fd9\u4e5f\u5c31\u662f\u4e3a\u4ec0\u4e48\u6709\u4e86handleAccept()\u4e86\u8fd8\u8981\u6709handleNewConnection()\u3002<\/span>\u6bcf\u4e2a SubReactor \u62e5\u6709\u72ec\u7acb\u7684 EventLoop \u548c epoll \u5b9e\u4f8b&#xff0c;\u8d1f\u8d23\u5bf9\u5e94\u8fde\u63a5\u7684\u53ef\u8bfb\u3001\u53ef\u5199\u4e8b\u4ef6\u5206\u53d1\u4e0e\u5904\u7406&#xff0c;\u5b9e\u73b0\u4e86 IO \u591a\u8def\u590d\u7528\u4e0e\u591a\u7ebf\u7a0b\u5e76\u884c\u5904\u7406&#xff0c;\u4ece\u800c\u5927\u5e45\u63d0\u5347\u4e86\u6027\u80fd&#xff01;<\/p>\n<p>\u4ee5\u4e0b\u53ea\u7ed9\u51faEventLoop\u7684\u5934\u6587\u4ef6&#xff0c;\u5177\u4f53\u5b9e\u73b0\u53ef\u770b\u6e90\u7801:<\/p>\n<p>\/*<br \/>\nEventLoop\u662fAeroChat\u4e2dReactor\u6a21\u5f0f\u7684\u6838\u5fc3\u7ec4\u4ef6\u3002<br \/>\n\u6bcf\u4e2aEventLoop \u5bf9\u8c61\u7ed1\u5b9a\u4e00\u4e2a\u7ebf\u7a0b,\u5b9e\u73b0 &#034;One Loop Per Thread&#034; \u8bbe\u8ba1\u3002<br \/>\n\u5b83\u8d1f\u8d23&#xff1a;<br \/>\n\u9a71\u52a8\u4e8b\u4ef6\u5faa\u73af\u8c03\u7528Poller\u7684poll\u65b9\u6cd5\u83b7\u53d6\u6d3b\u8dc3Channel&#xff1b;<br \/>\n\u5206\u53d1 I\/O \u4e8b\u4ef6&#xff08;\u901a\u8fc7 Channel \u7684\u56de\u8c03&#xff09;<br \/>\n\u6267\u884c\u8de8\u7ebf\u7a0b\u6295\u9012\u7684\u4efb\u52a1&#xff08;\u901a\u8fc7\u65e0\u9501\u961f\u5217 pendingFunctors_&#xff09;\u3002<br \/>\n *\/<br \/>\n#ifndef EVENTLOOP_HPP<br \/>\n#define EVENTLOOP_HPP<\/p>\n<p>#include &lt;atomic&gt;<br \/>\n#include &lt;functional&gt;<br \/>\n#include &lt;memory&gt;<br \/>\n#include &lt;mutex&gt;<br \/>\n#include &lt;thread&gt;<br \/>\n#include &lt;vector&gt;<br \/>\n#include &lt;sys\/eventfd.h&gt;   \/\/ for eventfd<br \/>\n#include &lt;unistd.h&gt;         \/\/ for close<\/p>\n<p>#include &#034;Channel.hpp&#034;<br \/>\n#include &#034;Poller.hpp&#034;<br \/>\n#include &#034;concurrentqueue.hpp&#034;<\/p>\n<p>class EventLoop {<br \/>\npublic:<br \/>\n  EventLoop();<br \/>\n  ~EventLoop();<\/p>\n<p>  \/\/ \u542f\u52a8\u4e8b\u4ef6\u5faa\u73af&#xff08;\u963b\u585e&#xff09;<br \/>\n  void loop();<\/p>\n<p>  \/\/ \u8bf7\u6c42\u9000\u51fa\u4e8b\u4ef6\u5faa\u73af<br \/>\n  void quit();<\/p>\n<p>  \/\/ \u66f4\u65b0 Channel \u5728 Poller \u4e2d\u7684\u6ce8\u518c\u72b6\u6001&#xff08;\u65b0\u589e\u3001\u4fee\u6539\u6216\u5220\u9664&#xff09;<br \/>\n  void updateChannel(Channel *channel);<br \/>\n  void removeChannel(Channel *channel);<\/p>\n<p>  \/\/ \u8de8\u7ebf\u7a0b\u4efb\u52a1\u6295\u9012<br \/>\n  void runInLoop(<br \/>\n      const std::function&lt;void()&gt; &amp;cb); \/\/ \u82e5\u5728\u5f53\u524d\u7ebf\u7a0b\u5219\u76f4\u63a5\u6267\u884c&#xff0c;\u5426\u5219\u5165\u961f<br \/>\n  void queueInLoop(const std::function&lt;void()&gt; &amp;cb); \/\/ \u603b\u662f\u5165\u961f<\/p>\n<p>  \/\/ \u5224\u65ad\u8c03\u7528\u8005\u662f\u5426\u5728 EventLoop \u6240\u5c5e\u7ebf\u7a0b\u4e2d<br \/>\n  bool isInLoopThread() const {<br \/>\n    return threadId_ &#061;&#061; std::this_thread::get_id();<br \/>\n  }<\/p>\n<p>  \/\/ \u7d22\u5f15&#xff08;\u7528\u4e8e\u533a\u5206\u4e3b Reactor \u548c\u5b50 Reactor&#xff09;<br \/>\n  int getIndex() const { return index_; }<br \/>\n  void setIndex(int idx) { index_ &#061; idx; }<\/p>\n<p>private:<br \/>\n  \/\/ \u6267\u884c\u6240\u6709\u5f85\u5904\u7406\u7684\u8de8\u7ebf\u7a0b\u56de\u8c03<br \/>\n  void doPendingFunctors();<\/p>\n<p>  \/\/ \u5524\u9192 EventLoop \u7ebf\u7a0b&#xff08;\u5f53\u6709\u4efb\u52a1\u5165\u961f\u4e14\u4e0d\u5728\u672c\u7ebf\u7a0b\u65f6\u8c03\u7528&#xff09;<br \/>\n  void wakeup();<br \/>\n  \/\/ eventfd \u8bfb\u4e8b\u4ef6\u56de\u8c03&#xff0c;\u7528\u4e8e\u6e05\u7a7a\u5524\u9192\u6807\u5fd7<br \/>\n  void handleWakeup();<\/p>\n<p>  std::atomic&lt;bool&gt; looping_; \/\/ \u662f\u5426\u6b63\u5728\u5faa\u73af<br \/>\n  std::atomic&lt;bool&gt; quit_;    \/\/ \u662f\u5426\u9000\u51fa<br \/>\n  int index_; \/\/ \u6807\u8bc6&#xff08;\u4e3b Reactor \u4e3a 0&#xff0c;\u5b50 Reactor \u4e3a 1..N&#xff09;<br \/>\n  std::thread::id threadId_; \/\/ \u6240\u5c5e\u7ebf\u7a0b ID<br \/>\n  moodycamel::ConcurrentQueue&lt;std::function&lt;void()&gt;&gt;<br \/>\n      pendingFunctors_;         \/\/ \u65e0\u9501\u961f\u5217&#xff0c;\u5b58\u50a8\u8de8\u7ebf\u7a0b\u4efb\u52a1<br \/>\n  bool callingPendingFunctors_; \/\/ \u662f\u5426\u6b63\u5728\u6267\u884c\u5f85\u5904\u7406\u56de\u8c03(\u9632\u6b62\u91cd\u5165)<\/p>\n<p>  std::unique_ptr&lt;Poller&gt; poller_; \/\/ I\/O\u591a\u8def\u590d\u7528\u5668(\u5f53\u524d\u4e3aepoll)<\/p>\n<p>  \/\/ \u5524\u9192\u673a\u5236\u76f8\u5173<br \/>\n  int wakeupFd_;                               \/\/ eventfd \u6587\u4ef6\u63cf\u8ff0\u7b26<br \/>\n  std::unique_ptr&lt;Channel&gt; wakeupChannel_;     \/\/ \u5c01\u88c5 wakeupFd_ \u7684 Channel<br \/>\n};<\/p>\n<p>#endif \/\/ EVENTLOOP_HPP <\/p>\n<h3>6\u3001EventLoopThread:<\/h3>\n<p>\u8fd9\u4e2a\u7c7b\u5c31\u662f\u5b9e\u73b0One Loop Per Thead\u7684\u6838\u5fc3:<\/p>\n<p>\/*<br \/>\none loop per thread\u7684\u5177\u4f53\u5b9e\u73b0<br \/>\nEventLoopThread\u7684\u76ee\u7684\u5c31\u662f\u5c06EventLoop\u5bf9\u8c61\u4e0e\u4e00\u4e2a\u72ec\u7acb\u7ebf\u7a0b\u7ed1\u5b9a,\u4f7f\u8be5\u7ebf\u7a0b\u8fd0\u884c\u4e8b\u4ef6\u5faa\u73af,\u5b9e\u73b0\u4e00\u4e2a\u7ebf\u7a0b<br \/>\n\u4e00\u4e2a\u4e8b\u4ef6\u5faa\u73af<br \/>\n\u8fd9\u4e2a\u7c7b\u4e3b\u8981\u8d1f\u8d23:<br \/>\n1\u3001\u521b\u5efa\u7ebf\u7a0b,\u5728\u7ebf\u7a0b\u4e2d\u6784\u9020EventLoop\u5bf9\u8c61(\u6808\u4e0a)<br \/>\n2\u3001\u542f\u52a8\u65f6\u95f4\u5faa\u73af,\u76f4\u5230\u7ebf\u7a0b\u9000\u51fa<br \/>\n3\u3001\u63d0\u4f9b\u540c\u6b65\u673a\u5236&#xff0c;\u786e\u4fdd\u8c03\u7528\u8005\u80fd\u83b7\u5f97\u5df2\u7ecf\u5c31\u7eea\u7684EventLoop\u6307\u9488<br \/>\nEventLoopThread\u662f\u5982\u4f55\u4e0eThread\u5efa\u7acb\u8054\u7cfb\u7684&#xff1f;<\/p>\n<p>thread_.start() \u5185\u90e8\u8c03\u7528 pthread_create(&amp;tid_, nullptr, runInThread, this)\u3002<\/p>\n<p>\u65b0\u7ebf\u7a0b\u542f\u52a8\u540e,\u6267\u884c runInThread(this)\u3002<\/p>\n<p>runInThread\u5c06this \u8f6c\u56de Thread*\u5e76\u8c03\u7528 thread-&gt;func_(),<br \/>\n\u4e5f\u5c31\u662f\u6267\u884c EventLoopThread::threadFunc\u3002<br \/>\n\u4e8e\u662f,EventLoopThread::threadFunc\u5c31\u5728\u5b50\u7ebf\u7a0b\u4e2d\u8fd0\u884c,\u521b\u5efa\u5e76\u8fd0\u884cEventLoop\u3002<\/p>\n<p>*\/<br \/>\n#ifndef EVENTLOOPTHREAD_HPP<br \/>\n#define EVENTLOOPTHREAD_HPP<br \/>\n#include &#034;Thread.hpp&#034;<br \/>\n#include &#034;EventLoop.hpp&#034;<br \/>\n#include &lt;condition_variable&gt;<br \/>\n#include &lt;mutex&gt;<br \/>\n#include &lt;string&gt;<\/p>\n<p>class EventLoopThread : noncopyable {<br \/>\npublic:<br \/>\n    explicit EventLoopThread(const std::string&amp; name &#061; std::string());<br \/>\n    ~EventLoopThread();<\/p>\n<p>    EventLoop* startLoop();\/\/\u542f\u52a8\u7ebf\u7a0b,\u8fd4\u56de\u5173\u8054\u7684EventLoop\u6307\u9488<\/p>\n<p>private:<br \/>\n    void threadFunc();\/\/\u7ebf\u7a0b\u51fd\u6570<\/p>\n<p>    EventLoop* loop_;\/\/\u6307\u5411\u672c\u7ebf\u7a0b\u4e2d\u6240\u521b\u5efa\u7684EventLoop\u5bf9\u8c61<br \/>\n    bool exiting_;\/\/\u662f\u5426\u9000\u51fa<br \/>\n    Thread thread_;\/\/\u5bf9\u5e94\u7684\u7ebf\u7a0b<br \/>\n    std::mutex mutex_;\/\/\u4e92\u65a5\u9501<br \/>\n    std::condition_variable cond_;\/\/\u6761\u4ef6\u53d8\u91cf<br \/>\n};<\/p>\n<p>#endif \/\/ EVENTLOOPTHREAD_HPP<\/p>\n<p>\u4e3a\u4ec0\u4e48\u8bf4\u8fd9\u4e2a\u7c7b\u662f\u5b9e\u73b0One Loop Per Thread\u7684\u6838\u5fc3&#xff1f;\u6765\u770b\u4e00\u4e0b\u8fd9\u4e2a\u7c7b\u7684\u6210\u5458\u51fd\u6570:<\/p>\n<p>EventLoopThread::EventLoopThread(const std::string&amp; name)<br \/>\n    : loop_(nullptr), exiting_(false), thread_(std::bind(&amp;EventLoopThread::threadFunc, this), name) {}<br \/>\n   \/\/\u521b\u5efaThread\u5bf9\u8c61,\u7ebf\u7a0b\u51fd\u6570\u4e3aEventLoopThread::threadFunc,\u5e76\u4f20\u5165this\u6307\u9488\u4ee5\u4fbf\u5728\u7ebf\u7a0b\u51fd\u6570\u4e2d\u8bbf\u95ee\u5f53\u524d<br \/>\n   \/\/\u5bf9\u8c61,name\u662f\u7ebf\u7a0b\u540d\u79f0<br \/>\n   \/\/\u6ce8\u610f:\u6b64\u65f6\u7ebf\u7a0b\u8fd8\u6ca1\u6709\u542f\u52a8,thread_\u5bf9\u8c61\u53ea\u662f\u6784\u9020\u5b8c\u6574,thread_.start()\u5e76\u6ca1\u6709\u8fdb\u884c\u8c03\u7528<br \/>\nEventLoopThread::~EventLoopThread() {<br \/>\n    exiting_ &#061; true;<br \/>\n    if (loop_) {<br \/>\n        loop_-&gt;quit();<br \/>\n        thread_.join();<br \/>\n    }<br \/>\n}<\/p>\n<p>EventLoop* EventLoopThread::startLoop() {<br \/>\n    thread_.start();\/\/\u542f\u52a8\u5e95\u5c42\u7ebf\u7a0b,\u7ebf\u7a0b\u5165\u53e3\u51fd\u662fthreadFunc<br \/>\n    std::unique_lock&lt;std::mutex&gt; lock(mutex_);<br \/>\n    cond_.wait(lock, [this]() { return loop_ !&#061; nullptr; });\/\/\u5e26\u8c13\u8bcd\u7684\u5b89\u5168\u7248\u672c,\u7b2c\u4e8c\u4e2a\u53c2\u6570\u8fd4\u56de\u503c\u4e3abool\u7c7b\u578b,\u662f\u4e00\u4e2alambda\u8868\u8fbe\u5f0f<br \/>\n    return loop_;\/\/\u4e00\u65e6\u5b50\u7ebf\u7a0b\u5b8c\u6210\u521d\u59cb\u5316\u5e76\u901a\u77e5\u6761\u4ef6\u53d8\u91cf,startLoop()\u8fd4\u56de\u6307\u5411\u8be5EventLoop\u7684\u6307\u9488<br \/>\n    \/\/\u6b64\u540e\u5916\u90e8\u5c31\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e2a\u6307\u9488\u8fdb\u884c\u8de8\u7ebf\u7a0b\u8c03\u7528(\u5982runInLoop)<br \/>\n}<\/p>\n<p>void EventLoopThread::threadFunc() {<br \/>\n    EventLoop loop;\/\/\u6ce8\u610f\u8fd9\u91cc\u5e76\u4e0d\u662fnew\u800c\u662f\u5728\u6808\u4e0a\u521b\u5efa\u5bf9\u8c61<br \/>\n    static int idx &#061; 1;<br \/>\n    loop.setIndex(idx&#043;&#043;);\/\/\u8bbe\u7f6e\u7d22\u5f15<\/p>\n<p>    {<br \/>\n        std::lock_guard&lt;std::mutex&gt; lock(mutex_);<br \/>\n        loop_ &#061; &amp;loop;\/\/\u628a\u6808\u5bf9\u8c61\u7684\u5730\u5740\u8d4b\u503c\u7ed9\u6210\u5458\u53d8\u91cf<br \/>\n        cond_.notify_one();\/\/\u901a\u77e5startLoop\u5df2\u7ecf\u5c31\u7eea<br \/>\n    }<br \/>\n    LOG_INFO(&#034;[SUCCESS] Sub Reactor \\\\&#034;&#034; &#043; thread_.name() &#043; &#034;\\\\&#034; (\u7ebf\u7a0bID: &#034; &#043;<br \/>\n             std::to_string(std::hash&lt;std::thread::id&gt;{}(std::this_thread::get_id())) &#043; &#034;) \u5df2\u542f\u52a8&#034;);<br \/>\n    loop.loop();\/\/\u8fdb\u5165\u4e8b\u4ef6\u5faa\u73af<br \/>\n    \/\/\u8c03\u7528 EventLoop::loop(),\u8be5\u51fd\u6570\u5185\u90e8\u662f\u4e00\u4e2a\u65e0\u9650\u5faa\u73af(\u77e5\u9053quit()\u88ab\u8c03\u7528),\u5904\u7406 I\/O \u4e8b\u4ef6\u548c\u8de8\u7ebf\u7a0b\u56de\u8c03\u3002<br \/>\n    std::lock_guard&lt;std::mutex&gt; lock(mutex_);<br \/>\n    loop_ &#061; nullptr;\/\/\u5faa\u73af\u7ed3\u675f,\u8fdb\u884c\u6e05\u7406<br \/>\n} <\/p>\n<p>threadFunc()\u5728\u6808\u4e0a\u521b\u5efaEventLoop\u5bf9\u8c61&#xff0c;\u8fd9\u6837\u5c31\u6709\u4e00\u4e2a\u597d\u5904&#xff0c;\u751f\u547d\u5468\u671f\u4e0e\u7ebf\u7a0b\u7ed1\u5b9a&#xff0c;\u7ebf\u7a0b\u9000\u51fa\u65f6\u81ea\u52a8\u9500\u6bc1&#xff0c;\u65e0\u9700\u624b\u52a8\u8fdb\u884cdelete\u3002<\/p>\n<p>\u6bcf\u4e2aEventLoopThread\u5b9e\u4f8b\u552f\u4e00\u5bf9\u5e94\u4e00\u4e2a\u5b50\u7ebf\u7a0b&#xff0c;\u4e14\u8be5\u7ebf\u7a0b\u53ea\u8fd0\u884c\u8fd9\u4e2aEventLoop&#xff0c;\u5b9e\u73b0\u4e86One Loop Per Thread\u3002<\/p>\n<h3>7\u3001EventLoopThreadPool:<\/h3>\n<p>EventLoopThreadPool\u00a0\u521b\u5efa\u5e76\u6301\u6709\u591a\u4e2a\u00a0EventLoopThread&#xff0c;\u6bcf\u4e2a\u00a0EventLoopThread\u00a0\u542f\u52a8\u540e\u4ea7\u751f\u4e00\u4e2a\u5b50\u7ebf\u7a0b\u53ca\u5176\u00a0EventLoop\u3002<\/p>\n<p>\/*<br \/>\nEventLoopThreadPool\u662fAeroChat\u4e2d\u7ba1\u7406\u591a\u4e2a\u5b50Reactor\u7ebf\u7a0b\u7684\u6838\u5fc3\u7c7b,\u5b83\u5c01\u88c5\u4e86\u591a\u4e2aEventLoopThead,\u5bf9\u5916\u63d0\u4f9b<br \/>\n\u7edf\u4e00\u7684\u63a5\u53e3\u6765\u83b7\u53d6\u5b50Reactor\u7684EventLoop,\u8d1f\u8d23\u65b0\u8fde\u63a5\u7684\u8d1f\u8f7d\u5747\u8861<br \/>\n*\/<br \/>\n#ifndef EVENTLOOPTHREADPOOL_HPP<br \/>\n#define EVENTLOOPTHREADPOOL_HPP<\/p>\n<p>#include &lt;vector&gt;<br \/>\n#include &lt;memory&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &#034;EventLoop.hpp&#034;<br \/>\n#include &#034;EventLoopThread.hpp&#034;<br \/>\n#include&lt;atomic&gt;<\/p>\n<p>class EventLoopThreadPool {<br \/>\npublic:<br \/>\n    EventLoopThreadPool(EventLoop* baseLoop, int numThreads);<br \/>\n    ~EventLoopThreadPool() &#061; default;<\/p>\n<p>    void start();\/\/\u5f00\u542f\u6240\u4ee5\u7ebf\u7a0b,\u8fdb\u884cloop<br \/>\n    EventLoop* getNextLoop();          \/\/ \u65e0\u53c2\u83b7\u53d6\u4e0b\u4e00\u4e2aloop<br \/>\n    EventLoop* getLoop(int idx);       \/\/ \u6839\u636e\u7d22\u5f15\u83b7\u53d6\u6307\u5b9aloop<br \/>\n    int getThreadNum() const;          \/\/ \u83b7\u53d6\u7ebf\u7a0b\u6570&#xff08;\u4ec5\u58f0\u660e&#xff09;<br \/>\n    std::vector&lt;EventLoop*&gt; getAllLoops() const; \/\/ \u83b7\u53d6\u6240\u6709loop&#xff08;\u4ec5\u58f0\u660e&#xff09;<\/p>\n<p>private:<br \/>\n    EventLoop* baseLoop_;              \/\/ \u4e3bReactor\u7684loop<br \/>\n    int numThreads_;                   \/\/ \u5b50Reactor\u6570\u91cf<br \/>\n    std::atomic&lt;int&gt;next_{0};                         \/\/ \u8f6e\u8be2\u7d22\u5f15<br \/>\n    bool started_;                     \/\/ \u662f\u5426\u542f\u52a8<br \/>\n    std::vector&lt;std::unique_ptr&lt;EventLoopThread&gt;&gt; threads_; \/\/ \u5b50\u7ebf\u7a0b\u5217\u8868<br \/>\n    std::vector&lt;EventLoop*&gt; loops_;    \/\/ \u5b50loop\u5217\u8868 \u5b58\u50a8\u6240\u4ee5EventLoop\u5bf9\u5e94\u7684\u6307\u9488<br \/>\n};<\/p>\n<p>#endif \/\/ EVENTLOOPTHREADPOOL_HPP <\/p>\n<h2>\u4e09\u3001\u670d\u52a1\u5668\u8fd0\u884c\u6d41\u7a0b:<\/h2>\n<p>\u770b\u4e86\u4e0a\u9762\u7684\u5404\u4e2a\u7ec4\u4ef6\u53ef\u80fd\u5e76\u4e0d\u80fd\u6574\u4e2a\u670d\u52a1\u5668\u7684\u6d41\u7a0b&#xff0c;\u4e0b\u9762\u6211\u5c06\u4ecemain\u51fd\u6570\u5165\u624b&#xff0c;\u8be6\u7ec6\u8bb2\u89e3\u4e00\u4e0b\u8fd9\u4e2a\u670d\u52a1\u5668\u7684\u8fd0\u884c\u6d41\u7a0b&#xff0c;\u4ee5\u4fbf\u4e8e\u5927\u5bb6\u7406\u89e3\u3002<\/p>\n<p>int main(int argc, char* argv[]) {<br \/>\n    signal(SIGPIPE, SIG_IGN);<\/p>\n<p>    if (argc &lt; 2) {<br \/>\n        printf(&#034;Usage: %s port\\\\n&#034;, argv[0]);<br \/>\n        return 1;<br \/>\n    }<\/p>\n<p>    uint16_t port &#061; static_cast&lt;uint16_t&gt;(atoi(argv[1]));<br \/>\n    EventLoop loop; \/\/\u521b\u5efa\u4e00\u4e2aEventLoop\u5bf9\u8c61loop<br \/>\n    int subReactorNum &#061; std::thread::hardware_concurrency();\/\/\u7531\u4e8e\u6211\u7684\u670d\u52a1\u5668\u662f8\u68388GB,\u6240\u4ee5\u8fd9\u91cc\u7684<br \/>\n    \/\/subReactorNum&#061;\u6211\u7684cpu\u6838\u5fc3\u6570\u4e5f\u5c31\u662f\u7b49\u4e8e8<br \/>\n    ChatServer server(&amp;loop, port, subReactorNum);<\/p>\n<p>    server.start();\/\/\u542f\u52a8\u670d\u52a1\u5668<br \/>\n    loop.loop();\/\/\u5f00\u59cbloop<br \/>\nprintf(&#034;\u670d\u52a1\u5668\u542f\u52a8\u6210\u529f&#xff01;\\\\n&#034;);<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<p>\u9996\u5148\u770b\u670d\u52a1\u5668\u4e3b\u51fd\u6570&#xff0c;\u9996\u5148\u521b\u5efa\u4e00\u4e2aEventLoop\u4e5f\u5c31\u662f\u4e3bEventLoop,\u7136\u540e\u521b\u5efa\u4e00\u4e2aserver\u5bf9\u8c61&#xff0c;\u6267\u884cChatServer::start()\u3002<\/p>\n<p>void ChatServer::start() {<br \/>\n    threadPool_-&gt;start();<\/p>\n<p>    listenFd_ &#061; sock_.Socket();<br \/>\n    int reuse &#061; 1;<br \/>\n    setsockopt(listenFd_, SOL_SOCKET, SO_REUSEADDR, &amp;reuse, sizeof(reuse));<br \/>\n    setsockopt(listenFd_, SOL_SOCKET, SO_REUSEPORT, &amp;reuse, sizeof(reuse));<br \/>\n    fcntl(listenFd_, F_SETFL, O_NONBLOCK | FD_CLOEXEC);<br \/>\n    sock_.Bind(listenFd_, port_);<br \/>\n    sock_.Listen(listenFd_, 8192);<\/p>\n<p>    acceptChannel_ &#061; std::make_unique&lt;Channel&gt;(loop_, listenFd_);<br \/>\n    acceptChannel_-&gt;setReadCallback([this] { handleAccept(); });<br \/>\n    acceptChannel_-&gt;enableReading();<br \/>\n    acceptChannel_-&gt;enableET();<br \/>\n    loop_-&gt;updateChannel(acceptChannel_.get());<\/p>\n<p>    LOG_INFO(&#034;[EchoServer] Started on port &#034; &#043; std::to_string(port_));<br \/>\n}<\/p>\n<p>\u7136\u540e\u6267\u884cthreadPool_.start()\u3002<\/p>\n<p>void EventLoopThreadPool::start() {<br \/>\n   \/\/\u542f\u52a8\u6240\u6709Reactor\u5bf9\u5e94\u7684\u7ebf\u7a0b,\u5f00\u542f\u4e8b\u4ef6\u5faa\u73af(loop)<br \/>\n    if (started_) return;<br \/>\n    started_ &#061; true;<br \/>\n    for (int i &#061; 0; i &lt; numThreads_; &#043;&#043;i) {<br \/>\n        auto t &#061; std::make_unique&lt;EventLoopThread&gt;(&#034;SubReactor-&#034; &#043; std::to_string(i));<br \/>\n        EventLoop* subLoop &#061; t-&gt;startLoop();<br \/>\n        loops_.push_back(subLoop);<br \/>\n        threads_.push_back(std::move(t));<br \/>\n    }<br \/>\n}<\/p>\n<p>\u8fd9\u4e2a\u51fd\u6570\u4f1a\u5f00\u542f\u6240\u6709\u7684EventLoop,\u4e00\u4e2aSubReactor\/SubLoop\u5bf9\u5e94\u4e00\u4e2a\u7ebf\u7a0b\u3002t\u662f\u4e00\u4e2astd::unique_ptr&lt;EventLoopThread&gt;\u00a0\u5bf9\u8c61&#xff0c;\u7136\u540e\u901a\u8fc7\u8fd9\u4e2a\u667a\u80fd\u6307\u9488\u53bb\u8c03\u7528EventLoopThread::startLoop()&#xff0c;\u90a3\u4e48startLoop()\u00a0\u5185\u90e8\u5177\u4f53\u662f\u5982\u4f55\u5f00\u542f\u4e00\u4e2a\u7ebf\u7a0b\u5e76\u8fd0\u884c\u4e8b\u4ef6\u5faa\u73af\u7684\u5462&#xff1f;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"251\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260724201848-6a63c8a830be7.png\" width=\"1915\" \/><\/p>\n<p>thread_\u662f\u4e00\u4e2aThread\u7c7b\u7684\u5bf9\u8c61&#xff0c;\u6240\u4ee5\u6211\u4eec\u770b\u4e00\u4e0bThread::start()\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"210\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260724201848-6a63c8a861f46.png\" width=\"1714\" \/><\/p>\n<p>\u8fd9\u4e2aThread\u7c7b\u7684\u6784\u9020\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u3002\u88ab\u7ed1\u5b9a\u5230\u4e86threadFunc&#xff0c;\u4e5f\u5c31\u662f\u8bf4Thread\u6784\u9020\u65f6\u5c31\u4f1a\u56de\u8c03threadFunc\u8fd9\u4e2a\u56de\u8c03\u51fd\u6570\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"545\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260724201848-6a63c8a88d39e.png\" width=\"1652\" \/><\/p>\n<p>\u5982\u4e0a\u5c31\u5b9e\u73b0\u4e86One Loop Per Thread&#xff0c;\u7136\u540e\u4e3b\u7ebf\u7a0b\u518d\u6267\u884cloop()\u3002\u5f53\u6709\u65b0\u8fde\u63a5\u5230\u6765\u7684\u65f6\u5019\u5c31\u56de\u8c03handleAcceptor&#xff0c;\u4e3b\u7ebf\u7a0b\u53ea\u8d1f\u8d23\u63a5\u6536\u65b0\u8fde\u63a5\u7136\u540e\u901a\u8fc7\u8f6e\u8be2\u7684\u65b9\u5f0f\u5c06\u8fd9\u4e9b\u65b0\u8fde\u63a5\u5206\u53d1\u7ed9\u4e0d\u540c\u7684\u5b50Reactor\/\u5b50Loop,\u5b50Reactor\u53bb\u6267\u884chandleNewConnection()\u3002\u8fd9\u5c31\u662f\u4e3b\u4eceReactor&#xff01;\u4e5f\u662f\u8be5\u670d\u52a1\u5668\u9ad8\u6027\u80fd\u7684\u5173\u952e\u6240\u5728\u3002<\/p>\n<p>\u66f4\u591a\u7ec6\u8282\u53ef\u67e5\u770b\u6e90\u7801:<\/p>\n<p>AeroChat\/benchmark at master \u00b7 ywx914705\/AeroChat<\/p>\n<p>\u5b8c\u6574\u9879\u76ee(\u9ad8\u6027\u80fd\u804a\u5929\u5ba4\u670d\u52a1\u5668&#xff09;\u6e90\u7801&#xff1a;<\/p>\n<p>ywx914705\/AeroChat: AeroChat \u662f\u4e00\u4e2a\u4f7f\u7528 C&#043;&#043;11 \u5f00\u53d1\u7684\u9ad8\u5e76\u53d1\u804a\u5929\u670d\u52a1\u5668&#xff0c;\u91c7\u7528 **\u4e3b\u4ece Reactor &#043; \u5f02\u6b65\u4efb\u52a1\u961f\u5217** \u67b6\u6784&#xff0c;\u652f\u6301\u5355\u673a\u6570\u4e07\u5e76\u53d1\u8fde\u63a5\u3002\u9879\u76ee\u5b9e\u73b0\u4e86\u5b8c\u6574\u7684\u7528\u6237\u8ba4\u8bc1\u3001\u79c1\u804a\u3001\u7fa4\u804a\u3001\u79bb\u7ebf\u6d88\u606f\u3001\u4f1a\u8bdd\u7ba1\u7406\u7b49\u529f\u80fd&#xff0c;\u5e76\u96c6\u6210\u4e86 MySQL \u4e0e Redis \u4f5c\u4e3a\u5b58\u50a8\u5c42\u3002<\/p>\n<p><span style=\"color:#fe2c24\">\u9879\u76ee\u5df2\u5f00\u6e90,\u914d\u5957\u5b8c\u6574\u7684README&#xff0c;\u89c9\u5f97\u5199\u7684\u4e0d\u9519\u7684\u670b\u53cb\u53ef\u4ee5\u5e2e\u5fd9star\u4e00\u4e0b&#xff0c;\u8c22\u8c22&#xff01;&#xff01;&#xff01;<\/span><\/p>\n<h2 style=\"background-color:transparent\">\u56db\u3001\u53c2\u8003\u8d44\u6599&#xff1a;<\/h2>\n<ul>\n<li>\n<p>muduo \u7f51\u7edc\u5e93<\/p>\n<\/li>\n<li>\n<p>The C10K problem<\/p>\n<\/li>\n<li>\n<p>epoll \u5b98\u65b9\u624b\u518c<\/p>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5f15\u8a00<br \/>\n\u5728\u9ad8\u5e76\u53d1\u7f51\u7edc\u7f16\u7a0b\u9886\u57df&#xff0c;Reactor \u6a21\u578b\u51ed\u501f\u5176\u9ad8\u6548\u7684\u4e8b\u4ef6\u9a71\u52a8\u673a\u5236&#xff0c;\u6210\u4e3a\u6784\u5efa\u9ad8\u6027\u80fd\u670d\u52a1\u5668\u7684\u9996\u9009\u65b9\u6848\u3002\u672c\u6587\u5c06\u4ece\u96f6\u5f00\u59cb&#xff0c;\u8be6\u7ec6\u5256\u6790\u4e00\u4e2a\u57fa\u4e8e\u00a0one loop per thread\u00a0\u67b6\u6784\u7684 C TCP Echo \u670d\u52a1\u5668\u7684\u5b9e\u73b0\u3002\u8be5\u670d\u52a1\u5668\u53c2\u8003\u4e86 muduo \u7f51\u7edc\u5e93\u7684\u8bbe\u8ba1\u601d\u60f3&#xff0c;\u652f\u6301\u591a\u6838\u6269\u5c55\u3001\u975e\u963b\u585e I\/O\u3001ET \u6a21\u5f0f&#xff0c;\u5e76\u91c7\u7528\u5f02\u6b65\u65e5\u5fd7\u548c\u65e0\u9501\u961f\u5217\u8fdb\u884c\u4f18\u5316\u3002\u6700\u7ec8\u5b9e\u73b0\u4e86\u4e00\u4e2a\u53ef\u652f\u6491\u6570\u6570\u4e07\u5e76\u53d1\u8fde\u63a5(\u53d6\u51b3\u4e8e\u786c\u4ef6\u914d\u7f6e)\u7684\u9ad8\u6027\u80fd <\/p>\n","protected":false},"author":2,"featured_media":81759,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,58,205,947,43],"topic":[],"class_list":["post-81764","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-c","tag-linux","tag-tcp-ip","tag-websocket","tag-43"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u4ece\u96f6\u6784\u5efa\u9ad8\u6027\u80fd Reactor \u670d\u52a1\u5668\uff1a - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/81764.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u4ece\u96f6\u6784\u5efa\u9ad8\u6027\u80fd Reactor \u670d\u52a1\u5668\uff1a - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u5f15\u8a00 \u5728\u9ad8\u5e76\u53d1\u7f51\u7edc\u7f16\u7a0b\u9886\u57df&#xff0c;Reactor \u6a21\u578b\u51ed\u501f\u5176\u9ad8\u6548\u7684\u4e8b\u4ef6\u9a71\u52a8\u673a\u5236&#xff0c;\u6210\u4e3a\u6784\u5efa\u9ad8\u6027\u80fd\u670d\u52a1\u5668\u7684\u9996\u9009\u65b9\u6848\u3002\u672c\u6587\u5c06\u4ece\u96f6\u5f00\u59cb&#xff0c;\u8be6\u7ec6\u5256\u6790\u4e00\u4e2a\u57fa\u4e8e\u00a0one loop per thread\u00a0\u67b6\u6784\u7684 C TCP Echo \u670d\u52a1\u5668\u7684\u5b9e\u73b0\u3002\u8be5\u670d\u52a1\u5668\u53c2\u8003\u4e86 muduo \u7f51\u7edc\u5e93\u7684\u8bbe\u8ba1\u601d\u60f3&#xff0c;\u652f\u6301\u591a\u6838\u6269\u5c55\u3001\u975e\u963b\u585e I\/O\u3001ET \u6a21\u5f0f&#xff0c;\u5e76\u91c7\u7528\u5f02\u6b65\u65e5\u5fd7\u548c\u65e0\u9501\u961f\u5217\u8fdb\u884c\u4f18\u5316\u3002\u6700\u7ec8\u5b9e\u73b0\u4e86\u4e00\u4e2a\u53ef\u652f\u6491\u6570\u6570\u4e07\u5e76\u53d1\u8fde\u63a5(\u53d6\u51b3\u4e8e\u786c\u4ef6\u914d\u7f6e)\u7684\u9ad8\u6027\u80fd\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/81764.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-07-24T20:18:49+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260724201847-6a63c8a791499.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/81764.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/81764.html\",\"name\":\"\u4ece\u96f6\u6784\u5efa\u9ad8\u6027\u80fd Reactor \u670d\u52a1\u5668\uff1a - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-07-24T20:18:49+00:00\",\"dateModified\":\"2026-07-24T20:18:49+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/81764.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/81764.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/81764.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u4ece\u96f6\u6784\u5efa\u9ad8\u6027\u80fd Reactor \u670d\u52a1\u5668\uff1a\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u4ece\u96f6\u6784\u5efa\u9ad8\u6027\u80fd Reactor \u670d\u52a1\u5668\uff1a - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/81764.html","og_locale":"zh_CN","og_type":"article","og_title":"\u4ece\u96f6\u6784\u5efa\u9ad8\u6027\u80fd Reactor \u670d\u52a1\u5668\uff1a - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u5f15\u8a00 \u5728\u9ad8\u5e76\u53d1\u7f51\u7edc\u7f16\u7a0b\u9886\u57df&#xff0c;Reactor \u6a21\u578b\u51ed\u501f\u5176\u9ad8\u6548\u7684\u4e8b\u4ef6\u9a71\u52a8\u673a\u5236&#xff0c;\u6210\u4e3a\u6784\u5efa\u9ad8\u6027\u80fd\u670d\u52a1\u5668\u7684\u9996\u9009\u65b9\u6848\u3002\u672c\u6587\u5c06\u4ece\u96f6\u5f00\u59cb&#xff0c;\u8be6\u7ec6\u5256\u6790\u4e00\u4e2a\u57fa\u4e8e\u00a0one loop per thread\u00a0\u67b6\u6784\u7684 C TCP Echo \u670d\u52a1\u5668\u7684\u5b9e\u73b0\u3002\u8be5\u670d\u52a1\u5668\u53c2\u8003\u4e86 muduo \u7f51\u7edc\u5e93\u7684\u8bbe\u8ba1\u601d\u60f3&#xff0c;\u652f\u6301\u591a\u6838\u6269\u5c55\u3001\u975e\u963b\u585e I\/O\u3001ET \u6a21\u5f0f&#xff0c;\u5e76\u91c7\u7528\u5f02\u6b65\u65e5\u5fd7\u548c\u65e0\u9501\u961f\u5217\u8fdb\u884c\u4f18\u5316\u3002\u6700\u7ec8\u5b9e\u73b0\u4e86\u4e00\u4e2a\u53ef\u652f\u6491\u6570\u6570\u4e07\u5e76\u53d1\u8fde\u63a5(\u53d6\u51b3\u4e8e\u786c\u4ef6\u914d\u7f6e)\u7684\u9ad8\u6027\u80fd","og_url":"https:\/\/www.wsisp.com\/helps\/81764.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-07-24T20:18:49+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260724201847-6a63c8a791499.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"8 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/81764.html","url":"https:\/\/www.wsisp.com\/helps\/81764.html","name":"\u4ece\u96f6\u6784\u5efa\u9ad8\u6027\u80fd Reactor \u670d\u52a1\u5668\uff1a - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-07-24T20:18:49+00:00","dateModified":"2026-07-24T20:18:49+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/81764.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/81764.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/81764.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u4ece\u96f6\u6784\u5efa\u9ad8\u6027\u80fd Reactor \u670d\u52a1\u5668\uff1a"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/81764","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=81764"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/81764\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/81759"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=81764"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=81764"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=81764"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=81764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}