{"id":17076,"date":"2025-04-18T23:32:11","date_gmt":"2025-04-18T15:32:11","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/17076.html"},"modified":"2025-04-18T23:32:11","modified_gmt":"2025-04-18T15:32:11","slug":"%e9%9d%a2%e8%af%95%ef%bc%9a%e4%bb%8e%e9%9b%b6%e5%ae%9e%e7%8e%b0%e4%b8%80%e4%b8%aamutli-reactor%e6%9c%8d%e5%8a%a1%e5%99%a8-%e9%a1%b9%e7%9b%ae","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/17076.html","title":{"rendered":"\u9762\u8bd5\uff1a\u4ece\u96f6\u5b9e\u73b0\u4e00\u4e2aMutli-reactor\u670d\u52a1\u5668\u2014\u2014\u9879\u76ee"},"content":{"rendered":"<p>\u6700\u8fd1\u4e5f\u662f\u5728\u5b66\u4e60Reactor\u7684\u539f\u7406&#xff0c;\u4e5f\u770b\u4e86\u5f88\u591a\u76f8\u5173\u7684\u9879\u76ee\u3002\u51fa\u4e8e\u5b66\u4e60\u7684\u5fc3\u6001&#xff0c;\u6211\u5c31\u5c1d\u8bd5\u5b66\u4e60\u9648\u7855\u7684Tcp\u7f51\u7edc\u7f16\u7a0b\u5e93&#xff08;Muduo&#xff09;\u3002\u8fd9\u7bc7\u6587\u7ae0\u5c06\u5e26\u4f60\u4ece\u96f6\u5f00\u59cb\u5b9e\u73b0\u4e00\u4e2aMutli-reactor\u670d\u52a1\u5668\u3002<\/p>\n<h2>\u4e00\u3001Mutli-reactor\u539f\u7406<\/h2>\n<h3>1.1 \u4ec0\u4e48\u662fMutli-reactor&#xff1f;<\/h3>\n<p>\u5728\u9ad8\u6027\u80fd\u7f51\u7edc\u7f16\u7a0b\u4e2d&#xff0c;Reactor\u6a21\u5f0f\u662f\u4e00\u79cd\u5e38\u7528\u7684\u4e8b\u4ef6\u9a71\u52a8\u67b6\u6784\u6a21\u5f0f&#xff0c;\u5b83\u5141\u8bb8\u670d\u52a1\u5668\u9ad8\u6548\u5730\u5904\u7406\u5927\u91cf\u5e76\u53d1\u8fde\u63a5\u3002\u800cMulti-Reactor\u6a21\u5f0f\u662f\u5bf9\u7ecf\u5178Reactor\u6a21\u5f0f\u7684\u4e00\u79cd\u6269\u5c55\u548c\u4f18\u5316&#xff0c;\u901a\u8fc7\u5c06\u4e8b\u4ef6\u5206\u6d3e\u4e0e\u4e8b\u4ef6\u5904\u7406\u5206\u79bb&#xff0c;\u5e76\u5229\u7528\u591a\u4e2aReactor\u5b9e\u4f8b\u6765\u63d0\u9ad8\u7cfb\u7edf\u6027\u80fd\u548c\u53ef\u6269\u5c55\u6027\u3002\u5728\u8fd9\u79cd\u6a21\u5f0f\u4e0b&#xff0c;\u901a\u5e38\u6709\u4e00\u4e2a\u4e3b\u7ebf\u7a0b&#xff08;\u6216\u5c11\u91cf\u7ebf\u7a0b&#xff09;\u8d1f\u8d23\u76d1\u542c\u65b0\u8fde\u63a5\u8bf7\u6c42&#xff0c;\u5e76\u5c06\u8fd9\u4e9b\u8fde\u63a5\u5206\u914d\u7ed9\u5de5\u4f5c\u7ebf\u7a0b\u6c60\u4e2d\u7684\u4e0d\u540cReactor\u5b9e\u4f8b\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<p>\u5f53\u7136&#xff0c;\u8fd9\u79cd\u6a21\u578b\u4e5f\u6709\u660e\u663e\u7f3a\u70b9&#xff0c;\u8fde\u63a5\u5efa\u7acb\u3001IO \u4e8b\u4ef6\u8bfb\u53d6\u4ee5\u53ca\u4e8b\u4ef6\u5206\u53d1\u5b8c\u5168\u6709\u5355\u7ebf\u7a0b\u5904\u7406&#xff1b;\u6bd4\u5982\u5f53\u67d0\u4e2a\u8fde\u63a5\u901a\u8fc7\u7cfb\u7edf\u8c03\u7528\u6b63\u5728\u8bfb\u53d6\u6570\u636e&#xff0c;\u6b64\u65f6\u76f8\u5bf9\u4e8e\u5176\u4ed6\u4e8b\u4ef6\u6765\u8bf4&#xff0c;\u5b8c\u5168\u662f\u963b\u585e\u72b6\u6001&#xff0c;\u65b0\u8fde\u63a5\u65e0\u6cd5\u5904\u7406\u3001\u5176\u4ed6\u8fde\u63a5\u7684 IO\u3001\u67e5\u8be2 IO \u8bfb\u5199\u4ee5\u53ca\u4e8b\u4ef6\u5206\u53d1\u90fd\u65e0\u6cd5\u5b8c\u6210\u3002\u4f46\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5b66\u4e60Mutli-reactor\u7f16\u7a0b\u6a21\u578b\u62d3\u5c55\u81ea\u5df1\u5bf9\u4e8eReactor\u7684\u4e86\u89e3\u3002\u4e0b\u56fe\u662f\u7b80\u5355\u7684Mutli-reactor\u793a\u4f8b\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"807\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418153209-68027079a9d41.png\" width=\"1138\" \/><\/p>\n<h3>1.2 \u5982\u4f55\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684Mutli-reactor\u6a21\u578b&#xff1f;<\/h3>\n<h4>\u5173\u952e\u70b9<\/h4>\n<li>\u4e3b\u7ebf\u7a0b&#xff08;Main Reactor&#xff09;&#xff1a;\u8d1f\u8d23\u76d1\u542c\u65b0\u7684TCP\u8fde\u63a5\u8bf7\u6c42&#xff0c;\u5e76\u5c06\u5176\u5206\u914d\u7ed9\u5de5\u4f5c\u7ebf\u7a0b&#xff08;Sub Reactors&#xff09;\u3002<\/li>\n<li>\u5de5\u4f5c\u7ebf\u7a0b&#xff08;Sub Reactors&#xff09;&#xff1a;\u6bcf\u4e2a\u7ebf\u7a0b\u72ec\u7acb\u8fd0\u884c&#xff0c;\u4f7f\u7528epoll\u76d1\u63a7\u81ea\u5df1\u8d1f\u8d23\u7684\u5ba2\u6237\u7aef\u8fde\u63a5\u4e0a\u7684I\/O\u4e8b\u4ef6&#xff0c;\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002<\/li>\n<li>\u975e\u963b\u585eI\/O\u4e0e\u8fb9\u7f18\u89e6\u53d1&#xff08;EPOLLET&#xff09;&#xff1a;\u8bbe\u7f6e\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e3a\u975e\u963b\u585e\u6a21\u5f0f&#xff0c;\u5e76\u4f7f\u7528\u8fb9\u7f18\u89e6\u53d1\u65b9\u5f0f\u6765\u63d0\u9ad8\u6548\u7387\u3002<\/li>\n<h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<p>\u4e0b\u9762\u7684\u4ee3\u7801&#xff0c;\u7b80\u5355\u4ecb\u7ecd\u4e86\u5982\u4f55\u5b9e\u73b0\u4e00\u4e2a\u57fa\u672c\u7684Mutli-reactor\u670d\u52a1\u5668&#xff08;\u4e10\u7248&#xff09;<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;thread&gt;<br \/>\n#include &lt;vector&gt;<br \/>\n#include &lt;sys\/epoll.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;fcntl.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<\/p>\n<p>#define MAX_EVENTS 10 \/\/ \u6700\u5927\u4e8b\u4ef6\u6570<br \/>\n#define PORT 8080     \/\/ \u76d1\u542c\u7aef\u53e3<\/p>\n<p>\/\/ \u5c06\u6587\u4ef6\u63cf\u8ff0\u7b26\u8bbe\u7f6e\u4e3a\u975e\u963b\u585e\u6a21\u5f0f<br \/>\nvoid set_nonblock(int fd) {<br \/>\n    int flags &#061; fcntl(fd, F_GETFL, 0); \/\/ \u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u72b6\u6001\u6807\u5fd7<br \/>\n    fcntl(fd, F_SETFL, flags | O_NONBLOCK); \/\/ \u8bbe\u7f6e\u4e3a\u975e\u963b\u585e\u6a21\u5f0f<br \/>\n}<\/p>\n<p>\/\/ \u5904\u7406\u5ba2\u6237\u7aef\u8fde\u63a5<br \/>\nvoid handle_client(int client_fd) {<br \/>\n    char buffer[1024]; \/\/ \u7f13\u51b2\u533a\u7528\u4e8e\u5b58\u50a8\u8bfb\u53d6\u7684\u6570\u636e<br \/>\n    \/\/ \u5faa\u73af\u8bfb\u53d6\u5e76\u5904\u7406\u6765\u81ea\u5ba2\u6237\u7aef\u7684\u6570\u636e<br \/>\n    while(true) {<br \/>\n        \/\/ \u4ece\u5ba2\u6237\u7aef\u8bfb\u53d6\u6570\u636e\u5230\u7f13\u51b2\u533a<br \/>\n        ssize_t read_bytes &#061; read(client_fd, buffer, sizeof(buffer));<br \/>\n        if(read_bytes &gt; 0) { \/\/ \u5982\u679c\u8bfb\u53d6\u5230\u6570\u636e<br \/>\n            std::cout &lt;&lt; &#034;Received: &#034; &lt;&lt; std::string(buffer, read_bytes) &lt;&lt; std::endl; \/\/ \u6253\u5370\u63a5\u6536\u5230\u7684\u6570\u636e<br \/>\n            write(client_fd, buffer, read_bytes); \/\/ \u5c06\u6570\u636e\u56de\u4f20\u7ed9\u5ba2\u6237\u7aef&#xff08;echo&#xff09;<br \/>\n        } else { \/\/ \u6ca1\u6709\u6570\u636e\u6216\u53d1\u751f\u9519\u8bef\u65f6\u5173\u95ed\u8fde\u63a5<br \/>\n            close(client_fd);<br \/>\n            break;<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ \u5b50Reactor\u5904\u7406\u903b\u8f91&#xff0c;\u8d1f\u8d23\u5904\u7406\u5177\u4f53\u7684\u5ba2\u6237\u7aef\u8fde\u63a5<br \/>\nvoid sub_reactor(int listen_fd) {<br \/>\n    \/\/ \u521b\u5efaepoll\u5b9e\u4f8b<br \/>\n    int epoll_fd &#061; epoll_create1(0);<br \/>\n    struct epoll_event event;<br \/>\n    event.events &#061; EPOLLIN | EPOLLET; \/\/ \u76d1\u542c\u53ef\u8bfb\u4e8b\u4ef6\u548c\u8fb9\u7f18\u89e6\u53d1\u6a21\u5f0f<br \/>\n    event.data.fd &#061; listen_fd; \/\/ \u5173\u8054\u76d1\u542csocket<br \/>\n    epoll_ctl(epoll_fd, EPOLL_CTL_ADD, listen_fd, &amp;event); \/\/ \u5c06\u76d1\u542csocket\u52a0\u5165epoll\u76d1\u63a7\u5217\u8868<\/p>\n<p>    struct epoll_event events[MAX_EVENTS]; \/\/ \u7528\u4e8e\u5b58\u50a8\u53d1\u751f\u7684\u4e8b\u4ef6<br \/>\n    while(true) {<br \/>\n        \/\/ \u7b49\u5f85\u4e8b\u4ef6\u53d1\u751f&#xff08;\u963b\u585e\u76f4\u5230\u6709\u4e8b\u4ef6&#xff09;<br \/>\n        int n &#061; epoll_wait(epoll_fd, events, MAX_EVENTS, -1);<br \/>\n        for(int i &#061; 0; i &lt; n; &#043;&#043;i) {<br \/>\n            if(events[i].data.fd &#061;&#061; listen_fd) { \/\/ \u5982\u679c\u662f\u65b0\u7684\u8fde\u63a5\u8bf7\u6c42<br \/>\n                sockaddr_in client_addr;<br \/>\n                socklen_t client_len &#061; sizeof(client_addr);<br \/>\n                int client_fd &#061; accept(listen_fd, (sockaddr*)&amp;client_addr, &amp;client_len); \/\/ \u63a5\u53d7\u65b0\u8fde\u63a5<br \/>\n                if(client_fd &#061;&#061; -1) continue;<br \/>\n                set_nonblock(client_fd); \/\/ \u8bbe\u7f6e\u4e3a\u975e\u963b\u585e\u6a21\u5f0f<br \/>\n                event.events &#061; EPOLLIN | EPOLLET; \/\/ \u8bbe\u7f6e\u76d1\u542c\u4e8b\u4ef6\u7c7b\u578b<br \/>\n                event.data.fd &#061; client_fd; \/\/ \u5173\u8054\u65b0\u7684\u5ba2\u6237\u7aefsocket<br \/>\n                epoll_ctl(epoll_fd, EPOLL_CTL_ADD, client_fd, &amp;event); \/\/ \u5c06\u65b0\u7684\u5ba2\u6237\u7aefsocket\u52a0\u5165epoll\u76d1\u63a7\u5217\u8868<br \/>\n            } else { \/\/ \u5982\u679c\u662f\u5df2\u6709\u8fde\u63a5\u7684\u6570\u636e\u5230\u8fbe<br \/>\n                handle_client(events[i].data.fd); \/\/ \u5904\u7406\u5ba2\u6237\u7aef\u6570\u636e<br \/>\n                epoll_ctl(epoll_fd, EPOLL_CTL_DEL, events[i].data.fd, nullptr); \/\/ \u4eceepoll\u4e2d\u79fb\u9664\u8be5socket<br \/>\n                close(events[i].data.fd); \/\/ \u5173\u95edsocket<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    int listen_fd &#061; socket(AF_INET, SOCK_STREAM, 0); \/\/ \u521b\u5efa\u76d1\u542csocket<br \/>\n    set_nonblock(listen_fd); \/\/ \u8bbe\u7f6e\u4e3a\u975e\u963b\u585e\u6a21\u5f0f<\/p>\n<p>    sockaddr_in server_addr;<br \/>\n    server_addr.sin_family &#061; AF_INET; \/\/ \u4f7f\u7528IPv4\u5730\u5740\u65cf<br \/>\n    server_addr.sin_port &#061; htons(PORT); \/\/ \u7ed1\u5b9a\u7aef\u53e3\u53f7<br \/>\n    server_addr.sin_addr.s_addr &#061; htonl(INADDR_ANY); \/\/ \u7ed1\u5b9a\u6240\u6709\u53ef\u7528\u7684\u7f51\u7edc\u63a5\u53e3<\/p>\n<p>    bind(listen_fd, (sockaddr*)&amp;server_addr, sizeof(server_addr)); \/\/ \u7ed1\u5b9asocket\u5230\u6307\u5b9a\u7684IP\u548c\u7aef\u53e3<br \/>\n    listen(listen_fd, SOMAXCONN); \/\/ \u5f00\u59cb\u76d1\u542c<\/p>\n<p>    \/\/ \u4e3bReactor\u5faa\u73af\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u8fd0\u884c<br \/>\n    int epoll_fd &#061; epoll_create1(0); \/\/ \u521b\u5efaepoll\u5b9e\u4f8b<br \/>\n    struct epoll_event event;<br \/>\n    event.events &#061; EPOLLIN; \/\/ \u76d1\u542c\u53ef\u8bfb\u4e8b\u4ef6<br \/>\n    event.data.fd &#061; listen_fd; \/\/ \u5173\u8054\u76d1\u542csocket<br \/>\n    epoll_ctl(epoll_fd, EPOLL_CTL_ADD, listen_fd, &amp;event); \/\/ \u5c06\u76d1\u542csocket\u52a0\u5165epoll\u76d1\u63a7\u5217\u8868<\/p>\n<p>    std::vector&lt;std::thread&gt; threads;<br \/>\n    const int num_threads &#061; std::thread::hardware_concurrency(); \/\/ \u83b7\u53d6\u786c\u4ef6\u652f\u6301\u7684\u6700\u5927\u5e76\u53d1\u7ebf\u7a0b\u6570<br \/>\n    for(int i &#061; 0; i &lt; num_threads; &#043;&#043;i) {<br \/>\n        threads.emplace_back(sub_reactor, listen_fd); \/\/ \u542f\u52a8\u591a\u4e2a\u5b50Reactor\u7ebf\u7a0b<br \/>\n    }<\/p>\n<p>    struct epoll_event events[MAX_EVENTS]; \/\/ \u7528\u4e8e\u5b58\u50a8\u53d1\u751f\u7684\u4e8b\u4ef6<br \/>\n    while(true) {<br \/>\n        int n &#061; epoll_wait(epoll_fd, events, MAX_EVENTS, -1); \/\/ \u7b49\u5f85\u4e8b\u4ef6\u53d1\u751f<br \/>\n        for(int i &#061; 0; i &lt; n; &#043;&#043;i) {<br \/>\n            if(events[i].data.fd &#061;&#061; listen_fd) { \/\/ \u5982\u679c\u662f\u65b0\u7684\u8fde\u63a5\u8bf7\u6c42<br \/>\n                sockaddr_in client_addr;<br \/>\n                socklen_t client_len &#061; sizeof(client_addr);<br \/>\n                int client_fd &#061; accept(listen_fd, (sockaddr*)&amp;client_addr, &amp;client_len); \/\/ \u63a5\u53d7\u65b0\u8fde\u63a5<br \/>\n                if(client_fd &#061;&#061; -1) continue;<br \/>\n                set_nonblock(client_fd); \/\/ \u8bbe\u7f6e\u4e3a\u975e\u963b\u585e\u6a21\u5f0f<\/p>\n<p>                \/\/ \u5728\u5b9e\u9645\u5e94\u7528\u4e2d&#xff0c;\u8fd9\u91cc\u5e94\u8be5\u5c06\u65b0\u8fde\u63a5\u5206\u914d\u7ed9\u4e00\u4e2a\u5b50Reactor\u8fdb\u884c\u5904\u7406<br \/>\n                \/\/ \u4e3a\u4e86\u7b80\u5316\u793a\u4f8b&#xff0c;\u6211\u4eec\u53ea\u662f\u6253\u5370\u4e00\u6761\u6d88\u606f<br \/>\n                std::cout &lt;&lt; &#034;New connection accepted&#034; &lt;&lt; std::endl;<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    for(auto&amp; t : threads) {<br \/>\n        if(t.joinable()) t.join(); \/\/ \u7b49\u5f85\u6240\u6709\u5b50Reactor\u7ebf\u7a0b\u7ed3\u675f<br \/>\n    }<\/p>\n<p>    return 0;<br \/>\n} <\/p>\n<ul>\n<li>\n<p>set_nonblock\u51fd\u6570&#xff1a;\u5c06\u6587\u4ef6\u63cf\u8ff0\u7b26\u8bbe\u7f6e\u4e3a\u975e\u963b\u585e\u6a21\u5f0f&#xff0c;\u8fd9\u6837\u5f53\u5c1d\u8bd5\u8bfb\u5199\u64cd\u4f5c\u800c\u6ca1\u6709\u6570\u636e\u53ef\u8bfb\u6216\u5199\u65f6\u4e0d\u4f1a\u963b\u585e\u3002<\/p>\n<\/li>\n<li>\n<p>handle_client\u51fd\u6570&#xff1a;\u5904\u7406\u6bcf\u4e2a\u5ba2\u6237\u7aef\u7684\u5177\u4f53\u903b\u8f91&#xff0c;\u5305\u62ec\u63a5\u6536\u6570\u636e\u3001\u6253\u5370\u63a5\u6536\u5230\u7684\u6570\u636e\u4ee5\u53ca\u5c06\u6570\u636e\u56de\u4f20\u7ed9\u5ba2\u6237\u7aef&#xff08;echo&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>sub_reactor\u51fd\u6570&#xff1a;\u8fd9\u662f\u5b50Reactor\u7684\u5de5\u4f5c\u903b\u8f91&#xff0c;\u4e3b\u8981\u8d1f\u8d23\u5904\u7406\u5177\u4f53\u5ba2\u6237\u7aef\u7684I\/O\u4e8b\u4ef6\u3002\u9996\u5148\u521b\u5efa\u4e00\u4e2aepoll\u5b9e\u4f8b&#xff0c;\u5e76\u5c06\u76d1\u542c\u5957\u63a5\u5b57\u6dfb\u52a0\u5230epoll\u7684\u76d1\u63a7\u5217\u8868\u4e2d\u3002\u7136\u540e\u8fdb\u5165\u4e00\u4e2a\u65e0\u9650\u5faa\u73af&#xff0c;\u7b49\u5f85\u5e76\u5904\u7406\u4e8b\u4ef6\u3002\u5982\u679c\u4e8b\u4ef6\u5bf9\u5e94\u4e8e\u4e00\u4e2a\u65b0\u7684\u8fde\u63a5\u8bf7\u6c42&#xff0c;\u5219\u63a5\u53d7\u8be5\u8fde\u63a5&#xff0c;\u5e76\u5c06\u5176\u6dfb\u52a0\u5230epoll\u76d1\u63a7\u5217\u8868\u4e2d&#xff1b;\u5982\u679c\u662f\u5df2\u6709\u7684\u8fde\u63a5\u4e0a\u6709\u6570\u636e\u5230\u8fbe&#xff0c;\u5219\u8c03\u7528handle_client\u51fd\u6570\u5904\u7406\u6570\u636e\u3002<\/p>\n<\/li>\n<li>\n<p>main\u51fd\u6570&#xff1a;<\/p>\n<ul>\n<li>\u521b\u5efa\u76d1\u542c\u5957\u63a5\u5b57&#xff0c;\u5e76\u8bbe\u7f6e\u4e3a\u975e\u963b\u585e\u6a21\u5f0f\u3002<\/li>\n<li>\u7ed1\u5b9a\u5957\u63a5\u5b57\u5230\u7279\u5b9a\u7684IP\u5730\u5740\u548c\u7aef\u53e3&#xff0c;\u5e76\u5f00\u59cb\u76d1\u542c\u3002<\/li>\n<li>\u521b\u5efa\u4e00\u4e2aepoll\u5b9e\u4f8b\u5e76\u5c06\u76d1\u542c\u5957\u63a5\u5b57\u6dfb\u52a0\u5230\u5176\u4e2d\u3002<\/li>\n<li>\u6839\u636e\u7cfb\u7edf\u652f\u6301\u7684\u6700\u5927\u5e76\u53d1\u7ebf\u7a0b\u6570\u542f\u52a8\u591a\u4e2a\u5b50Reactor\u7ebf\u7a0b\u3002<\/li>\n<li>\u8fdb\u5165\u4e00\u4e2a\u65e0\u9650\u5faa\u73af&#xff0c;\u7b49\u5f85\u5e76\u5904\u7406\u4e8b\u4ef6\u3002\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d&#xff0c;\u4e3b\u8981\u662f\u7b80\u5355\u5730\u6253\u5370\u51fa\u201c\u65b0\u8fde\u63a5\u5df2\u63a5\u53d7\u201d\u7684\u6d88\u606f\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d&#xff0c;\u9700\u8981\u5b9e\u73b0\u5c06\u65b0\u8fde\u63a5\u5206\u914d\u7ed9\u67d0\u4e2a\u5b50Reactor\u7684\u903b\u8f91&#xff08;\u8fd9\u4e00\u90e8\u5206\u7684\u4ee3\u7801\u903b\u8f91\u5c06\u5728\u540e\u7eed\u7684\u4ee3\u7801\u4e2d\u5b9e\u73b0&#xff09;\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u4e0b\u9762\u7684\u4ee3\u7801\u5b9e\u73b0\u4e86\u591a\u4e2a\u5e76\u53d1client\u5ba2\u6237\u7aef\u5411Mutli-reactor\u670d\u52a1\u5668\u53d1\u9001\u62a5\u6587&#xff1a;<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;thread&gt;<br \/>\n#include &lt;vector&gt;<br \/>\n#include &lt;cstring&gt;<br \/>\n#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;arpa\/inet.h&gt;<br \/>\n#include &lt;unistd.h&gt;<\/p>\n<p>void client_task(int client_id, const char* ip, int port) {<br \/>\n    int sock &#061; 0;<br \/>\n    struct sockaddr_in serv_addr;<br \/>\n    const char* hello &#061; &#034;Hello from client&#034;;<br \/>\n    char buffer[1024] &#061; {0};<\/p>\n<p>    \/\/ \u521b\u5efasocket<br \/>\n    if ((sock &#061; socket(AF_INET, SOCK_STREAM, 0)) &lt; 0) {<br \/>\n        std::cerr &lt;&lt; &#034;Client &#034; &lt;&lt; client_id &lt;&lt; &#034;: Socket creation error\\\\n&#034;;<br \/>\n        return;<br \/>\n    }<\/p>\n<p>    serv_addr.sin_family &#061; AF_INET;<br \/>\n    serv_addr.sin_port &#061; htons(port);<\/p>\n<p>    \/\/ \u5c06IP\u5730\u5740\u4ece\u6587\u672c\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u5f62\u5f0f<br \/>\n    if (inet_pton(AF_INET, ip, &amp;serv_addr.sin_addr) &lt;&#061; 0) {<br \/>\n        std::cerr &lt;&lt; &#034;Client &#034; &lt;&lt; client_id &lt;&lt; &#034;: Invalid address\/ Address not supported\\\\n&#034;;<br \/>\n        return;<br \/>\n    }<\/p>\n<p>    \/\/ \u8fde\u63a5\u5230\u670d\u52a1\u5668<br \/>\n    if (connect(sock, (struct sockaddr*)&amp;serv_addr, sizeof(serv_addr)) &lt; 0) {<br \/>\n        std::cerr &lt;&lt; &#034;Client &#034; &lt;&lt; client_id &lt;&lt; &#034;: Connection failed\\\\n&#034;;<br \/>\n        return;<br \/>\n    }<\/p>\n<p>    \/\/ \u53d1\u9001\u6d88\u606f\u7ed9\u670d\u52a1\u5668<br \/>\n    send(sock, hello, strlen(hello), 0);<br \/>\n    std::cout &lt;&lt; &#034;Client &#034; &lt;&lt; client_id &lt;&lt; &#034;: Hello message sent\\\\n&#034;;<\/p>\n<p>    \/\/ \u63a5\u6536\u670d\u52a1\u5668\u7684\u54cd\u5e94<br \/>\n    read(sock, buffer, 1024);<br \/>\n    std::cout &lt;&lt; &#034;Client &#034; &lt;&lt; client_id &lt;&lt; &#034;: Server response: &#034; &lt;&lt; buffer &lt;&lt; std::endl;<\/p>\n<p>    close(sock); \/\/ \u5173\u95edsocket<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    const int num_clients &#061; 100; \/\/ \u8981\u542f\u52a8\u7684\u5ba2\u6237\u7aef\u6570\u91cf<br \/>\n    const char* server_ip &#061; &#034;127.0.0.1&#034;; \/\/ \u670d\u52a1\u5668IP\u5730\u5740<br \/>\n    int server_port &#061; 8080; \/\/ \u670d\u52a1\u5668\u7aef\u53e3<\/p>\n<p>    std::vector&lt;std::thread&gt; clients;<br \/>\n    for(int i &#061; 0; i &lt; num_clients; &#043;&#043;i) {<br \/>\n        clients.emplace_back(client_task, i &#043; 1, server_ip, server_port);<br \/>\n    }<\/p>\n<p>    \/\/ \u7b49\u5f85\u6240\u6709\u5ba2\u6237\u7aef\u7ebf\u7a0b\u5b8c\u6210<br \/>\n    for(auto&amp; t : clients) {<br \/>\n        if(t.joinable()) t.join();<br \/>\n    }<\/p>\n<p>    return 0;<br \/>\n} <\/p>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u4e3b\u8981\u903b\u8f91\u4e3a&#xff1a;\u5f00\u542f\u591a\u4e2aclient\u5ba2\u6237\u7aef\u8fde\u63a5\u5230\u670d\u52a1\u5668&#xff0c;\u5e76\u53d1\u9001hello\u62a5\u6587\u3001\u63a5\u53d7hello\u62a5\u6587\u3002<\/p>\n<p>\u5b9e\u73b0\u6548\u679c\u5982\u4e0b&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"269\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418153210-6802707a14956.png\" width=\"1251\" \/><\/p>\n<p>\u53ef\u4ee5\u770b\u5230\u6211\u4eec\u6709100\u4e2a\u5ba2\u6237\u7aef\u5411\u670d\u52a1\u5668\u53d1\u9001hello\u62a5\u6587&#xff0c;\u5e76\u4e14\u4e5f\u5168\u90e8\u63a5\u6536\u5230\u4e86\u3002<\/p>\n<h3>1.3 Multi-Reactor\u6709\u4ec0\u4e48\u7528&#xff1f;<\/h3>\n<p>\u91c7\u7528Multi-Reactor\u6a21\u578b\u7684\u4f18\u52bf\u5728\u4e8e&#xff1a;<\/p>\n<ul>\n<li>\u9ad8\u5e76\u53d1\u5904\u7406\u80fd\u529b&#xff1a;\u901a\u8fc7\u4f7f\u7528\u591a\u4e2aReactor\u5b9e\u4f8b&#xff0c;\u53ef\u4ee5\u5728\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u907f\u514d\u5355\u4e2aReactor\u6210\u4e3a\u74f6\u9888&#xff0c;\u4ece\u800c\u652f\u6301\u66f4\u9ad8\u7684\u5e76\u53d1\u8fde\u63a5\u6570\u3002<\/li>\n<li>\u826f\u597d\u7684\u6269\u5c55\u6027&#xff1a;\u968f\u7740\u670d\u52a1\u5668\u786c\u4ef6\u8d44\u6e90\u7684\u589e\u52a0&#xff08;\u5982CPU\u6838\u5fc3\u6570&#xff09;&#xff0c;\u53ef\u4ee5\u901a\u8fc7\u589e\u52a0Reactor\u7684\u6570\u91cf\u6765\u5145\u5206\u5229\u7528\u65b0\u589e\u8d44\u6e90&#xff0c;\u589e\u5f3a\u7cfb\u7edf\u6027\u80fd\u3002<\/li>\n<li>\u7b80\u5316\u590d\u6742\u5ea6\u7ba1\u7406&#xff1a;\u5c06\u4e8b\u4ef6\u5206\u6d3e\u4e0e\u4e8b\u4ef6\u5904\u7406\u5206\u79bb&#xff0c;\u6709\u52a9\u4e8e\u964d\u4f4e\u4ee3\u7801\u590d\u6742\u5ea6&#xff0c;\u4fbf\u4e8e\u7ef4\u62a4\u548c\u6269\u5c55\u3002<\/li>\n<\/ul>\n<h3>1.4\u5176\u4ed6\u7684Reactor\u6a21\u578b<\/h3>\n<p>\u4e3b\u8981\u7684Reactor\u6a21\u578b\u4e3b\u8981\u6709\u5355\u7ebf\u7a0bReactor\u6a21\u578b\u3001\u00a0\u7ebf\u7a0b\u6c60Reactor\u6a21\u578b\u3001\u4e3b\u4eceReactor\u6a21\u578b\u3002\u800c\u6211\u4eec\u4e3b\u8981\u8981\u5b9e\u73b0\u7684\u662f\u7ebf\u7a0b\u6c60Reactor\u6a21\u578b&#xff08;Multi-Reactor&#xff09;\u3002\u4e0b\u9762\u662f\u5b83\u4eec\u4e4b\u95f4\u7684\u6bd4\u8f83\u3002<\/p>\n<p>\u60a8\u4e5f\u53ef\u4ee5\u901a\u8fc7\u9ad8\u6027\u80fd\u7f51\u7edc\u7f16\u7a0b\u4e4b Reactor \u7f51\u7edc\u6a21\u578b&#xff08;\u5f7b\u5e95\u641e\u61c2&#xff09;_reactor\u7f51\u7edc\u6a21\u578b-CSDN\u535a\u5ba2\u8fd9\u7bc7\u6587\u7ae0\u8fdb\u884c\u5b66\u4e60\u3002<\/p>\n<h4>1.4.1\u00a0\u5355\u7ebf\u7a0bReactor\u6a21\u578b<\/h4>\n<p>\u63cf\u8ff0&#xff1a;<\/p>\n<ul>\n<li>\u5728\u5355\u7ebf\u7a0bReactor\u6a21\u578b\u4e2d&#xff0c;\u6240\u6709\u7684\u4e8b\u4ef6\u5206\u6d3e&#xff08;\u76d1\u542c\u65b0\u8fde\u63a5\u3001\u8bfb\u5199\u64cd\u4f5c\u7b49&#xff09;\u548c\u4e1a\u52a1\u5904\u7406\u90fd\u5728\u540c\u4e00\u4e2a\u7ebf\u7a0b\u5185\u5b8c\u6210\u3002<\/li>\n<li>\u8fd9\u79cd\u6a21\u5f0f\u5b9e\u73b0\u7b80\u5355&#xff0c;\u9002\u7528\u4e8e\u5ba2\u6237\u7aef\u6570\u91cf\u4e0d\u591a\u6216I\/O\u64cd\u4f5c\u8017\u65f6\u8f83\u77ed\u7684\u60c5\u51b5\u3002<\/li>\n<\/ul>\n<p>\u4f18\u7f3a\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u4f18\u70b9&#xff1a;\u00a0\u5b9e\u73b0\u7b80\u5355\u76f4\u63a5&#xff0c;\u6613\u4e8e\u7406\u89e3\u548c\u7ef4\u62a4&#xff1b;\u7531\u4e8e\u6240\u6709\u64cd\u4f5c\u90fd\u5728\u4e00\u4e2a\u7ebf\u7a0b\u4e2d\u8fdb\u884c&#xff0c;\u56e0\u6b64\u4e0d\u5b58\u5728\u7ebf\u7a0b\u5207\u6362\u548c\u540c\u6b65\u95ee\u9898\u3002<\/li>\n<li>\u7f3a\u70b9&#xff1a;\u00a0\u6027\u80fd\u53d7\u9650\u4e8e\u5355\u4e2aCPU\u6838\u5fc3\u7684\u80fd\u529b&#xff0c;\u96be\u4ee5\u5e94\u5bf9\u9ad8\u5e76\u53d1\u573a\u666f&#xff1b;\u5982\u679c\u67d0\u4e2a\u5904\u7406\u8fc7\u7a0b\u8017\u65f6\u8f83\u957f&#xff0c;\u4f1a\u5f71\u54cd\u5176\u4ed6\u4efb\u52a1\u7684\u53ca\u65f6\u54cd\u5e94\u3002<\/li>\n<\/ul>\n<h4>1.4.2\u00a0\u7ebf\u7a0b\u6c60Reactor\u6a21\u578b&#xff08;\u6211\u4eec\u76ee\u524d\u8981\u5b9e\u73b0\u7684&#xff09;<\/h4>\n<p>\u63cf\u8ff0&#xff1a;<\/p>\n<ul>\n<li>\u6b64\u6a21\u578b\u5728\u57fa\u672c\u7684Reactor\u57fa\u7840\u4e0a\u589e\u52a0\u4e86\u7ebf\u7a0b\u6c60\u673a\u5236&#xff0c;\u5373\u5f53Reactor\u63a5\u6536\u5230\u4e00\u4e2a\u4e8b\u4ef6\u540e&#xff0c;\u5b83\u4e0d\u4f1a\u76f4\u63a5\u5904\u7406\u8be5\u4e8b\u4ef6&#xff0c;\u800c\u662f\u5c06\u4e8b\u4ef6\u4ea4\u7ed9\u7ebf\u7a0b\u6c60\u4e2d\u7684\u4e00\u4e2a\u5de5\u4f5c\u7ebf\u7a0b\u6765\u6267\u884c\u3002<\/li>\n<li>\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u4efb\u52a1\u963b\u585eReactor\u4e3b\u7ebf\u7a0b&#xff0c;\u63d0\u9ad8\u7cfb\u7edf\u7684\u6574\u4f53\u6548\u7387\u3002<\/li>\n<\/ul>\n<p>\u4f18\u7f3a\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u4f18\u70b9&#xff1a;\u00a0\u63d0\u5347\u4e86\u7cfb\u7edf\u5bf9\u4e0d\u540c\u8d1f\u8f7d\u7684\u9002\u5e94\u80fd\u529b&#xff0c;\u7279\u522b\u662f\u5bf9\u4e8e\u90a3\u4e9b\u53ef\u80fd\u5305\u542b\u8017\u65f6\u4e1a\u52a1\u903b\u8f91\u7684\u8bf7\u6c42&#xff1b;\u901a\u8fc7\u5408\u7406\u914d\u7f6e\u7ebf\u7a0b\u6c60\u5927\u5c0f&#xff0c;\u53ef\u4ee5\u5728\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u4f18\u5316\u8d44\u6e90\u5229\u7528\u7387\u3002<\/li>\n<li>\u7f3a\u70b9&#xff1a;\u00a0\u9700\u8981\u989d\u5916\u7ba1\u7406\u7ebf\u7a0b\u6c60&#xff0c;\u589e\u52a0\u4e86\u7cfb\u7edf\u590d\u6742\u5ea6&#xff1b;\u82e5\u7ebf\u7a0b\u6c60\u914d\u7f6e\u4e0d\u5f53&#xff0c;\u53ef\u80fd\u4f1a\u5bfc\u81f4\u8d44\u6e90\u6d6a\u8d39\u6216\u8005\u8bf7\u6c42\u6392\u961f\u7b49\u5f85\u7684\u95ee\u9898\u3002<\/li>\n<\/ul>\n<h4>1.4.3\u00a0\u4e3b\u4eceReactor\u6a21\u578b<\/h4>\n<p>\u63cf\u8ff0&#xff1a;<\/p>\n<ul>\n<li>\u4e3b\u4eceReactor\u6a21\u578b\u662f\u4e3a\u4e86\u89e3\u51b3\u5355Reactor\u6a21\u578b\u5728\u9ad8\u5e76\u53d1\u60c5\u51b5\u4e0b\u6027\u80fd\u74f6\u9888\u800c\u8bbe\u8ba1\u7684\u4e00\u79cd\u6539\u8fdb\u65b9\u6848\u3002\u4e3bReactor\u8d1f\u8d23\u76d1\u542c\u65b0\u7684\u8fde\u63a5\u8bf7\u6c42&#xff0c;\u5e76\u5c06\u5df2\u5efa\u7acb\u7684\u8fde\u63a5\u5206\u914d\u7ed9\u591a\u4e2a\u4eceReactor\u5b9e\u4f8b\u5904\u7406\u3002<\/li>\n<li>\u6bcf\u4e2a\u4eceReactor\u72ec\u7acb\u5730\u5904\u7406\u5176\u5206\u914d\u5230\u7684\u8fde\u63a5\u4e0a\u7684\u8bfb\u5199\u4e8b\u4ef6&#xff0c;\u4ece\u800c\u5b9e\u73b0\u4e86\u5e76\u884c\u5904\u7406\u3002<\/li>\n<\/ul>\n<p>\u4f18\u7f3a\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u4f18\u70b9&#xff1a;\u00a0\u80fd\u591f\u5145\u5206\u5229\u7528\u591a\u6838\u5904\u7406\u5668\u7684\u4f18\u52bf&#xff0c;\u63d0\u5347\u7cfb\u7edf\u7684\u541e\u5410\u91cf&#xff1b;\u901a\u8fc7\u5206\u79bb\u65b0\u8fde\u63a5\u63a5\u53d7\u4e0e\u5df2\u6709\u8fde\u63a5\u5904\u7406\u7684\u8fc7\u7a0b&#xff0c;\u51cf\u8f7b\u4e86\u4e3bReactor\u7684\u538b\u529b\u3002<\/li>\n<li>\u7f3a\u70b9&#xff1a;\u00a0\u8bbe\u8ba1\u548c\u5b9e\u73b0\u6bd4\u5355Reactor\u6a21\u578b\u66f4\u52a0\u590d\u6742&#xff1b;\u9700\u8981\u7cbe\u5fc3\u8bbe\u8ba1\u4ee5\u786e\u4fdd\u8d1f\u8f7d\u5747\u8861&#xff0c;\u907f\u514d\u67d0\u4e9b\u4eceReactor\u8fc7\u8f7d\u3002<\/li>\n<\/ul>\n<h2>\u4e8c\u3001Epoll\u7c7b\u7684\u521b\u5efa \u00a0<\/h2>\n<p>\u00a0\u5173\u4e8eselect\u3001poll\u3001epoll\u7684\u539f\u7406\u53ef\u4ee5\u53c2\u8003\u6211\u8fd9\u7bc7\u6587\u7ae0&#xff1a;<\/p>\n<p>\u9762\u8bd5&#xff1a;Epoll\u3001Poll\u3001Select\u5b66\u4e60\u8bb0\u5f55<\/p>\n<p>\u5f53\u4f60\u6df1\u5165\u5230C&#043;&#043;\u5f00\u53d1\u7684\u4e16\u754c\u4e2d\u5c1d\u8bd5\u5b9e\u73b0Multi-Reactor\u6a21\u578b\u65f6&#xff0c;\u4f60\u4f1a\u53d1\u73b0epoll\u8fd9\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\u786e\u5b9e\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u3002\u4f46\u662f&#xff0c;\u76f4\u63a5\u4f7f\u7528epoll\u8fdb\u884c\u4e8b\u4ef6\u76d1\u542c\u548c\u5904\u7406\u6709\u65f6\u5c31\u50cf\u662f\u5728\u8d70\u94a2\u4e1d\u2014\u2014\u4e0d\u4ec5\u9700\u8981\u624b\u52a8\u8bb0\u5f55\u5404\u79cd\u4e8b\u4ef6&#xff0c;\u8fd8\u5f97\u81ea\u5df1\u7ba1\u7406\u8ba1\u65f6\u5668&#xff0c;\u5c24\u5176\u662f\u5728\u975e\u963b\u585e\u72b6\u6001\u4e0b\u64cd\u4f5c\u7684\u65f6\u5019&#xff0c;\u8fd9\u7b80\u76f4\u5c31\u50cf\u662f\u4e00\u573a\u5669\u68a6\u3002<\/p>\n<p>\u6240\u4ee5&#xff0c;\u4e3a\u4ec0\u4e48\u4e0d\u8ba9\u6211\u4eec\u6765\u7b80\u5316\u8fd9\u4e00\u5207\u5462&#xff1f;\u8bbe\u60f3\u4e00\u4e0b&#xff0c;\u5982\u679c\u6211\u4eec\u80fd\u591f\u521b\u5efa\u4e00\u4e2a\u53eb\u505aEPollPoller\u7684\u7c7b\u6765\u5e2e\u6211\u4eec\u641e\u5b9a\u8fd9\u4e00\u5207&#xff0c;\u90a3\u8be5\u6709\u591a\u68d2&#xff01;\u901a\u8fc7\u8fd9\u4e2a\u7c7b\u7684\u6784\u9020\u51fd\u6570&#xff08;\u5b9e\u9645\u4e0a\u5c31\u662f\u8c03\u7528epoll_create&#xff09;&#xff0c;\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u51faepoll\u53e5\u67c4\u3002\u66f4\u9177\u7684\u662f&#xff0c;\u6211\u4eec\u53ef\u4ee5\u5bf9\u5916\u63d0\u4f9b\u4e00\u4e2a\u66f4\u65b0\u63a5\u53e3&#xff08;\u4e5f\u5c31\u662fupdate\u7a97\u53e3&#xff09;&#xff0c;\u8fd9\u6837\u5916\u754c\u5c31\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u901a\u8fc7\u7c7b\u4f3cepoll_ctl\u7684\u65b9\u5f0f\u6765\u6dfb\u52a0\u4ed6\u4eec\u60f3\u8981\u76d1\u542c\u7684\u4e8b\u4ef6\u4e86\u3002<\/p>\n<p>\u60f3\u8c61\u4e00\u4e0b&#xff0c;\u4f60\u4e0d\u518d\u9700\u8981\u62c5\u5fc3\u90a3\u4e9b\u590d\u6742\u7684\u7ec6\u8282&#xff0c;\u53ea\u9700\u8981\u4e13\u6ce8\u4e8e\u4f60\u7684\u4e1a\u52a1\u903b\u8f91\u3002\u5f53\u6709\u65b0\u7684\u4e8b\u4ef6\u5230\u6765\u65f6&#xff0c;\u6bd4\u5982\u53ef\u8bfb\u4e8b\u4ef6\u53d1\u751f&#xff0c;\u6211\u4eec\u7684EPollPoller\u5c31\u4f1a\u8c03\u7528\u76f8\u5e94\u7684\u56de\u8c03\u51fd\u6570&#xff0c;\u7136\u540e\u4f60\u5c31\u80fd\u77e5\u9053\u201c\u563f&#xff0c;\u8fd9\u91cc\u6709\u6d88\u606f\u7b49\u4f60\u5904\u7406\u5462&#xff01;\u201d\u8fd9\u79cd\u8bbe\u8ba1\u4e0d\u4ec5\u5927\u5927\u7b80\u5316\u4e86\u4ee3\u7801\u7684\u590d\u6742\u5ea6&#xff0c;\u8fd8\u8ba9\u6574\u4e2a\u5f00\u53d1\u8fc7\u7a0b\u53d8\u5f97\u66f4\u52a0\u6d41\u7545\u6109\u5feb\u3002<\/p>\n<p>\u603b\u4e4b&#xff0c;\u901a\u8fc7\u5f15\u5165\u8fd9\u6837\u4e00\u4e2aEPollPoller\u7c7b&#xff0c;\u6211\u4eec\u4e0d\u4ec5\u53ef\u4ee5\u4f7fMulti-Reactor\u6a21\u578b\u7684\u5b9e\u73b0\u53d8\u5f97\u66f4\u52a0\u7b80\u5355\u76f4\u89c2&#xff0c;\u540c\u65f6\u4e5f\u4e3a\u540e\u7eed\u7684\u529f\u80fd\u6269\u5c55\u7559\u4e0b\u4e86\u5145\u8db3\u7684\u7a7a\u95f4&#xff01;<\/p>\n<h3>2.1 EPollPoller\u7c7b\u4ecb\u7ecd<\/h3>\n<h4>2.1.1 Poller\u7c7b<\/h4>\n<p>\u5728\u521b\u5efa EventLoop \u7684\u65f6\u5019&#xff0c;\u786e\u5b9e\u9700\u8981\u521d\u59cb\u5316\u4e00\u4e2a\u65b0\u7684 Poller \u5b9e\u4f8b\u6765\u7ba1\u7406\u4e0e\u76d1\u542c\u76f8\u5173\u7684 Channel\u3002\u6bcf\u4e2a Channel \u5305\u542b\u4e86\u4e00\u4e2a socket \u7aef\u53e3\u53ca\u5176\u76f8\u5173\u8054\u7684\u56de\u8c03\u51fd\u6570\u96c6\u5408&#xff0c;\u7528\u4e8e\u5904\u7406\u5404\u79cd\u4e8b\u4ef6&#xff08;\u5982\u8fde\u63a5\u3001\u8bfb\u53d6\u3001\u5199\u5165\u7b49&#xff09;\u3002\u63a5\u4e0b\u6765&#xff0c;\u6211\u4eec\u5bf9\u8fd9\u4e9b\u63a5\u53e3\u548c\u673a\u5236\u8fdb\u884c\u66f4\u52a0\u6e05\u6670\u7684\u63cf\u8ff0&#xff1a;<\/p>\n<h5>\u63a5\u53e3\u8bf4\u660e<\/h5>\n<ul>\n<li>\n<p>virtual Timestamp poll(int timeout, ChannelList* channels) &#061; 0; \u8fd9\u662f\u4e00\u4e2a\u7eaf\u865a\u51fd\u6570&#xff0c;\u5176\u5b9e\u73b0\u7531\u6d3e\u751f\u7c7b\u63d0\u4f9b\u3002\u8be5\u65b9\u6cd5\u6267\u884c\u4e00\u6b21\u8f6e\u8be2\u64cd\u4f5c&#xff0c;\u68c0\u67e5\u662f\u5426\u6709\u4efb\u4f55\u5df2\u6ce8\u518c\u7684\u901a\u9053\u53d1\u751f\u4e86\u4e8b\u4ef6&#xff0c;\u5e76\u8fd4\u56de\u53d1\u751f\u4e8b\u4ef6\u7684\u901a\u9053\u5217\u8868\u4ee5\u53ca\u5f53\u524d\u7684\u65f6\u95f4\u6233\u3002\u5b83\u5141\u8bb8\u6846\u67b6\u901a\u8fc7\u7edf\u4e00\u63a5\u53e3\u8bbf\u95ee\u4e0d\u540c\u7684\u5e95\u5c42I\/O\u591a\u8def\u590d\u7528\u673a\u5236&#xff08;\u4f8b\u5982 epoll, poll&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>virtual void updateChannel(Channel* channel) &#061; 0; \u548c virtual void removeChannel(Channel* channel) &#061; 0; \u8fd9\u4e24\u4e2a\u7eaf\u865a\u51fd\u6570\u5206\u522b\u7528\u4e8e\u66f4\u65b0&#xff08;\u6dfb\u52a0\u6216\u4fee\u6539&#xff09;\u548c\u79fb\u9664\u76d1\u542c\u7684\u901a\u9053\u3002\u5b83\u4eec\u786e\u4fdd\u4e86\u53ef\u4ee5\u6839\u636e\u5e94\u7528\u7a0b\u5e8f\u7684\u9700\u6c42\u52a8\u6001\u5730\u8c03\u6574\u6b63\u5728\u76d1\u542c\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u96c6&#xff0c;\u4ece\u800c\u5b9e\u73b0\u9ad8\u6548\u4e14\u7075\u6d3b\u7684\u4e8b\u4ef6\u9a71\u52a8\u67b6\u6784\u3002<\/p>\n<\/li>\n<li>\n<p>bool hasChannel(Channel* channel) const; \u6b64\u65b9\u6cd5\u7528\u4e8e\u68c0\u67e5\u7ed9\u5b9a\u7684\u901a\u9053\u662f\u5426\u5df2\u7ecf\u88ab\u6ce8\u518c\u5230 Poller \u4e2d\u3002\u8fd9\u5bf9\u4e8e\u907f\u514d\u91cd\u590d\u6ce8\u518c\u6216\u8005\u9519\u8bef\u5220\u9664\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/li>\n<li>\n<p>static Poller* newDefaultPoller(EventLoop* loop); \u8fd9\u662f\u4e00\u4e2a\u9759\u6001\u5de5\u5382\u65b9\u6cd5&#xff0c;\u65e8\u5728\u65e0\u9700\u76f4\u63a5\u6307\u5b9a\u5177\u4f53\u7c7b\u540d\u7684\u60c5\u51b5\u4e0b\u521b\u5efa\u5e76\u8fd4\u56de\u4e00\u4e2a\u5177\u4f53\u7684 Poller \u5b9e\u4f8b&#xff08;\u53ef\u80fd\u662f\u67d0\u4e2a\u6d3e\u751f\u7c7b\u5b9e\u4f8b&#xff09;\u3002\u8fd9\u79cd\u65b9\u6cd5\u589e\u5f3a\u4e86\u4ee3\u7801\u7684\u7075\u6d3b\u6027\u548c\u53ef\u7ef4\u62a4\u6027&#xff0c;\u56e0\u4e3a\u5b83\u5141\u8bb8\u8f7b\u677e\u5207\u6362\u4e0d\u540c\u7c7b\u578b\u7684 Poller \u5b9e\u73b0\u800c\u65e0\u9700\u4fee\u6539\u5ba2\u6237\u7aef\u4ee3\u7801\u3002<\/p>\n<\/li>\n<\/ul>\n<h5>\u5185\u90e8\u673a\u5236<\/h5>\n<p>\u5728 Poller \u7c7b\u5185\u90e8&#xff0c;\u4fdd\u5b58\u4e86\u4e00\u4e2a\u6307\u5411 EventLoop \u7684\u6307\u9488 ownerLoop_&#xff0c;\u8fd9\u8868\u660e\u4e86\u8be5 Poller \u5c5e\u4e8e\u54ea\u4e2a\u4e8b\u4ef6\u7ebf\u7a0b&#xff08;\u5373 EventLoop&#xff09;\u3002\u8fd9\u79cd\u8bbe\u8ba1\u5bf9\u4e8e Multi-Reactor \u6a21\u578b\u81f3\u5173\u91cd\u8981\u3002\u5728 Multi-Reactor \u67b6\u6784\u4e2d&#xff0c;\u901a\u5e38\u6709\u4e00\u4e2a\u4e3b Reactor \u8d1f\u8d23\u63a5\u6536\u65b0\u8fde\u63a5\u5e76\u5c06\u8fd9\u4e9b\u8fde\u63a5\u5206\u914d\u7ed9\u5b50 Reactor&#xff08;Sub-Reactor&#xff09;\u3002\u6bcf\u4e2a Sub-Reactor \u90fd\u9700\u8981\u77e5\u9053\u5b83\u8d1f\u8d23\u76d1\u542c\u54ea\u4e9b\u4e8b\u4ef6\u3002<\/p>\n<p>\u6b64\u5916\u8fd9\u4e2aPoller\u5728\u4e3bReactor\u7684\u5b9e\u73b0\u5f62\u5f0f\u662fAceptor&#xff08;\u63a5\u53d7\u65b0\u8fde\u63a5\u5e76\u5206\u914d\u7ed9Sub-Reactor&#xff0c;\u540e\u7eed\u4ecb\u7ecd&#xff09;<\/p>\n<p>\u56e0\u6b64&#xff0c;ownerLoop_ \u8ba9 Sub-Reactor \u660e\u767d\u81ea\u5df1\u5e94\u8be5\u76d1\u542c\u54ea\u4e9b\u4e8b\u4ef6\u3002\u4e00\u65e6\u901a\u8fc7 poll \u65b9\u6cd5\u5f97\u77e5\u6709\u65b0\u7684\u4e8b\u4ef6\u5230\u8fbe\u65f6&#xff0c;Sub-Reactor \u5c31\u4f1a\u6267\u884c\u5bf9\u5e94\u7684\u4e8b\u4ef6\u56de\u8c03\u51fd\u6570&#xff0c;\u4ece\u800c\u5b9e\u73b0\u9ad8\u6548\u7684\u4e8b\u4ef6\u5904\u7406\u6d41\u7a0b\u3002\u8fd9\u79cd\u673a\u5236\u786e\u4fdd\u4e86\u7cfb\u7edf\u53ef\u4ee5\u6709\u6548\u5730\u5904\u7406\u5927\u91cf\u7684\u5e76\u53d1\u8fde\u63a5&#xff0c;\u540c\u65f6\u4fdd\u6301\u826f\u597d\u7684\u54cd\u5e94\u901f\u5ea6\u548c\u8d44\u6e90\u5229\u7528\u7387\u3002<\/p>\n<p>\u8be6\u7ec6\u7684\u4ee3\u7801\u4ecb\u7ecd\u89c1\u4e0b&#xff1a;<\/p>\n<p>\/\/ Poller.h<br \/>\n#ifndef POLLER_H<br \/>\n#define POLLER_H<\/p>\n<p>#include &#034;noncopyable.h&#034;<br \/>\n#include &#034;Channel.h&#034;<br \/>\n#include &#034;Timestamp.h&#034;<\/p>\n<p>#include &lt;vector&gt;<br \/>\n#include &lt;unordered_map&gt;<br \/>\nclass Poller:noncopyable{<br \/>\npublic:<br \/>\n    using ChannelList&#061;std::vector&lt;Channel*&gt;;<br \/>\n    \/\/\u6784\u9020\u51fd\u6570&#xff0c;\u5c06\u6b21poller\u7ed1\u5b9a\u5230\u4e00\u4e2aEventLoop&#xff08;\u7ebf\u7a0b\u5faa\u73af&#xff09;\u4e2d<br \/>\n    Poller(EventLoop* loop);<br \/>\n    \/\/ \u6790\u51fd\u6570&#xff0c;\u91ca\u653e\u6240\u6709channel\u3002\u5e76\u4e14\u5177\u6709\u7ee7\u627f\u7684\u6027\u8d28&#xff0c;\u6240\u4ee5\u8bbe\u7f6e\u4e3a\u865a\u51fd\u6570<br \/>\n    virtual ~Poller()&#061;default;<br \/>\n    \/\/\u5bf9\u5916\u66b4\u9732\u7684poll\u7ed3\u6784&#xff0c;\u4e3b\u8981\u7528\u4e8e\u8c03\u7528epoll_wait\u6216\u8005poll_wait<br \/>\n    \/\/ \u63a5\u53d7\u8d85\u65f6\u4e8b\u4ef6&#xff0c;\u5e76\u8fd4\u56de\u53d1\u751f\u4e8b\u4ef6\u7684ChannelList&#xff0c;\u8fd4\u56de\u7684\u65f6\u95f4\u4e3apoll\u51fd\u6570\u88ab\u8c03\u7528\u7684\u65f6\u95f4\u70b9<br \/>\n    virtual Timestamp poll(int timeout,ChannelList* channels)&#061;0;<br \/>\n    \/\/ \u6ce8\u518c\u65b0\u7684\u4e8b\u4ef6&#xff08;\u4f7f\u7528Channel\u5305\u88c5\u7684\u4e8b\u4ef6&#xff09;<br \/>\n    virtual void updateChannel(Channel* channel)&#061;0;<br \/>\n    \/\/ \u5220\u9664\u5df2\u6ce8\u518c\u7684\u4e8b\u4ef6&#xff08;\u4f7f\u7528Channel\u5305\u88c5\u7684\u4e8b\u4ef6&#xff09;&#xff0c;\u5728Channel\u88ab\u5173\u95ed\u6216\u8005\u88ab\u9500&#xfffd;&#xfffd;\u65f6\u88ab\u8c03\u7528&#xff0c;\u5e76\u5728Poller\u4e2d\u5220\u9664\u8be5channel\u5e76\u5173\u95edepoll\u7684\u53e5&#xfffd;&#xfffd;<br \/>\n    virtual void removeChannel(Channel* channel)&#061;0;<br \/>\n    \/\/ \u68c0\u6d4b\u5f53\u524d\u662f\u5426\u6709\u4e8b\u4ef6(\u4f7f\u7528Channel\u5305\u88c5\u7684\u4e8b\u4ef6)\u76d1\u542c<br \/>\n    bool hasChannel(Channel* channel)const;<br \/>\n    \/\/ \u521b\u5efa\u5e76\u8fd4\u56de\u4e00\u4e2a Poller \u7c7b\u578b\u7684\u5bf9\u8c61\u6307\u9488<br \/>\n    \/\/ \u4e3a\u4ec0\u4e48\u8981\u8bbe\u7f6e\u4e3astatic&#xff1a;\u9759\u6001\u65b9\u6cd5\u53ef\u4ee5\u4e0d\u4f9d\u8d56\u4e8e\u7c7b\u7684\u4efb\u4f55\u5b9e\u4f8b\u6210\u5458\u53d8\u91cf\u6216\u5b9e\u4f8b\u65b9\u6cd5\u3002\u8fd9\u610f\u5473\u7740\u4e0d\u9700\u8981\u521b\u5efa Poller \u7c7b\u7684\u5bf9\u8c61\u5c31\u53ef\u4ee5\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\u3002<br \/>\n    static Poller* newDefaultPoller(EventLoop* loop);<\/p>\n<p>protected:<br \/>\n    using ChannelMap&#061;std::unordered_map&lt;int, Channel*&gt;;<br \/>\n    \/\/ \u76d1\u542c\u7684fd\u4e0echannel\u952e\u503c\u5bf9&#xff0c;\u65b9\u4fbf\u79fb\u9664\u589e\u52a0\u76d1\u542c\u4e8b\u4ef6&#xff08;channel&#xff09;<br \/>\n    ChannelMap channels_;<br \/>\nprivate:<br \/>\n    EventLoop* ownerLoop_;\/\/ \u5b9a\u4e49Poller\u6240\u5c5e\u7684\u4e8b\u4ef6\u5faa\u73afEventLoop<br \/>\n};<\/p>\n<p>#endif \/\/ POLLER_H<\/p>\n<p>\/\/ \u5b9e\u73b0Poller.cc<br \/>\n#include &#034;Poller.h&#034;<\/p>\n<p>\/\/\u6784\u9020\u51fd\u6570\u4f20\u5165\u6240\u5c5e\u7684eventloop\u5373\u53ef&#xff0c;\u8ba9poller\u77e5\u9053\u81ea\u5df1\u6240\u5c5e\u4e0e\u54ea\u4e2a\u7ebf\u7a0b<br \/>\n\/\/ \u4e3a\u4ec0\u4e48\u8981\u77e5\u9053\u81ea\u5df1\u5c5e\u4e8e\u90a3\u4e2a\u7ebf\u7a0b\u5462<br \/>\n\/\/ \u56e0\u4e3a\u5728\u540e\u7eed\u7684multi-reactor\u4e2d\u4f1a\u6709\u4e2a\u7ebf\u7a0b\u6c60\u7684\u6210\u5458<br \/>\n\/\/ \u5728\u7ebf\u7a0b\u6c60\u4e2d\u6240\u6709\u7684\u7ebf\u7a0b\u90fd\u88ab\u5206\u914d\u4e86\u4e00\u4e2apoller&#xff0c;\u7ebf\u7a0b\u4e0d\u65ad\u4fa6\u542c\u8fd9\u4e2apoller&#xff0c;\u7ba1\u7406\u4e8b\u4ef6<br \/>\nPoller::Poller(EventLoop* eventLoop)<br \/>\n    : ownerLoop_(eventLoop){<\/p>\n<p>    }<br \/>\n\/\/ \u67e5\u627e\u8fd9\u4e2aChannel\u662f\u5426\u6709\u76ee\u6807channel<br \/>\nbool Poller::hasChannel(Channel* channel)const{<br \/>\n    auto it&#061;channels_.find(channel-&gt;fd());<br \/>\n    return it!&#061;channels_.end()&amp;&amp;it-&gt;second&#061;&#061;channel;<br \/>\n} <\/p>\n<h4>2.1.2 EPollPoller\u7c7b<\/h4>\n<h5>\u5b9e\u73b0\u7ec6\u8282<\/h5>\n<p>\u4e3a\u4e86\u5229\u7528 epoll \u9ad8\u6548\u5730\u7ba1\u7406\u5927\u91cf\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u6211\u4eec\u5728\u521d\u59cb\u5316 EPollPoller \u65f6\u4f1a\u8c03\u7528 epoll_create \u51fd\u6570\u6765\u521b\u5efa\u4e00\u4e2a epoll \u5b9e\u4f8b\u3002\u8fd9\u91cc&#xff0c;kInitEventListSize \u53c2\u6570\u7528\u4e8e\u6307\u5b9a\u8be5 epoll \u5b9e\u4f8b\u521d\u59cb\u76d1\u542c\u5217\u8868\u7684\u5927\u5c0f&#xff0c;\u5373\u5b83\u9884\u5206\u914d\u7684\u7a7a\u95f4\u5927\u5c0f&#xff0c;\u4ee5\u4f18\u5316\u6027\u80fd\u5e76\u51cf\u5c11\u52a8\u6001\u6269\u5c55\u7684\u9700\u6c42\u3002<\/p>\n<h5>\u6838\u5fc3\u65b9\u6cd5&#xff1a;poll<\/h5>\n<p>poll \u65b9\u6cd5\u662f EPollPoller \u7c7b\u4e2d\u5bf9 Poller \u62bd\u8c61\u7c7b\u6240\u5b9a\u4e49\u7684\u7eaf\u865a\u51fd\u6570 poll \u7684\u5177\u4f53\u5b9e\u73b0\u3002\u5b83\u7684\u4e3b\u8981\u804c\u8d23\u662f\u901a\u8fc7\u8c03\u7528 epoll_wait \u6765\u7b49\u5f85\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0a\u7684\u4e8b\u4ef6&#xff0c;\u5e76\u5904\u7406\u8fd9\u4e9b\u4e8b\u4ef6\u3002\u5177\u4f53\u6765\u8bf4&#xff0c;\u5176\u5de5\u4f5c\u6d41\u7a0b\u5982\u4e0b&#xff1a;<\/p>\n<li>\n<p>\u6267\u884c epoll_wait&#xff1a;\u6b64\u6b65\u9aa4\u7528\u4e8e\u76d1\u63a7\u6240\u6709\u5df2\u6ce8\u518c\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u7b49\u5f85\u5176\u4e2d\u4efb\u4f55\u4e00\u4e2a\u53d8\u4e3a\u201c\u5c31\u7eea\u201d\u72b6\u6001&#xff08;\u4f8b\u5982&#xff0c;\u53ef\u4ee5\u8fdb\u884c\u8bfb\u5199\u64cd\u4f5c&#xff09;\u3002\u53c2\u6570 timeout \u6307\u5b9a\u4e86\u7b49\u5f85\u7684\u6700\u5927\u65f6\u957f&#xff0c;\u5982\u679c\u5728\u6b64\u671f\u95f4\u6ca1\u6709\u4e8b\u4ef6\u53d1\u751f&#xff0c;\u5219\u51fd\u6570\u8fd4\u56de\u3002<\/p>\n<\/li>\n<li>\n<p>\u53d1\u73b0\u5c31\u7eea\u4e8b\u4ef6&#xff1a;\u4e00\u65e6 epoll_wait \u8fd4\u56de&#xff0c;\u5b83\u5c06\u63d0\u4f9b\u4e00\u7ec4\u5df2\u51c6\u5907\u597d\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u53ca\u5176\u5bf9\u5e94\u7684\u4e8b\u4ef6\u7c7b\u578b\u3002<\/p>\n<\/li>\n<li>\n<p>\u586b\u5145\u6d3b\u8dc3\u901a\u9053&#xff1a;\u901a\u8fc7\u8c03\u7528\u79c1\u6709\u6210\u5458\u65b9\u6cd5 fillActiveChannels&#xff0c;\u6839\u636e epoll_wait \u8fd4\u56de\u7684\u7ed3\u679c&#xff0c;\u5c06\u6240\u6709\u5c31\u7eea\u4e8b\u4ef6\u5bf9\u5e94\u7684\u901a\u9053\u4fe1\u606f\u586b\u5145\u5230 activeChannels \u5217\u8868\u4e2d\u3002\u8fd9\u4e00\u6b65\u9aa4\u786e\u4fdd\u4e86\u5916\u90e8\u4ee3\u7801\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bbf\u95ee\u5e76\u5904\u7406\u8fd9\u4e9b\u4e8b\u4ef6\u3002<\/p>\n<\/li>\n<li>\n<p>\u4ea4\u7531\u5916\u90e8\u5904\u7406&#xff1a;\u6700\u7ec8&#xff0c;poll \u65b9\u6cd5\u8fd4\u56de\u5305\u542b\u65f6\u95f4\u6233\u548c\u6d3b\u8dc3\u901a\u9053\u5217\u8868\u7684\u4fe1\u606f&#xff0c;\u4f9b\u5916\u90e8\u903b\u8f91\u8fdb\u4e00\u6b65\u5904\u7406\u3002\u6bd4\u5982\u89e6\u53d1\u76f8\u5e94\u7684\u56de\u8c03\u51fd\u6570&#xff0c;\u6267\u884c\u5177\u4f53\u7684\u4e1a\u52a1\u903b\u8f91\u7b49\u3002<\/p>\n<\/li>\n<p>\u00a0\u5728\u8fd9\u4e2aEventLoop\u4ee3\u7801\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u8c03\u7528poll\u7684\u65b9\u6cd5&#xff08;EventLoop\u540e\u7eed\u8be6\u7ec6\u4ecb\u7ecd&#xff09;&#xff1a;<\/p>\n<p>void EventLoop::loop(){<br \/>\n    looping_&#061;true;<br \/>\n    quit_&#061;false;<\/p>\n<p>    while(!quit_){<br \/>\n        activeChannels_.clear();<br \/>\n        \/\/ \u8c03\u7528Poller\u7684poll\u51fd\u6570\u83b7\u53d6\u611f\u5174\u7684\u4e8b\u4ef6<br \/>\n        pollReturnTime_&#061;poller_-&gt;poll(kPollTimeMs, &amp;activeChannels_);<br \/>\n        \/\/ \u5904\u7406\u611f\u5174\u8da3\u7684\u4e8b\u4ef6<br \/>\n        for(Channel* channel : activeChannels_){<br \/>\n            currentActiveChannel_ &#061; channel;<br \/>\n            \/\/ \u5904\u7406channel\u4e0a\u53d1\u751f\u7684\u4e8b\u4ef6<br \/>\n            channel-&gt;handleEvent(pollReturnTime_);<br \/>\n        }<br \/>\n        \/\/ \u6267\u884c\u5f53\u524dEventLoop\u4e8b\u4ef6\u5faa\u73af\u9700\u8981\u5904\u7406\u7684\u56de\u8c03\u64cd\u4f5c<br \/>\n        \/**<br \/>\n         * IO thread&#xff1a;mainLoop accept fd \u6253\u5305\u6210 chennel \u5206\u53d1\u7ed9 subLoop<br \/>\n         * mainLoop\u5b9e\u73b0\u6ce8\u518c\u4e00\u4e2a\u56de\u8c03&#xff0c;\u4ea4\u7ed9subLoop\u6765\u6267\u884c&#xff0c;wakeup subLoop \u4e4b\u540e&#xff0c;\u8ba9\u5176\u6267\u884c\u6ce8\u518c\u7684\u56de\u8c03\u64cd\u4f5c<br \/>\n         * \u8fd9\u4e9b\u56de\u8c03\u51fd\u6570\u5728 std::vector&lt;Functor&gt; pendingFunctors_; \u4e4b\u4e2d<br \/>\n         *\/<br \/>\n        doPendingFunctors();<br \/>\n    }<br \/>\n    looping_&#061;false;<br \/>\n} <\/p>\n<p>\u5173\u952e\u6210\u5458\u53d8\u91cf<\/p>\n<ul>\n<li>\n<p>int epollfd_;&#xff1a;\u8fd9\u662f\u7531 epoll_create \u521b\u5efa\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u7528\u4e8e\u540e\u7eed\u7684\u6240\u6709 epoll \u64cd\u4f5c\u3002<\/p>\n<\/li>\n<li>\n<p>std::vector&lt;epoll_event&gt; events_;&#xff1a;\u8fd9\u662f\u4e00\u4e2a\u52a8\u6001\u6570\u7ec4&#xff0c;\u7528\u6765\u5b58\u50a8\u4ece epoll_wait \u8c03\u7528\u4e2d\u83b7\u53d6\u5230\u7684\u4e8b\u4ef6\u4fe1\u606f\u3002\u5176\u521d\u59cb\u5927\u5c0f\u7531 kInitEventListSize \u786e\u5b9a&#xff0c;\u4f46\u53ef\u4ee5\u6839\u636e\u9700\u8981\u81ea\u52a8\u6269\u5c55\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u540c\u65f6updateChannel\u3001removeChannel\u662f\u4e3a\u4e86\u65b9\u4fbf\u589e\u52a0\u3001\u5220\u9664channel\u76d1\u542c\u7684\u4e8b\u4ef6\u8282\u70b9\u3002<\/p>\n<p>EPollPoller\u8be6\u7ec6\u4ee3\u7801\u5982\u4e0b&#xff1a;<\/p>\n<p>\/\/ EPollPoller<\/p>\n<p>#ifndef EPOLLPOLLER_H<br \/>\n#define EPOLLPOLLER_H<\/p>\n<p>#include &lt;vector&gt;<br \/>\n#include &lt;sys\/epoll.h&gt;<br \/>\n#include &lt;unistd.h&gt;<\/p>\n<p>#include &#034;Poller.h&#034;<br \/>\n#include &#034;Timestamp.h&#034;<\/p>\n<p>class EPollPoller:public Poller{<br \/>\npublic:<br \/>\n    EPollPoller(EventLoop* loop);<br \/>\n    ~EPollPoller() override;<\/p>\n<p>    Timestamp poll(int timeout, ChannelList* activeChannels) override;<br \/>\n    void updateChannel(Channel* channel) override;<br \/>\n    void removeChannel(Channel* channel) override;<\/p>\n<p>private:<br \/>\n    \/\/ \u9ed8\u8ba4\u76d1\u542c\u6570\u91cf<br \/>\n    static const int kInitEventListSize &#061; 16;<br \/>\n    \/\/ \u586b\u5199\u6d3b\u8dc3\u7684\u8fde\u63a5<br \/>\n    void fillActiveChannels(int numEvents,ChannelList* activeChannels)const;<br \/>\n    \/\/ \u66f4\u65b0channel\u901a\u9053&#xff0c;\u672c\u8d28\u662f\u8c03\u7528\u4e86epoll_ctl<br \/>\n    void update(int operation,Channel* channel);<\/p>\n<p>    int epollfd_;   \/\/epoll_create\u5728\u5185\u6838\u521b\u5efa\u7a7a\u95f4\u8fd4\u56de\u7684fd<br \/>\n    std::vector&lt;epoll_event&gt; events_;<br \/>\n};<\/p>\n<p>#endif<\/p>\n<p>\/\/ EPollPoller.cc<\/p>\n<p>#include &#034;EventLoop.h&#034;<br \/>\n#include &#034;Poller.h&#034;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;sys\/eventfd.h&gt;<br \/>\n#include &lt;fcntl.h&gt;<br \/>\n\/\/ \u9632\u6b62\u4e00\u4e2a\u7ebf\u7a0b\u521b\u5efa\u591a\u4e2aEventLoop (thread_local)<br \/>\n__thread EventLoop *t_loopInThisThread &#061; nullptr;<br \/>\n\/\/ \u5b9a\u4e49\u9ed8\u8ba4\u7684Poller IO\u590d\u7528\u63a5\u53e3\u7684\u8d85\u65f6\u65f6\u95f4<br \/>\nconst int kPollTimeMs&#061;10000;<\/p>\n<p>\/\/ eventfd\u590d\u7528<br \/>\nint createEventfd(){<br \/>\n    int eventfd &#061; ::eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);<br \/>\n    if(eventfd &lt; 0){<br \/>\n        perror(&#034;eventfd&#034;);<br \/>\n        abort();<br \/>\n    }<br \/>\n    return eventfd;<br \/>\n}<\/p>\n<p>EventLoop::EventLoop():<br \/>\n    looping_(false),<br \/>\n    quit_(false),<br \/>\n    callingPendingFunctors_(false),<br \/>\n    threadId_(CurrentThread::tid()),<br \/>\n    poller_(Poller::newDefaultPoller(this)),<br \/>\n    timerQueue_(new TimerQueue(this)),<br \/>\n    wakeupFd_(createEventfd()),<br \/>\n    wakeupChannel_(new Channel(this,wakeupFd_)),<br \/>\n    currentActiveChannel_(nullptr){<br \/>\n    \/\/ \u8ba9mainLoop\u548cwakeupChannel\u5728\u540c\u4e00\u4e2a\u7ebf\u7a0b\u4e2d\u6267\u884c<br \/>\n    if(t_loopInThisThread){<br \/>\n        std::cerr&lt;&lt; &#034;Another EventLoop&#034; &lt;&lt; t_loopInThisThread &lt;&lt; &#034; exists in this thread &#034; &lt;&lt; threadId_&lt;&lt;std::endl;<br \/>\n    }else{<br \/>\n        t_loopInThisThread &#061; this;<br \/>\n    }<\/p>\n<p>    \/\/ \u8bbe\u7f6ewakeupfd\u7684\u4e8b\u4ef6\u7c7b\u578b\u4ee5\u53ca\u53d1\u751f\u4e8b\u4ef6\u7684\u56de\u8c03\u51fd\u6570<br \/>\n    wakeupChannel_-&gt;setReadCallback(std::bind(&amp;EventLoop::handleRead, this));<br \/>\n    \/\/ \u6bcf\u4e00\u4e2aEventLoop\u90fd\u76d1\u542cwakeChannel\u7684EPOLLIN\u4e8b\u4ef6<br \/>\n    wakeupChannel_-&gt;enableReading();<br \/>\n}<\/p>\n<p>EventLoop::~EventLoop(){<br \/>\n    \/\/\u79fb\u9664\u6240\u6709\u611f\u5174\u8da3\u7684\u4e8b\u4ef6<br \/>\n    wakeupChannel_-&gt;disableAll();<br \/>\n    \/\/ \u5c06channel\u4eceEentLoop\u4e2d\u5220\u9664<br \/>\n    wakeupChannel_-&gt;remove();<br \/>\n    ::close(wakeupFd_);<br \/>\n    \/\/\u6307\u5411EventLoop\u7684\u6307\u9488\u81f3\u7a7a<br \/>\n    t_loopInThisThread &#061; nullptr;<br \/>\n}<\/p>\n<p>void EventLoop::loop(){<br \/>\n    looping_&#061;true;<br \/>\n    quit_&#061;false;<\/p>\n<p>    while(!quit_){<br \/>\n        activeChannels_.clear();<br \/>\n        \/\/ \u8c03\u7528Poller\u7684poll\u51fd\u6570\u83b7\u53d6\u611f\u5174\u7684\u4e8b\u4ef6<br \/>\n        pollReturnTime_&#061;poller_-&gt;poll(kPollTimeMs, &amp;activeChannels_);<br \/>\n        \/\/ \u5904\u7406\u611f\u5174\u8da3\u7684\u4e8b\u4ef6<br \/>\n        for(Channel* channel : activeChannels_){<br \/>\n            currentActiveChannel_ &#061; channel;<br \/>\n            \/\/ \u5904\u7406channel\u4e0a\u53d1\u751f\u7684\u4e8b\u4ef6<br \/>\n            channel-&gt;handleEvent(pollReturnTime_);<br \/>\n        }<br \/>\n        \/\/ \u6267\u884c\u5f53\u524dEventLoop\u4e8b\u4ef6\u5faa\u73af\u9700\u8981\u5904\u7406\u7684\u56de\u8c03\u64cd\u4f5c<br \/>\n        \/**<br \/>\n         * IO thread&#xff1a;mainLoop accept fd \u6253\u5305\u6210 chennel \u5206\u53d1\u7ed9 subLoop<br \/>\n         * mainLoop\u5b9e\u73b0\u6ce8\u518c\u4e00\u4e2a\u56de\u8c03&#xff0c;\u4ea4\u7ed9subLoop\u6765\u6267\u884c&#xff0c;wakeup subLoop \u4e4b\u540e&#xff0c;\u8ba9\u5176\u6267\u884c\u6ce8\u518c\u7684\u56de\u8c03\u64cd\u4f5c<br \/>\n         * \u8fd9\u4e9b\u56de\u8c03\u51fd\u6570\u5728 std::vector&lt;Functor&gt; pendingFunctors_; \u4e4b\u4e2d<br \/>\n         *\/<br \/>\n        doPendingFunctors();<br \/>\n    }<br \/>\n    looping_&#061;false;<br \/>\n}<\/p>\n<p>void EventLoop::quit(){<br \/>\n    quit_&#061;true;<br \/>\n    \/**<br \/>\n     * TODO:\u751f\u4ea7\u8005\u6d88\u8d39\u8005\u961f\u5217\u6d3e\u53d1\u65b9\u5f0f\u548cmuduo\u7684\u6d3e\u53d1\u65b9\u5f0f<br \/>\n     * \u6709\u53ef\u80fd\u662f\u522b\u7684\u7ebf\u7a0b\u8c03\u7528quit(\u8c03\u7528\u7ebf\u7a0b\u4e0d\u662f\u751f\u6210EventLoop\u5bf9\u8c61\u7684\u90a3\u4e2a\u7ebf\u7a0b)<br \/>\n     * \u6bd4\u5982\u5728\u5de5\u4f5c\u7ebf\u7a0b(subLoop)\u4e2d\u8c03\u7528\u4e86IO\u7ebf\u7a0b(mainLoop)<br \/>\n     * \u8fd9\u79cd\u60c5\u51b5\u4f1a\u5524\u9192\u4e3b\u7ebf\u7a0b<br \/>\n    *\/<br \/>\n    if(isInLoopThread()){<br \/>\n        wakeup();<br \/>\n    }<br \/>\n}<\/p>\n<p>void EventLoop::runInLoop(Functor cb){<br \/>\n    \/\/ \u6bcf\u4e2aEventLoop\u90fd\u4fdd\u5b58\u521b\u5efa\u81ea\u5df1\u7684\u7ebf\u7a0btid<br \/>\n    \/\/ \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7CurrentThread::tid()\u83b7\u53d6\u5f53\u524d\u6267\u884c\u7ebf\u7a0b\u7684tid\u7136\u540e\u548cEventLoop\u4fdd\u5b58\u7684\u8fdb\u884c\u6bd4\u8f83<br \/>\n    if(isInLoopThread()){<br \/>\n        cb();<br \/>\n    }<br \/>\n    \/\/ \u5728\u975e\u5f53\u524deventLoop\u7ebf\u7a0b\u4e2d\u6267\u884c\u56de\u8c03\u51fd\u6570&#xff0c;\u9700\u8981\u5524\u9192evevntLoop\u6240\u5728\u7ebf\u7a0b<br \/>\n    else{<br \/>\n        queueInLoop(cb);<br \/>\n    }<br \/>\n}<\/p>\n<p>void EventLoop::queueInLoop(Functor cb){<br \/>\n    {<br \/>\n        std::unique_lock&lt;std::mutex&gt; lock(mutex_);<br \/>\n        pendingFunctors_.emplace_back(cb);<br \/>\n    }<br \/>\n    \/\/ \u5524\u9192\u76f8\u5e94\u7684&#xff0c;\u9700\u8981\u6267\u884c\u4e0a\u9762\u56de\u8c03\u64cd\u4f5c\u7684loop\u7ebf\u7a0b<br \/>\n    \/**<br \/>\n     * TODO:<br \/>\n     * std::atomic_bool callingPendingFunctors_; \u6807\u5fd7\u5f53\u524dloop\u662f\u5426\u6709\u9700\u8981\u6267\u884c\u7684\u56de\u8c03\u64cd\u4f5c<br \/>\n     * \u8fd9\u4e2a || callingPendingFunctors_ \u6bd4\u8f83\u6709\u5fc5\u8981&#xff0c;\u56e0\u4e3a\u5728\u6267\u884c\u56de\u8c03\u7684\u8fc7\u7a0b\u53ef\u80fd\u4f1a\u52a0\u5165\u65b0\u7684\u56de\u8c03<br \/>\n     * \u5219\u8fd9\u4e2a\u65f6\u5019\u4e5f\u9700\u8981\u5524\u9192&#xff0c;\u5426\u5219\u5c31\u4f1a\u53d1\u751f\u6709\u4e8b\u4ef6\u5230\u6765\u4f46\u662f\u4ecd\u88ab\u963b\u585e\u4f4f\u7684\u60c5\u51b5<br \/>\n     *\/<br \/>\n    if(!isInLoopThread()||callingPendingFunctors_){<br \/>\n        \/\/\u5524\u9192loop\u6240\u5728\u7ebf\u7a0b<br \/>\n        wakeup();<br \/>\n    }<br \/>\n}<\/p>\n<p>void EventLoop::wakeup(){<br \/>\n    uint64_t one &#061; 1;<br \/>\n    ssize_t n &#061; ::write(wakeupFd_, &amp;one, sizeof one);<br \/>\n    if(n!&#061;sizeof(one)){<br \/>\n        perror(&#034;EventLoop::wakeup&#034;);<br \/>\n        abort();<br \/>\n    }<br \/>\n}<\/p>\n<p>void EventLoop::handleRead(){<br \/>\n    uint64_t one &#061; 1;<br \/>\n    ssize_t n &#061; ::read(wakeupFd_, &amp;one, sizeof(one));<br \/>\n    \/\/ \u5ffd\u7565\u8bfb\u53d6\u7684\u5b57\u8282\u6570<br \/>\n    if(n!&#061;sizeof(one)){<br \/>\n        perror(&#034;EventLoop::wakeup&#034;);<br \/>\n        abort();<br \/>\n    }<br \/>\n}<\/p>\n<p>void EventLoop::updateChannel(Channel* channel){<br \/>\n    poller_-&gt;updateChannel(channel);<br \/>\n}<\/p>\n<p>void EventLoop::removeChannel(Channel* channel){<br \/>\n    poller_-&gt;removeChannel(channel);<br \/>\n}<\/p>\n<p>bool EventLoop::hasChannel(Channel* channel){<br \/>\n    return poller_-&gt;hasChannel(channel);<br \/>\n}<\/p>\n<p>void EventLoop::doPendingFunctors(){<br \/>\n    std::vector&lt;Functor&gt; pendingFunctors;<br \/>\n    callingPendingFunctors_ &#061; true;<br \/>\n    \/**<br \/>\n     * TODO:<br \/>\n     * \u5982\u679c\u6ca1\u6709\u751f\u6210\u8fd9\u4e2a\u5c40\u90e8\u7684 functors<br \/>\n     * \u5219\u5728\u4e92\u65a5\u9501\u52a0\u6301\u4e0b&#xff0c;\u6211\u4eec\u76f4\u63a5\u904d\u5386pendingFunctors<br \/>\n     * \u5176\u4ed6\u7ebf\u7a0b\u8fd9\u4e2a\u65f6\u5019\u65e0\u6cd5\u8bbf\u95ee&#xff0c;\u65e0\u6cd5\u5411\u91cc\u9762\u6ce8\u518c\u56de\u8c03\u51fd\u6570&#xff0c;\u589e\u52a0\u670d\u52a1\u5668\u65f6\u5ef6<br \/>\n    *\/<br \/>\n    {<br \/>\n        std::unique_lock&lt;std::mutex&gt; lock(mutex_);<br \/>\n        pendingFunctors_.swap(pendingFunctors);<br \/>\n    }<\/p>\n<p>    for(const auto&amp; cb : pendingFunctors){<br \/>\n        cb();<br \/>\n    }<\/p>\n<p>    callingPendingFunctors_ &#061; false;<br \/>\n} <\/p>\n<h4>2.1.3 \u603b\u7ed3<\/h4>\n<p>\u5916\u90e8\u5982\u4f55\u521b\u5efaPoller\u7ba1\u7406\u6211\u4eec\u7684poller\u5462&#xff1f;<\/p>\n<p>\u90a3\u5f53\u7136\u662f\u901a\u8fc7Poller\u4e2d\u7684newDefaultPoller\u65b9\u6cd5\u5566\u3002\u901a\u8fc7\u8bbe\u7f6e\u7cfb\u7edf\u73af\u5883\u53d8\u91cf&#xff0c;\u6211\u4eec\u53ef\u4ee5\u81ea\u5df1\u9009\u62e9\u751f\u6210\u90a3\u79cdio\u590d\u7528\u65b9\u6cd5\u3002\u5f53\u7136&#xff0c;\u76ee\u524d\u9ed8\u8ba4\u751f\u6210\u7684\u90fd\u662fEPollPoller&#xff01;<\/p>\n<p>#include &#034;Poller.h&#034;<br \/>\n#include &#034;EPollPoller.h&#034;<\/p>\n<p>#include &lt;stdlib.h&gt;<\/p>\n<p>\/\/ \u901a\u8fc7\u73af\u5883\u53d8\u91cf\u9009\u62e9\u90a3\u79cd\u89e6\u53d1\u65b9\u5f0f<br \/>\nPoller* Poller::newDefaultPoller(EventLoop* eventLoop){<br \/>\n    if(getenv(&#034;MUDUO_USE_POLL&#034;)){<br \/>\n        return nullptr; \/\/\u751f\u6210poll\u5b9e\u4f8b<br \/>\n    }else{<br \/>\n        return new EPollPoller(eventLoop);\/\/\u751f\u6210epoll\u5b9e\u4f8b<br \/>\n    }<br \/>\n} <\/p>\n<p>\u6700\u540e\u8865\u5145\u65b0\u8fde\u63a5\u5230\u6765\u65f6\u5019\u7684\u8c03\u7528\u6d41\u7a0b&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"812\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418153210-6802707a40ed5.png\" width=\"883\" \/><\/p>\n<\/p>\n<h2>\u4e09\u3001\u6838\u5fc3\u4ee3\u7801\u7684\u5b9e\u73b0<\/h2>\n<p>\u5728\u4e0a\u4e00\u7ae0&#xff0c;\u6211\u4eec\u753b\u4e86\u4e00\u4e2a\u7b80\u6613\u7684\u8c03\u7528\u6d41\u7a0b\u56fe&#xff0c;\u5982\u4e0b&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"812\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418153210-6802707a40ed5.png\" width=\"883\" \/><\/p>\n<p>\u90a3\u4e48&#xff0c;\u6211\u4eec\u540e\u7eed\u5c06\u4f1a\u4e0d\u65ad\u5730\u57fa\u4e8e\u8fd9\u5f20\u56fe&#xff0c;\u8865\u5145\u5b8c\u6574\u7684\u8c03\u7528\u63a5\u53e3\u3002<\/p>\n<h3>3.1 Channel\u7684\u5b9e\u73b0<\/h3>\n<p>\u518d\u4e0a\u4e00\u7ae0\u6211\u4eec\u5b9e\u73b0\u4e86EPollPoller\u3002\u6211\u4eec\u8ba8\u8bba\u5230\u4e86\u60f3\u7528\u4e00\u4e2aChannel\u6a21\u5757\u7ba1\u7406\u6211\u4eec\u7684\u65b0\u8fde\u63a5\u7684\u76d1\u542c\u4e8b\u4ef6&#xff0c;\u5e76\u4e14\u80fd\u591f\u5bf9\u53d1\u751f\u7684\u76d1\u542c\u4e8b\u4ef6\u901a\u8fc7Channel\u5b58\u653e\u7684\u76f8\u5e94\u7684\u56de\u8c03\u51fd\u6570&#xff0c;\u5b8c\u6210\u76f8\u5e94\u7684\u76d1\u542c\u4e8b\u4ef6\u64cd\u4f5c\u3002<\/p>\n<p>\u90a3\u4e48\u6211\u4eec\u9884\u671f\u7684\u8fd9\u4e2aChannel\u8981\u5b8c\u6210\u54ea\u4e9b\u64cd\u4f5c\u5462&#xff1f;<\/p>\n<ul>\n<li>\u5b58\u653e\u76d1\u542c\u4e8b\u4ef6\u7684fd&#xff08;\u53e5\u67c4&#xff0c;\u7528\u4e8epoller\u76d1\u542c&#xff09;<\/li>\n<li>\u611f\u5174\u8da3\u7684\u4e8b\u4ef6\u6807\u5fd7&#xff08;\u7528\u4e8e\u5411poller\u8868\u9762\u6211\u5e0c\u671b\u76d1\u542c\u90a3\u4e9b\u4e8b\u4ef6&#xff09;<\/li>\n<li>\u5177\u4f53\u53d1\u751f\u65f6\u95f4\u7684\u6807\u5fd7&#xff08;poller\u4e2d\u53d1\u751f\u7684\u5177\u4f53\u7684\u4e8b\u4ef6&#xff09;<\/li>\n<li>\u56de\u8c03\u51fd\u6570&#xff08;\u4e0d\u540c\u4e8b\u4ef6\u7684\u56de\u8c03\u51fd\u6570&#xff09;<\/li>\n<\/ul>\n<h3>3.1.1 Channel\u7c7b<\/h3>\n<h5>3.1.1.1 Channel\u7684\u6784\u9020\u51fd\u6570<\/h5>\n<p>\u6784\u9020\u51fd\u6570\u6709\u4e24\u4e2a\u53c2\u6570\u7684\u8f93\u5165&#xff0c;\u7b2c\u4e00\u4e2a\u662fEventLoop&#xff08;\u5faa\u73af\u4e8b\u4ef6\u2014\u2014\u4e5f\u53ef\u6682\u65f6\u89c6\u4e3aSub-Reactor\u6216Main-Reactor&#xff09;&#xff0c;\u8868\u660e\u8be5Channel\u5c5e\u4e8e\u54ea\u4e2areactor\u8981\u76d1\u542c\u7684\u7aef\u53e3\u548c\u76f8\u5173\u4e8b\u4ef6&#xff0c;\u7b2c\u4e8c\u4e2a\u53c2\u6570\u5c31\u662f\u8be5Channel\u76d1\u542c\u7684socket\u3002\u4ee3\u7801\u5982\u4e0b&#xff1a;<\/p>\n<p>\/\/ Channel.cc<br \/>\nChannel::Channel(EventLoop *loop, int fd)<br \/>\n    :   loop_(loop),<br \/>\n        fd_(fd),<br \/>\n        events_(0),<br \/>\n        revents_(0),<br \/>\n        index_(-1),<br \/>\n        tied_(false)<br \/>\n{<br \/>\n}<\/p>\n<p>\/\/ EventLoop<br \/>\n\/\/ \u521b\u5efa\u4e8b\u4ef6fd<br \/>\nint createEventfd()<br \/>\n{<br \/>\n    int evfd &#061; ::eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);<br \/>\n    if (evfd &lt; 0)<br \/>\n    {<br \/>\n        std::cerr &lt;&lt; &#034;eventfd error: &#034; &lt;&lt; std::endl;<br \/>\n    }<br \/>\n    return evfd;<br \/>\n}<\/p>\n<p>EventLoop::EventLoop() :<br \/>\n    looping_(false),<br \/>\n    quit_(false),<br \/>\n    callingPendingFunctors_(false),<br \/>\n    threadId_(CurrentThread::tid()),<br \/>\n    poller_(Poller::newDefaultPoller(this)),<br \/>\n    timerQueue_(new TimerQueue(this)),<br \/>\n    wakeupFd_(createEventfd()),                    \/\/\u8fd9\u91cc\u521b\u5efafd<br \/>\n    wakeupChannel_(new Channel(this, wakeupFd_)),    \/\/\u5728\u8fd9\u91cc\u521b\u5efa\u65b0\u7684Channel&#xff0c;\u5e76\u628a\u521a\u521b\u5efa\u7684fd\u4f5c\u4e3a\u8f93\u5165<br \/>\n    currentActiveChannel_(nullptr){} <\/p>\n<h5>3.1.1.2 \u4e8b\u4ef6\u66f4\u65b0\u7c7b\u63a5\u53e3<\/h5>\n<p>\u5916\u90e8\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u8fd9\u4e9b\u63a5\u53e3&#xff0c;\u4ece\u800c\u8c03\u7528epoll\u7684updatechannel\u63a5\u53e3&#xff0c;\u6700\u7ec8\u8c03\u7528epoll_ctl\u66f4\u65b0\u76d1\u542c\u4e8b\u4ef6<\/p>\n<p>\/\/ Channel.h<br \/>\n  \/\/ \u8bbe\u7f6efd\u76f8\u5e94\u7684\u4e8b\u4ef6\u72b6\u6001&#xff0c;update()\u5176\u672c\u8d28\u8c03\u7528epoll_ctl<br \/>\n    void enableReading() { events_ |&#061; kReadEvent; update(); }<br \/>\n    void disableReading() { events_ &amp;&#061; ~kReadEvent; update(); }<br \/>\n    void enableWriting() { events_ |&#061; kWriteEvent; update(); }<br \/>\n    void disableWriting() { events_ &amp;&#061; ~kWriteEvent; update(); }<br \/>\n    void disableAll() { events_ &amp;&#061; kNoneEvent; update(); }<br \/>\n\/\/ Channel.cc<br \/>\n void Channel::update()<br \/>\n {<br \/>\n    \/\/TODO:Channel::update()<br \/>\n    \/\/ \u901a\u8fc7\u8be5channel\u6240\u5c5e\u7684EventLoop&#xff0c;\u8c03\u7528poller\u5bf9\u5e94\u7684\u65b9\u6cd5&#xff0c;\u6ce8\u518cfd\u7684events\u4e8b\u4ef6<br \/>\n    loop_-&gt;updateChannel(this);<br \/>\n }<\/p>\n<p>\/\/ loop\u8c03\u7528 EventLoop.cc<\/p>\n<p>void EventLoop::updateChannel(Channel *channel)<br \/>\n{<br \/>\n    poller_-&gt;updateChannel(channel);<br \/>\n}<\/p>\n<p>\/\/ \u968f\u540e\u8c03\u7528updateChannel&#xff0c;\u4f7f\u7528epollpoller\u7684update\u63a5\u53e3&#xff0c;\u6700\u7ec8\u8c03\u7528epoll_ctl&#xff0c;\u5b8c\u6210\u5bf9\u76f8\u5173\u76d1\u542c\u4e8b\u4ef6\u7684\u66f4\u65b0<br \/>\nvoid EPollPoller::updateChannel(Channel *channel)<br \/>\n{<br \/>\n    \/\/ TODO:__FUNCTION__<br \/>\n    \/\/ \u83b7\u53d6\u53c2\u6570channel\u5728epoll\u7684\u72b6\u6001<br \/>\n    const int index &#061; channel-&gt;index();<\/p>\n<p>    \/\/ \u672a\u6dfb\u52a0\u72b6\u6001\u548c\u5df2\u5220\u9664\u72b6\u6001\u90fd\u6709\u53ef\u80fd\u4f1a\u88ab\u518d\u6b21\u6dfb\u52a0\u5230epoll\u4e2d<br \/>\n    if (index &#061;&#061; kNew || index &#061;&#061; kDeleted)<br \/>\n    {<br \/>\n        \/\/ \u6dfb\u52a0\u5230\u952e\u503c\u5bf9<br \/>\n        if (index &#061;&#061; kNew)<br \/>\n        {<br \/>\n            int fd &#061; channel-&gt;fd();<br \/>\n            channels_[fd] &#061; channel;<br \/>\n        }<br \/>\n        else \/\/ index &#061;&#061; kAdd<br \/>\n        {<br \/>\n        }<br \/>\n        \/\/ \u4fee\u6539channel\u7684\u72b6\u6001&#xff0c;\u6b64\u65f6\u662f\u5df2\u6dfb\u52a0\u72b6\u6001<br \/>\n        channel-&gt;set_index(kAdded);<br \/>\n        \/\/ \u5411epoll\u5bf9\u8c61\u52a0\u5165channel<br \/>\n        update(EPOLL_CTL_ADD, channel);<br \/>\n    }<br \/>\n    \/\/ channel\u5df2\u7ecf\u5728poller\u4e0a\u6ce8\u518c\u8fc7<br \/>\n    else<br \/>\n    {<br \/>\n        \/\/ \u6ca1\u6709\u611f\u5174\u8da3\u4e8b\u4ef6\u8bf4\u660e\u53ef\u4ee5\u4eceepoll\u5bf9\u8c61\u4e2d\u5220\u9664\u8be5channel\u4e86<br \/>\n        if (channel-&gt;isNoneEvent())<br \/>\n        {<br \/>\n            update(EPOLL_CTL_DEL, channel);<br \/>\n            channel-&gt;set_index(kDeleted);<br \/>\n        }<br \/>\n        \/\/ \u8fd8\u6709\u4e8b\u4ef6\u8bf4\u660e\u4e4b\u524d\u7684\u4e8b\u4ef6\u5220\u9664&#xff0c;\u4f46\u662f\u88ab\u4fee\u6539\u4e86<br \/>\n        else<br \/>\n        {<br \/>\n            update(EPOLL_CTL_MOD, channel);<br \/>\n        }<br \/>\n    }<br \/>\n}<br \/>\n\/\/ \u8f93\u5165\u5bf9\u76d1\u542c\u4e8b\u4ef6\u7684\u64cd\u4f5coperation&#xff0c;\u548c\u5bf9\u5e94\u7684\u4e8b\u4ef6channel<br \/>\nvoid EPollPoller::update(int operation, Channel *channel)<br \/>\n{<br \/>\n    epoll_event event;<br \/>\n    ::memset(&amp;event, 0, sizeof(event));<\/p>\n<p>    int fd &#061; channel-&gt;fd();<br \/>\n    event.events &#061; channel-&gt;events();<br \/>\n    event.data.fd &#061; fd;<br \/>\n    event.data.ptr &#061; channel;<\/p>\n<p>    if (::epoll_ctl(epollfd_, operation, fd, &amp;event) &lt; 0)<br \/>\n    {<br \/>\n        if (operation &#061;&#061; EPOLL_CTL_DEL)<br \/>\n        {<br \/>\n            LOG_ERROR &lt;&lt; &#034;epoll_ctl() del error:&#034; &lt;&lt; errno;<br \/>\n        }<br \/>\n        else<br \/>\n        {<br \/>\n            LOG_FATAL &lt;&lt; &#034;epoll_ctl add\/mod error:&#034; &lt;&lt; errno;<br \/>\n        }<br \/>\n    }<br \/>\n} <\/p>\n<h5>\u5b8c\u6574\u7684Channel\u4ee3\u7801<\/h5>\n<p>\u5b8c\u6574\u7684Channel\u4ee3\u7801\u5982\u4e0b&#xff1a;<\/p>\n<p>\/\/ Channel.h<br \/>\n#ifndef CHANNEL_H<br \/>\n#define CHANNEL_H<\/p>\n<p>#include &lt;functional&gt;<br \/>\n#include &lt;memory&gt;<br \/>\n#include &lt;sys\/epoll.h&gt;<\/p>\n<p>#include &#034;noncopyable.h&#034;<br \/>\n#include &#034;Timestamp.h&#034;<br \/>\n#include &#034;Logging.h&#034;<\/p>\n<p>\/\/ \u524d\u7f6e\u58f0\u660e&#xff0c;\u4e0d\u5f15\u7528\u5934\u6587\u4ef6\u9632\u6b62\u66b4\u9732\u592a\u591a\u4fe1\u606f<br \/>\nclass EventLoop;<br \/>\nclass Timestamp;<\/p>\n<p>\/**<br \/>\n* Channel \u7406\u89e3\u4e3a\u901a\u9053&#xff0c;\u5c01\u88c5\u4e86sockfd\u548c\u5176\u611f\u5174\u8da3\u7684event&#xff0c;\u5982EPOLLIN,EPOLLOUT<br \/>\n* \u8fd8\u7ed1\u5b9a\u4e86poller\u8fd4\u56de\u7684\u5177\u4f53\u4e8b\u4ef6<br \/>\n*\/<br \/>\nclass Channel : noncopyable<br \/>\n{<br \/>\npublic:<br \/>\n    using EventCallback &#061; std::function&lt;void()&gt;;<br \/>\n    using ReadEventCallback &#061; std::function&lt;void(Timestamp)&gt;;<\/p>\n<p>    Channel(EventLoop *loop, int fd);<br \/>\n    ~Channel();<\/p>\n<p>    \/\/ fd\u5f97\u5230poller\u901a\u77e5\u4ee5\u540e&#xff0c;\u5904\u7406\u4e8b\u4ef6\u7684\u56de\u8c03\u51fd\u6570<br \/>\n    void handleEvent(Timestamp receiveTime);<\/p>\n<p>    \/\/ \u8bbe\u7f6e\u56de\u8c03\u51fd\u6570\u5bf9\u8c61<br \/>\n    \/\/ \u4f7f\u7528\u53f3\u503c\u5f15\u7528&#xff0c;\u5ef6\u957f\u4e86cb\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f&#xff0c;\u907f\u514d\u4e86\u62f7\u8d1d\u64cd\u4f5c<br \/>\n    void setReadCallback(ReadEventCallback cb) { readCallback_ &#061; std::move(cb); }<br \/>\n    void setWriteCallback(EventCallback cb) { writeCallback_ &#061; std::move(cb); }<br \/>\n    void setCloseCallback(EventCallback cb) { closeCallback_ &#061; std::move(cb); }<br \/>\n    void setErrorCallback(EventCallback cb) { errorCallback_ &#061; std::move(cb); }<\/p>\n<p>    \/\/ TODO:\u9632\u6b62\u5f53 channel \u6267\u884c\u56de\u8c03\u51fd\u6570\u65f6\u88ab\u88ab\u624b\u52a8 remove \u6389<br \/>\n    void tie(const std::shared_ptr&lt;void&gt;&amp;);<\/p>\n<p>    int fd() const { return fd_; }                    \/\/ \u8fd4\u56de\u5c01\u88c5\u7684fd<br \/>\n    int events() const { return events_; }            \/\/ \u8fd4\u56de\u611f\u5174\u8da3\u7684\u4e8b\u4ef6<br \/>\n    void  set_revents(int revt) { revents_ &#061; revt; }  \/\/ \u8bbe\u7f6ePoller\u8fd4\u56de\u7684\u53d1\u751f\u4e8b\u4ef6<\/p>\n<p>    \/\/ \u8bbe\u7f6efd\u76f8\u5e94\u7684\u4e8b\u4ef6\u72b6\u6001&#xff0c;update()\u5176\u672c\u8d28\u8c03\u7528epoll_ctl<br \/>\n    void enableReading() { events_ |&#061; kReadEvent; update(); }<br \/>\n    void disableReading() { events_ &amp;&#061; ~kReadEvent; update(); }<br \/>\n    void enableWriting() { events_ |&#061; kWriteEvent; update(); }<br \/>\n    void disableWriting() { events_ &amp;&#061; ~kWriteEvent; update(); }<br \/>\n    void disableAll() { events_ &amp;&#061; kNoneEvent; update(); }<\/p>\n<p>    \/\/ \u8fd4\u56defd\u5f53\u524d\u7684\u4e8b\u4ef6\u72b6\u6001<br \/>\n    bool isNoneEvent() const { return events_ &#061;&#061; kNoneEvent; }<br \/>\n    bool isWriting() const { return events_ &amp; kWriteEvent; }<br \/>\n    bool isReading() const { return events_ &amp; kReadEvent; }<\/p>\n<p>    \/**<br \/>\n     * for Poller<br \/>\n     * const int kNew &#061; -1;     \/\/ fd\u8fd8\u672a\u88abpoller\u76d1\u89c6<br \/>\n     * const int kAdded &#061; 1;    \/\/ fd\u6b63\u88abpoller\u76d1\u89c6\u4e2d<br \/>\n     * const int kDeleted &#061; 2;  \/\/ fd\u88ab\u79fb\u9664poller<br \/>\n     *\/<br \/>\n    int index() { return index_; }<br \/>\n    void set_index(int idx) { index_ &#061; idx; }<\/p>\n<p>    \/\/ one lool per thread<br \/>\n    EventLoop* ownerLoop() { return loop_; }<br \/>\n    void remove();<\/p>\n<p>private:<br \/>\n    void update();<br \/>\n    void handleEventWithGuard(Timestamp receiveTime);<\/p>\n<p>    \/**<br \/>\n     * const int Channel::kNoneEvent &#061; 0;<br \/>\n     * const int Channel::kReadEvent &#061; EPOLLIN | EPOLLPRI;<br \/>\n     * const int Channel::kWriteEvent &#061; EPOLLOUT;<br \/>\n     *\/<br \/>\n    static const int kNoneEvent;<br \/>\n    static const int kReadEvent;<br \/>\n    static const int kWriteEvent;<\/p>\n<p>    EventLoop *loop_;   \/\/ \u5f53\u524dChannel\u5c5e\u4e8e\u7684EventLoop<br \/>\n    const int fd_;      \/\/ fd, Poller\u76d1\u542c\u5bf9\u8c61<br \/>\n    int events_;        \/\/ \u6ce8\u518cfd\u611f\u5174\u8da3\u7684\u4e8b\u4ef6<br \/>\n    int revents_;       \/\/ poller\u8fd4\u56de\u7684\u5177\u4f53\u53d1\u751f\u7684\u4e8b\u4ef6<br \/>\n    int index_;         \/\/ \u5728Poller\u4e0a\u6ce8\u518c\u7684\u60c5\u51b5<\/p>\n<p>    std::weak_ptr&lt;void&gt; tie_;   \/\/ \u5f31\u6307\u9488\u6307\u5411TcpConnection(\u5fc5\u8981\u65f6\u5347\u7ea7\u4e3ashared_ptr\u591a\u4e00\u4efd\u5f15\u7528\u8ba1\u6570&#xff0c;\u907f\u514d\u7528\u6237\u8bef\u5220)<br \/>\n    bool tied_;  \/\/ \u6807\u5fd7\u6b64 Channel \u662f\u5426\u88ab\u8c03\u7528\u8fc7 Channel::tie \u65b9\u6cd5<\/p>\n<p>    \/\/ \u56e0\u4e3a channel \u901a\u9053\u91cc\u9762\u80fd\u591f\u83b7\u77e5fd\u6700\u7ec8\u53d1\u751f\u7684\u5177\u4f53\u7684\u4e8b\u4ef6revents<br \/>\n    \/\/ \u4fdd\u5b58\u4e8b\u4ef6\u5230\u6765\u65f6\u7684\u56de\u8c03\u51fd\u6570<br \/>\n    ReadEventCallback readCallback_;<br \/>\n    EventCallback writeCallback_;<br \/>\n    EventCallback closeCallback_;<br \/>\n    EventCallback errorCallback_;<br \/>\n};<\/p>\n<p>#endif \/\/ CHANNEL_H<br \/>\n\/\/ Channel.cc<br \/>\n#include &#034;Channel.h&#034;<br \/>\n#include &#034;EventLoop.h&#034;<\/p>\n<p>const int Channel::kNoneEvent &#061; 0;<br \/>\nconst int Channel::kReadEvent &#061; EPOLLIN | EPOLLPRI;<br \/>\nconst int Channel::kWriteEvent &#061; EPOLLOUT;<\/p>\n<p>Channel::Channel(EventLoop *loop, int fd)<br \/>\n    :   loop_(loop),<br \/>\n        fd_(fd),<br \/>\n        events_(0),<br \/>\n        revents_(0),<br \/>\n        index_(-1),<br \/>\n        tied_(false)<br \/>\n{<br \/>\n}<\/p>\n<p>\/\/TODO:\u6790\u6784\u64cd\u4f5c\u548c\u65ad\u8a00&#xff0c;\u5224\u65ad\u662f\u5426\u662f\u5728\u5f53\u524d\u7ebf\u7a0b<br \/>\nChannel::~Channel()<br \/>\n{<br \/>\n}<\/p>\n<p>\/\/ \u5728TcpConnection\u5efa\u7acb\u5f97\u65f6\u5019\u4f1a\u8c03\u7528<br \/>\nvoid Channel::tie(const std::shared_ptr&lt;void&gt; &amp;obj)<br \/>\n{<br \/>\n    \/\/ weak_ptr \u6307\u5411 obj<br \/>\n    tie_ &#061; obj;<br \/>\n    tied_ &#061; true;<br \/>\n}<\/p>\n<p>\/**<br \/>\n * \u5f53\u6539\u53d8channel\u6240\u8868\u793afd\u7684events\u4e8b\u4ef6\u540e&#xff0c;update\u8d1f\u8d23\u5728poller\u91cc\u9762\u66f4\u6539fd\u76f8\u5e94\u7684\u4e8b\u4ef6epoll_ctl<br \/>\n *<br \/>\n *\/<br \/>\n void Channel::update()<br \/>\n {<br \/>\n    \/\/TODO:Channel::update()<br \/>\n    \/\/ \u901a\u8fc7\u8be5channel\u6240\u5c5e\u7684EventLoop&#xff0c;\u8c03\u7528poller\u5bf9\u5e94\u7684\u65b9\u6cd5&#xff0c;\u6ce8\u518cfd\u7684events\u4e8b\u4ef6<br \/>\n    loop_-&gt;updateChannel(this);<br \/>\n }<\/p>\n<p>\/\/ \u5728channel\u6240\u5c5e\u7684EventLoop\u4e2d&#xff0c;\u628a\u5f53\u524d\u7684channel\u5220\u9664\u6389<br \/>\nvoid Channel::remove()<br \/>\n{<br \/>\n    \/\/TODO:Channel::remove()<br \/>\n    loop_-&gt;removeChannel(this);<br \/>\n}<\/p>\n<p>\/\/ fd\u5f97\u5230poller\u901a\u77e5\u4ee5\u540e&#xff0c;\u53bb\u5904\u7406\u4e8b\u4ef6<br \/>\nvoid Channel::handleEvent(Timestamp receiveTime)<br \/>\n{<br \/>\n    \/**<br \/>\n     * \u8c03\u7528\u4e86Channel::tie\u5f97\u4f1a\u8bbe\u7f6etid_&#061;true<br \/>\n     * \u800cTcpConnection::connectEstablished\u4f1a\u8c03\u7528channel_-&gt;tie(shared_from_this());<br \/>\n     * \u6240\u4ee5\u5bf9\u4e8eTcpConnection::channel_ \u9700\u8981\u591a\u4e00\u4efd\u5f3a\u5f15\u7528\u7684\u4fdd\u8bc1\u4ee5\u514d\u7528\u6237\u8bef\u5220TcpConnection\u5bf9\u8c61<br \/>\n     *\/<br \/>\n    if (tied_)<br \/>\n    {<br \/>\n        \/\/ \u53d8\u6210shared_ptr\u589e\u52a0\u5f15\u7528\u8ba1\u6570&#xff0c;\u9632\u6b62\u8bef\u5220<br \/>\n        std::shared_ptr&lt;void&gt; guard &#061; tie_.lock();<br \/>\n        if (guard)<br \/>\n        {<br \/>\n            handleEventWithGuard(receiveTime);<br \/>\n        }<br \/>\n        \/\/ guard\u4e3a\u7a7a\u60c5\u51b5&#xff0c;\u8bf4\u660eChannel\u7684TcpConnection\u5bf9\u8c61\u5df2\u7ecf\u4e0d\u5b58\u5728\u4e86<br \/>\n    }<br \/>\n    else<br \/>\n    {<br \/>\n        handleEventWithGuard(receiveTime);<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ \u6839\u636e\u76f8\u5e94\u4e8b\u4ef6\u6267\u884c\u56de\u8c03\u64cd\u4f5c<br \/>\nvoid Channel::handleEventWithGuard(Timestamp receiveTime)<br \/>\n{<br \/>\n    \/\/ \u5bf9\u7aef\u5173\u95ed\u4e8b\u4ef6<br \/>\n    \/\/ \u5f53TcpConnection\u5bf9\u5e94Channel&#xff0c;\u901a\u8fc7shutdown\u5173\u95ed\u5199\u7aef&#xff0c;epoll\u89e6\u53d1EPOLLHUP<br \/>\n    if ((revents_ &amp; EPOLLHUP) &amp;&amp; !(revents_ &amp; EPOLLIN))<br \/>\n    {<br \/>\n        \/\/ \u786e\u8ba4\u662f\u5426\u62e5\u6709\u56de\u8c03\u51fd\u6570<br \/>\n        if (closeCallback_)<br \/>\n        {<br \/>\n            closeCallback_();<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u9519\u8bef\u4e8b\u4ef6<br \/>\n    if (revents_ &amp; (EPOLLERR))<br \/>\n    {<br \/>\n        LOG_ERROR &lt;&lt; &#034;the fd &#061; &#034; &lt;&lt; this-&gt;fd();<br \/>\n        if (errorCallback_)<br \/>\n        {<br \/>\n            errorCallback_();<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u8bfb\u4e8b\u4ef6<br \/>\n    if (revents_ &amp; (EPOLLIN | EPOLLPRI))<br \/>\n    {<br \/>\n        LOG_DEBUG &lt;&lt; &#034;channel have read events, the fd &#061; &#034; &lt;&lt; this-&gt;fd();<br \/>\n        if (readCallback_)<br \/>\n        {<br \/>\n            LOG_DEBUG &lt;&lt; &#034;channel call the readCallback_(), the fd &#061; &#034; &lt;&lt; this-&gt;fd();<br \/>\n            readCallback_(receiveTime);<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u5199\u4e8b\u4ef6<br \/>\n    if (revents_ &amp; EPOLLOUT)<br \/>\n    {<br \/>\n        if (writeCallback_)<br \/>\n        {<br \/>\n            writeCallback_();<br \/>\n        }<br \/>\n    }<br \/>\n} <\/p>\n<p>3.1.2 \u603b\u7ed3<\/p>\n<\/p>\n<h3>3.2 EventLoop\u7684\u5b9e\u73b0<\/h3>\n<p>\u51d1\u51d1\u5b57\u6570&#xff0c;\u540e\u7eed\u8865\u5145<\/p>\n<h3>3.3 Acceptor\u7684\u5b9e\u73b0<\/h3>\n<p>\u51d1\u51d1\u5b57\u6570&#xff0c;\u540e\u7eed\u8865\u5145<\/p>\n<h2 style=\"background-color:transparent\">\u56db\u3001\u5176\u4f59\u4ee3\u7801\u7684\u5b9e\u73b0<\/h2>\n<p>\u51d1\u51d1\u5b57\u6570&#xff0c;\u540e\u7eed\u8865\u5145<\/p>\n<h2>\u4e94\u3001\u5b9e\u73b0\u4e00\u4e2aserver<\/h2>\n<p>\u51d1\u51d1\u5b57\u6570&#xff0c;\u540e\u7eed\u8865\u5145<\/p>\n<h2>\u53c2\u8003<\/h2>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb818\u6b21\uff0c\u70b9\u8d5e29\u6b21\uff0c\u6536\u85cf26\u6b21\u3002\u5728\u9ad8\u6027\u80fd\u7f51\u7edc\u7f16\u7a0b\u4e2d\uff0cReactor\u6a21\u5f0f\u662f\u4e00\u79cd\u5e38\u7528\u7684\u4e8b\u4ef6\u9a71\u52a8\u67b6\u6784\u6a21\u5f0f\uff0c\u5b83\u5141\u8bb8\u670d\u52a1\u5668\u9ad8\u6548\u5730\u5904\u7406\u5927\u91cf\u5e76\u53d1\u8fde\u63a5\u3002\u800cMulti-Reactor\u6a21\u5f0f\u662f\u5bf9\u7ecf\u5178Reactor\u6a21\u5f0f\u7684\u4e00\u79cd\u6269\u5c55\u548c\u4f18\u5316\uff0c\u901a\u8fc7\u5c06\u4e8b\u4ef6\u5206\u6d3e\u4e0e\u4e8b\u4ef6\u5904\u7406\u5206\u79bb\uff0c\u5e76\u5229\u7528\u591a\u4e2aReactor\u5b9e\u4f8b\u6765\u63d0\u9ad8\u7cfb\u7edf\u6027\u80fd\u548c\u53ef\u6269\u5c55\u6027\u3002\u5728\u8fd9\u79cd\u6a21\u5f0f\u4e0b\uff0c\u901a\u5e38\u6709\u4e00\u4e2a\u4e3b\u7ebf\u7a0b\uff08\u6216\u5c11\u91cf\u7ebf\u7a0b\uff09\u8d1f\u8d23\u76d1\u542c\u65b0\u8fde\u63a5\u8bf7\u6c42\uff0c\u5e76\u5c06\u8fd9\u4e9b\u8fde\u63a5\u5206\u914d\u7ed9\u5de5\u4f5c\u7ebf\u7a0b\u6c60\u4e2d\u7684\u4e0d\u540cReactor\u5b9e\u4f8b\u8fdb\u884c\u5904\u7406\u3002\u5f53\u7136\uff0c\u8fd9\u79cd\u6a21\u578b\u4e5f\u6709\u660e\u663e\u7f3a\u70b9\uff0c\u8fde\u63a5\u5efa\u7acb\u3001IO \u4e8b\u4ef6\u8bfb\u53d6\u4ee5\u53ca\u4e8b\u4ef6\u5206\u53d1\u5b8c\u5168\u6709\u5355\u7ebf\u7a0b\u5904\u7406\uff1b<\/p>\n","protected":false},"author":2,"featured_media":17073,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[925,176],"topic":[],"class_list":["post-17076","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-925","tag-176"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u9762\u8bd5\uff1a\u4ece\u96f6\u5b9e\u73b0\u4e00\u4e2aMutli-reactor\u670d\u52a1\u5668\u2014\u2014\u9879\u76ee - \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\/17076.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u9762\u8bd5\uff1a\u4ece\u96f6\u5b9e\u73b0\u4e00\u4e2aMutli-reactor\u670d\u52a1\u5668\u2014\u2014\u9879\u76ee - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb818\u6b21\uff0c\u70b9\u8d5e29\u6b21\uff0c\u6536\u85cf26\u6b21\u3002\u5728\u9ad8\u6027\u80fd\u7f51\u7edc\u7f16\u7a0b\u4e2d\uff0cReactor\u6a21\u5f0f\u662f\u4e00\u79cd\u5e38\u7528\u7684\u4e8b\u4ef6\u9a71\u52a8\u67b6\u6784\u6a21\u5f0f\uff0c\u5b83\u5141\u8bb8\u670d\u52a1\u5668\u9ad8\u6548\u5730\u5904\u7406\u5927\u91cf\u5e76\u53d1\u8fde\u63a5\u3002\u800cMulti-Reactor\u6a21\u5f0f\u662f\u5bf9\u7ecf\u5178Reactor\u6a21\u5f0f\u7684\u4e00\u79cd\u6269\u5c55\u548c\u4f18\u5316\uff0c\u901a\u8fc7\u5c06\u4e8b\u4ef6\u5206\u6d3e\u4e0e\u4e8b\u4ef6\u5904\u7406\u5206\u79bb\uff0c\u5e76\u5229\u7528\u591a\u4e2aReactor\u5b9e\u4f8b\u6765\u63d0\u9ad8\u7cfb\u7edf\u6027\u80fd\u548c\u53ef\u6269\u5c55\u6027\u3002\u5728\u8fd9\u79cd\u6a21\u5f0f\u4e0b\uff0c\u901a\u5e38\u6709\u4e00\u4e2a\u4e3b\u7ebf\u7a0b\uff08\u6216\u5c11\u91cf\u7ebf\u7a0b\uff09\u8d1f\u8d23\u76d1\u542c\u65b0\u8fde\u63a5\u8bf7\u6c42\uff0c\u5e76\u5c06\u8fd9\u4e9b\u8fde\u63a5\u5206\u914d\u7ed9\u5de5\u4f5c\u7ebf\u7a0b\u6c60\u4e2d\u7684\u4e0d\u540cReactor\u5b9e\u4f8b\u8fdb\u884c\u5904\u7406\u3002\u5f53\u7136\uff0c\u8fd9\u79cd\u6a21\u578b\u4e5f\u6709\u660e\u663e\u7f3a\u70b9\uff0c\u8fde\u63a5\u5efa\u7acb\u3001IO \u4e8b\u4ef6\u8bfb\u53d6\u4ee5\u53ca\u4e8b\u4ef6\u5206\u53d1\u5b8c\u5168\u6709\u5355\u7ebf\u7a0b\u5904\u7406\uff1b\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/17076.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-04-18T15:32:11+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418153209-68027079a9d41.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=\"15 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/17076.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/17076.html\",\"name\":\"\u9762\u8bd5\uff1a\u4ece\u96f6\u5b9e\u73b0\u4e00\u4e2aMutli-reactor\u670d\u52a1\u5668\u2014\u2014\u9879\u76ee - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-18T15:32:11+00:00\",\"dateModified\":\"2025-04-18T15:32:11+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/17076.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/17076.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/17076.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u9762\u8bd5\uff1a\u4ece\u96f6\u5b9e\u73b0\u4e00\u4e2aMutli-reactor\u670d\u52a1\u5668\u2014\u2014\u9879\u76ee\"}]},{\"@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":"\u9762\u8bd5\uff1a\u4ece\u96f6\u5b9e\u73b0\u4e00\u4e2aMutli-reactor\u670d\u52a1\u5668\u2014\u2014\u9879\u76ee - \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\/17076.html","og_locale":"zh_CN","og_type":"article","og_title":"\u9762\u8bd5\uff1a\u4ece\u96f6\u5b9e\u73b0\u4e00\u4e2aMutli-reactor\u670d\u52a1\u5668\u2014\u2014\u9879\u76ee - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb818\u6b21\uff0c\u70b9\u8d5e29\u6b21\uff0c\u6536\u85cf26\u6b21\u3002\u5728\u9ad8\u6027\u80fd\u7f51\u7edc\u7f16\u7a0b\u4e2d\uff0cReactor\u6a21\u5f0f\u662f\u4e00\u79cd\u5e38\u7528\u7684\u4e8b\u4ef6\u9a71\u52a8\u67b6\u6784\u6a21\u5f0f\uff0c\u5b83\u5141\u8bb8\u670d\u52a1\u5668\u9ad8\u6548\u5730\u5904\u7406\u5927\u91cf\u5e76\u53d1\u8fde\u63a5\u3002\u800cMulti-Reactor\u6a21\u5f0f\u662f\u5bf9\u7ecf\u5178Reactor\u6a21\u5f0f\u7684\u4e00\u79cd\u6269\u5c55\u548c\u4f18\u5316\uff0c\u901a\u8fc7\u5c06\u4e8b\u4ef6\u5206\u6d3e\u4e0e\u4e8b\u4ef6\u5904\u7406\u5206\u79bb\uff0c\u5e76\u5229\u7528\u591a\u4e2aReactor\u5b9e\u4f8b\u6765\u63d0\u9ad8\u7cfb\u7edf\u6027\u80fd\u548c\u53ef\u6269\u5c55\u6027\u3002\u5728\u8fd9\u79cd\u6a21\u5f0f\u4e0b\uff0c\u901a\u5e38\u6709\u4e00\u4e2a\u4e3b\u7ebf\u7a0b\uff08\u6216\u5c11\u91cf\u7ebf\u7a0b\uff09\u8d1f\u8d23\u76d1\u542c\u65b0\u8fde\u63a5\u8bf7\u6c42\uff0c\u5e76\u5c06\u8fd9\u4e9b\u8fde\u63a5\u5206\u914d\u7ed9\u5de5\u4f5c\u7ebf\u7a0b\u6c60\u4e2d\u7684\u4e0d\u540cReactor\u5b9e\u4f8b\u8fdb\u884c\u5904\u7406\u3002\u5f53\u7136\uff0c\u8fd9\u79cd\u6a21\u578b\u4e5f\u6709\u660e\u663e\u7f3a\u70b9\uff0c\u8fde\u63a5\u5efa\u7acb\u3001IO \u4e8b\u4ef6\u8bfb\u53d6\u4ee5\u53ca\u4e8b\u4ef6\u5206\u53d1\u5b8c\u5168\u6709\u5355\u7ebf\u7a0b\u5904\u7406\uff1b","og_url":"https:\/\/www.wsisp.com\/helps\/17076.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-18T15:32:11+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418153209-68027079a9d41.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"15 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/17076.html","url":"https:\/\/www.wsisp.com\/helps\/17076.html","name":"\u9762\u8bd5\uff1a\u4ece\u96f6\u5b9e\u73b0\u4e00\u4e2aMutli-reactor\u670d\u52a1\u5668\u2014\u2014\u9879\u76ee - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-18T15:32:11+00:00","dateModified":"2025-04-18T15:32:11+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/17076.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/17076.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/17076.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u9762\u8bd5\uff1a\u4ece\u96f6\u5b9e\u73b0\u4e00\u4e2aMutli-reactor\u670d\u52a1\u5668\u2014\u2014\u9879\u76ee"}]},{"@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\/17076","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=17076"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/17076\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/17073"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=17076"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=17076"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=17076"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=17076"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}