{"id":60662,"date":"2026-01-16T01:05:28","date_gmt":"2026-01-15T17:05:28","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/60662.html"},"modified":"2026-01-16T01:05:28","modified_gmt":"2026-01-15T17:05:28","slug":"c%e4%bb%bfmuduo%e5%ba%93onethreadoneloop%e9%ab%98%e5%b9%b6%e5%8f%91%e6%9c%8d%e5%8a%a1%e5%99%a8","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/60662.html","title":{"rendered":"C++\u4effmuduo\u5e93onethreadoneloop\u9ad8\u5e76\u53d1\u670d\u52a1\u5668"},"content":{"rendered":"<h3>\u4e00.\u57fa\u672c\u8bbe\u8ba1\u4e0e\u6a21\u5757<\/h3>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/p>\n<p>class Buffer<br \/>\n{<br \/>\nprivate:<br \/>\n    std::vector&lt;char&gt; _buffer;<br \/>\n    uint64_t _reader_idx; \/\/ \u8bfb\u504f\u79fb\u91cf<br \/>\n    uint64_t _write_idx;  \/\/ \u5199\u504f\u79fb\u91cf<br \/>\n}<\/p>\n<p>class Socket<br \/>\n{<br \/>\nprivate:<br \/>\n    int _sockfd;<br \/>\n}<\/p>\n<p>class Channel<br \/>\n{<br \/>\nprivate:<br \/>\n    int _fd;<br \/>\n    uint32_t _events;<br \/>\n    uint32_t _revents;<br \/>\n    EventLoop *_loop;<br \/>\n    using EventCallback &#061; std::function&lt;void()&gt;;<br \/>\n    EventCallback _read_callback;  \/\/ \u53ef\u8bfb\u56de\u8c03<br \/>\n    EventCallback _write_callback; \/\/ \u53ef\u5199\u56de\u8c03<br \/>\n    EventCallback _error_callback; \/\/ \u9519\u8bef\u4e8b\u4ef6\u56de\u8c03<br \/>\n    EventCallback _close_callback; \/\/ \u8fde\u63a5\u65ad\u5f00\u56de\u8c03<br \/>\n    EventCallback _event_callback; \/\/ \u4efb\u610f\u4e8b\u4ef6\u56de\u8c03<br \/>\n}<br \/>\nclass Poller<br \/>\n{<br \/>\nprivate:<br \/>\n    int _epfd;<br \/>\n    struct epoll_event _evs[MAX_EPOLLEVENTS];<br \/>\n    std::unordered_map&lt;int, Channel *&gt; _channels;<br \/>\n}<\/p>\n<p>class Timertask<br \/>\n{<br \/>\nprivate:<br \/>\n    uint64_t _id;<br \/>\n    uint32_t _timeout; \/\/ \u5b9a\u65f6\u5668\u8d85\u65f6\u65f6\u95f4<br \/>\n    TaskFunc _task_cb; \/\/ \u5b9a\u65f6\u5668\u6267\u884c\u5b9a\u65f6\u4efb\u52a1<br \/>\n    bool _cancel;<br \/>\n    ReleaseFunc _release; \/\/ \u5220\u9664\u65f6\u95f4\u8f6e\u4e2d\u5b9a\u65f6\u5668\u5bf9\u8c61\u4fe1\u606f<br \/>\n}<br \/>\nclass Timerwheel<br \/>\n{<br \/>\nprivate:<br \/>\n    using WeakTask &#061; std::weak_ptr&lt;Timertask&gt;;<br \/>\n    using PtrTask &#061; std::shared_ptr&lt;Timertask&gt;;<br \/>\n    int _tick; \/\/ \u79d2\u9488\u8d70\u5411<br \/>\n    int _capacity;<br \/>\n    std::vector&lt;std::vector&lt;PtrTask&gt;&gt; _wheel;<br \/>\n    std::unordered_map&lt;uint64_t, WeakTask&gt; _timers; \/\/ \u8bb0\u5f55\u5b9a\u65f6\u5668\u4efb\u52a1<br \/>\n    int _timerfd;<br \/>\n    EventLoop *_loop;<br \/>\n    std::unique_ptr&lt;Channel&gt; _timer_channel;<br \/>\n}<\/p>\n<p>class EventLoop<br \/>\n{<br \/>\nprivate:<br \/>\n    using Functor &#061; std::function&lt;void()&gt;;<br \/>\n    std::thread::id _thread_id;<br \/>\n    int _event_fd;<br \/>\n    std::unique_ptr&lt;Channel&gt; _event_channel;<br \/>\n    Poller _poller;<br \/>\n    std::vector&lt;Functor&gt; _tasks;<br \/>\n    std::mutex _mutex;<br \/>\n    Timerwheel _tw;<br \/>\n}<\/p>\n<p>class Any<br \/>\n{<br \/>\nprivate:<br \/>\n    class holder<br \/>\n    {<br \/>\n    public:<br \/>\n        virtual ~holder() {}<br \/>\n        virtual const std::type_info &amp;type() &#061; 0;<br \/>\n        virtual holder *clone() &#061; 0;<br \/>\n    };<br \/>\n    template &lt;class T&gt;<br \/>\n    class placeholder : public holder<br \/>\n    {<br \/>\n    public:<br \/>\n        placeholder(const T &amp;val) : _val(val) {}<br \/>\n        virtual const std::type_info &amp;type()<br \/>\n        {<br \/>\n            return typeid(T);<br \/>\n        }<br \/>\n        virtual holder *clone()<br \/>\n        {<br \/>\n            return new placeholder(_val);<br \/>\n        }<\/p>\n<p>    public:<br \/>\n        T _val;<br \/>\n    };<br \/>\n    holder *_content;<br \/>\n}<\/p>\n<p>class LoopThread<br \/>\n{<br \/>\nprivate:<br \/>\n    std::mutex _mutex;<br \/>\n    std::condition_variable _cond;<br \/>\n    EventLoop *_loop;<br \/>\n    std::thread _thread;<br \/>\n}<\/p>\n<p>class LoopThreadPool<br \/>\n{<br \/>\nprivate:<br \/>\n    int _thread_count; \/\/ \u7ebf\u7a0b\u6570\u91cf<br \/>\n    int _next_idx;     \/\/ \u4e0b\u6807<br \/>\n    EventLoop *_baseloop;<br \/>\n    std::vector&lt;LoopThread *&gt; _threads;<br \/>\n    std::vector&lt;EventLoop *&gt; _loops;<br \/>\n}<\/p>\n<p>class Connection : public std::enable_shared_from_this&lt;Connection&gt;<br \/>\n{<br \/>\nprivate:<br \/>\n    uint64_t _conn_id;             \/\/ connection\u6807\u8bc6id&#xff0c;\u540c\u65f6\u4f5c\u4e3a\u8ba1\u65f6\u5668id<br \/>\n    int _sockfd;                   \/\/ \u8fde\u63a5\u63cf\u8ff0\u7b26<br \/>\n    bool _enable_inactive_release; \/\/ \u662f\u5426\u542f\u52a8 \u975e\u6d3b\u8dc3\u9500\u6bc1\u7684\u5224\u65ad\u7b26<br \/>\n    EventLoop *_loop;<br \/>\n    ConnStatu _statu;<br \/>\n    Socket _socket;<br \/>\n    Channel _channel;<br \/>\n    Buffer _in_buffer;<br \/>\n    Buffer _out_buffer;<br \/>\n    Any _context; \/\/ \u4e0a\u4e0b\u6587\u63a5\u53d7<br \/>\n    using ConnectionCallback &#061; std::function&lt;void(const PtrConnection &amp;)&gt;;<br \/>\n    using MessageCallback &#061; std::function&lt;void(const PtrConnection &amp;, Buffer *)&gt;;<br \/>\n    using ClosedCallback &#061; std::function&lt;void(const PtrConnection &amp;)&gt;;<br \/>\n    using AnyEventCallback &#061; std::function&lt;void(const PtrConnection &amp;)&gt;;<br \/>\n    ConnectionCallback _connection_callback;<br \/>\n    MessageCallback _message_callback;<br \/>\n    ClosedCallback _closed_callback;<br \/>\n    ClosedCallback _server_closed_callback;<br \/>\n    AnyEventCallback _anyevent_callback;<br \/>\n}<\/p>\n<p>class Acceptor<br \/>\n{<br \/>\nprivate:<br \/>\n    Socket _socket;<br \/>\n    EventLoop *_loop;<br \/>\n    Channel _channel;<\/p>\n<p>    using AcceptCallback &#061; std::function&lt;void(int fd)&gt;;<br \/>\n    AcceptCallback _accept_callback;<br \/>\n}<\/p>\n<p>class TcpServer<br \/>\n{<br \/>\nprivate:<br \/>\n    uint64_t _conn_id; \/\/ \u81ea\u589eid<br \/>\n    int _timeout;      \/\/ \u975e\u6d3b\u8dc3\u9650\u5236\u65f6\u95f4<br \/>\n    int _port;<br \/>\n    bool _enable_inactive_release; \/\/ \u662f\u5426\u542f\u52a8\u975e\u6d3b\u8dc3\u8fde\u63a5\u6807\u51c6<br \/>\n    EventLoop _baseloop;<br \/>\n    Acceptor _acceptor; \/\/ \u76d1\u542c\u5957\u63a5\u5b57\u7ba1\u7406\u5bf9\u8c61<br \/>\n    LoopThreadPool _pool;<br \/>\n    std::unordered_map&lt;uint64_t, PtrConnection&gt; _conns; \/\/ \u4fdd\u5b58connection\u5bf9\u8c61<br \/>\n    using Functor&#061;std::function&lt;void()&gt;;<br \/>\n    using ConnectionCallback &#061; std::function&lt;void(const PtrConnection &amp;)&gt;;<br \/>\n    using MessageCallback &#061; std::function&lt;void(const PtrConnection &amp;, Buffer *)&gt;;<br \/>\n    using ClosedCallback &#061; std::function&lt;void(const PtrConnection &amp;)&gt;;<br \/>\n    using AnyEventCallback &#061; std::function&lt;void(const PtrConnection &amp;)&gt;;<br \/>\n    ConnectionCallback _connection_callback;<br \/>\n    MessageCallback _message_callback;<br \/>\n    ClosedCallback _closed_callback;<br \/>\n    ClosedCallback _server_closed_callback;<br \/>\n    AnyEventCallback _anyevent_callback;<br \/>\n}<\/p>\n<p>\/\/httpserver \u6a21\u5757<br \/>\nclass Util<br \/>\n{<br \/>\n    static size_t Split(const std::string &amp;str, const std::string &amp;sep,     std::vector&lt;std::string&gt; *arry)<br \/>\n    static bool ReadFile(const std::string &amp;filename, std::string *str)<br \/>\n\/\/ \u5411\u6587\u4ef6\u5199\u5165\u5185\u5bb9<br \/>\n    static bool WriteFile(const std::string &amp;filename, const std::string &amp;str)<br \/>\n \/\/ URL\u7f16\u7801<br \/>\n    static std::string UrlEncode(const std::string url, bool convert_space_to_plus)<br \/>\n\/\/\u5b57\u7b26\u8f6c\u5316<br \/>\n    static char HEXTOI(char c)<br \/>\n\/\/ URL\u89e3\u7801<br \/>\n    static std::string UrlDecode(const std::string url, bool convert_space_to_plus)<br \/>\n\/\/ \u54cd\u5e94\u72b6\u6001\u7801\u83b7\u53d6<br \/>\n    static std::string StatuDesc(int statu)<br \/>\n\/\/ \u6587\u4ef6\u540e\u7f00\u540d\u83b7\u53d6mime<br \/>\n    static std::string ExMime(const std::string &amp;filename)<br \/>\n\/\/ \u5224\u65ad\u6587\u4ef6\u662f\u5426\u662f\u76ee\u5f55<br \/>\n    static bool IsDirectory(const std::string &amp;filename)<br \/>\n \/\/ \u5224\u65ad\u6587\u4ef6\u662f\u5426\u662f\u666e\u901a\u6587\u4ef6<br \/>\n    static bool IsRegular(const std::string &amp;filename)<br \/>\n\/\/ http\u8bf7\u6c42\u7684\u8d44\u6e90\u8def\u5f84\u6709\u6548\u6027\u5224\u65ad<br \/>\n    static bool ValidPath(const std::string &amp;path)<br \/>\n}<\/p>\n<p>class HttpRequest<br \/>\n{<br \/>\npublic:<br \/>\n    std::string _method;                                   \/\/ \u8bf7\u6c42\u65b9\u6cd5<br \/>\n    std::string _path;                                     \/\/ \u8d44\u6e90\u8def\u5f84<br \/>\n    std::string _version;                                  \/\/ \u534f\u8bae\u7248\u672c<br \/>\n    std::string _body;                                     \/\/ \u8bf7\u6c42\u6b63\u6587<br \/>\n    std::smatch _matchs;                                   \/\/ \u8d44\u6e90\u8def\u5f84\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u7684\u6570\u636e<br \/>\n    std::unordered_map&lt;std::string, std::string&gt; _headers; \/\/ \u5934\u90e8\u5b57\u6bb5<br \/>\n    std::unordered_map&lt;std::string, std::string&gt; _params;  \/\/ \u67e5\u8be2\u5b57\u7b26\u4e32<br \/>\n}<\/p>\n<p>class HttpResponse<br \/>\n{<br \/>\npublic:<br \/>\n    int _statu;<br \/>\n    bool _redirector_flag;<br \/>\n    std::string _body;<br \/>\n    std::string _redirect_url;<br \/>\n    std::unordered_map&lt;std::string, std::string&gt; _headers;<br \/>\n}<\/p>\n<p>class HttpContext\/\/\u5bf9\u8bf7\u6c42\u5185\u5bb9\u89e3\u6790<br \/>\n{<br \/>\nprivate:<br \/>\n    int _resp_statu;           \/\/ \u54cd\u5e94\u72b6\u6001\u7801<br \/>\n    HttpRecvStatu _recv_statu; \/\/ \u63a5\u53d7\u8bf7\u6c42\u7684\u89e3\u6790\u72b6\u6001<br \/>\n    HttpRequest _request;      \/\/ \u63a5\u53d7\u7684\u8bf7\u6c42<br \/>\n}<\/p>\n<p>class HttpServer{\/\/\u5b8c\u6210\u54cd\u5e94\u62a5\u6587 \u4ee5\u53ca\u9759\u6001\u6216\u52a8\u6001\u8d44\u6e90\u83b7\u53d6\u5904\u7406\u7684\u5b9e\u73b0<br \/>\n    private:<br \/>\n    std::string _basedir;<br \/>\n        using Handler&#061;std::function&lt;void(const HttpRequest&amp;,HttpResponse*)&gt;;<br \/>\n         using Handlers&#061; std::vector&lt;std::pair&lt;std::regex,Handler&gt;&gt;;<br \/>\n        Handlers _get_route;<br \/>\n        Handlers _post_route;<br \/>\n        Handlers _put_route;<br \/>\n        Handlers _delete_route;<br \/>\n        TcpServer _server;<br \/>\n}<\/p>\n<h3>\u4e8c.Buffer\u7c7b\u4e0e\u65f6\u95f4\u8f6e\u7c7bAny\u4efb\u610f\u7c7b\u7684\u8bbe\u8ba1<\/h3>\n<p>Buffer:Buffer\u7c7b\u7528\u4e8e\u5bf9socket\u63a5\u53d7\u7684\u5ba2\u6237\u7aef\u6d88\u606f\u8fdb\u884c\u7f13\u5b58\u3002<\/p>\n<p>TimerWheel:\u5bf9\u4e8e\u5b9a\u65f6\u4efb\u52a1\u6dfb\u52a0\u5e76\u5230\u65f6\u6267\u884c&#xff0c;\u5e76\u7528\u4e8e\u8d85\u65f6\u672a\u54cd\u5e94\u7684\u65ad\u5f00\u8fde\u63a5\u529f\u80fd\u3002<\/p>\n<p>Any:\u7528\u4e8e\u5b58\u50a8\u4efb\u610f\u5bf9\u8c61&#xff0c;\u4e3b\u8981\u7528\u4e8e\u5b58\u50a8\u8bf7\u6c42\u62a5\u6587\u3002<\/p>\n<p>TimerWheel\u8bbe\u8ba1:<\/p>\n<p>\u4e3b\u8981\u662f\u5bf9Connection\u8fde\u63a5\u8fdb\u884c\u7684\u8d85\u65f6\u7ba1\u7406&#xff0c;\u5982\u679c\u8d85\u65f6\u5373\u9500\u6bc1<\/p>\n<p>timerwheel\u5185\u90e8\u6709\u4e00\u4e2atimerid\u662flinux\u7cfb\u7edf\u63d0\u4f9b\u7684\u5b9a\u65f6\u5668&#xff0c;\u7528\u4e8e\u5b9a\u65f6\u529f\u80fd&#xff0c;\u5e76\u6709\u4e00\u4e2aChannel\u5bf9\u8c61\u7528\u4e8e\u5bf9timerwheel\u56de\u8c03\u4efb\u52a1\u7684\u76d1\u542c&#xff0c;\u6bcf\u5f53timerfd\u8fdb\u884c\u4e00\u6b65&#xff0c;\u90fd\u4f1a\u89e6\u53d1\u76d1\u542c\u7684\u53ef\u8bfb\u56de\u8c03&#xff0c;\u8fdb\u884c\u5bf9\u65f6\u95f4\u8f6e\u5185\u90e8tick\u523b\u949f\u79fb\u52a8\u3002<\/p>\n<p>Tick\u79fb\u52a8\u4e0e\u4efb\u52a1\u6267\u884c\u8bbe\u8ba1:timertask\u7531\u667a\u80fd\u6307\u9488\u7ba1\u7406&#xff0c;timerwheel\u5185\u90e8hash\u7ba1\u7406\u4efb\u52a1weakptr&#xff0c;\u800c\u65f6\u95f4\u8f6e\u53d8\u91cf\u5b58\u50a8\u4efb\u52a1share_ptr&#xff0c;\u65f6\u95f4\u8f6e\u8d70\u5411\u7684\u5f53\u524d\u8282\u70b9\u4f1aclear\u5f53\u524d\u5b58\u50a8\u7684share_ptr&#xff0c;\u5982\u679c\u6307\u9488\u88ab\u5b8c\u5168\u91ca\u653e&#xff0c;\u5219\u901a\u8fc7timertask\u7684\u6790\u6784\u51fd\u6570\u6267\u884c\u8bbe\u5b9a\u7684\u4efb\u52a1&#xff0c;\u6240\u4ee5timerwheel\u53ea\u9700\u8981\u5411\u540e\u6dfb\u52a0share_ptr&#xff0c;\u5c31\u53ef\u4ee5\u63a8\u8fdftimertask\u4efb\u52a1\u6267\u884c&#xff0c;\u5b9e\u73b0\u5b9a\u65f6\u9500\u6bc1\u7684\u5237\u65b0\u673a\u5236<\/p>\n<p>Any\u7c7b\u8bbe\u8ba1:<\/p>\n<p>  class holder<br \/>\n    {<br \/>\n    public:<br \/>\n        virtual ~holder() {}<br \/>\n        virtual const std::type_info &amp;type() &#061; 0;<br \/>\n        virtual holder *clone() &#061; 0;<br \/>\n    };<br \/>\n    template &lt;class T&gt;<br \/>\n    class placeholder : public holder<br \/>\n    {<br \/>\n    public:<br \/>\n        placeholder(const T &amp;val) : _val(val) {}<br \/>\n        virtual const std::type_info &amp;type()<br \/>\n        {<br \/>\n            return typeid(T);<br \/>\n        }<br \/>\n        virtual holder *clone()<br \/>\n        {<br \/>\n            return new placeholder(_val);<br \/>\n        }<\/p>\n<p>    public:<br \/>\n        T _val;<br \/>\n    };<br \/>\n    holder *_content; <\/p>\n<p>\u901a\u8fc7c&#043;&#043;\u7ee7\u627f\u673a\u5236&#xff0c;\u7528\u7236\u7c7b\u6307\u9488\u6307\u5411\u5b50\u7c7b\u5bf9\u8c61&#xff0c;\u5b50\u7c7b\u5b58\u50a8\u5bf9\u5e94\u5b58\u50a8\u7684\u6570\u636e&#xff0c;\u901a\u8fc7\u6a21\u677f\u5b9e\u73b0\u5b58\u50a8\u4efb\u610f\u7c7b\u578b\u7684\u7c7b<\/p>\n<h3>\u4e09.Channel Poller\u4e0eEventloop\u7684\u5173\u8054<\/h3>\n<p>\u00a0 \u00a0 \u00a0 \u00a0\u00a0<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"362\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260115170525-69691e55be14a.png\" width=\"1177\" \/><\/p>\n<p>\u63a5\u53e3\u4e0e\u6210\u5458\u53d8\u91cf<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1022\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260115170525-69691e55ee441.png\" width=\"1259\" \/><\/p>\n<p>Channel\u7528\u4e8e\u5bf9\u4e8b\u4ef6\u76d1\u63a7\u7684\u7ba1\u7406&#xff0c;\u76d1\u63a7\u5b9e\u73b0\u7531Poller\u63d0\u4f9b&#xff0c;EventLoop\u5bf9\u5e94\u4e00\u4e2a\u5355\u7ebf\u7a0b\u4efb\u52a1\u5904\u7406&#xff0c;Start()\u5f00\u59cb\u4efb\u52a1\u7684\u5904\u7406&#xff0c;\u540c\u65f6\u4fdd\u8bc1\u4efb\u52a1\u6267\u884c\u5728\u5355\u7ebf\u7a0b\u5185\u3002<\/p>\n<p>Channel bind\u5bf9\u5e94\u7684read write error\u7b49\u4e8b\u4ef6\u56de\u8c03\u7684\u5bf9\u5e94\u51fd\u6570\u8c03\u7528&#xff0c;epoll_wait\u4e8b\u4ef6\u7531start\u8fdb\u884c\u5904\u7406&#xff0c;\u5ef6\u8fdf\u6267\u884c\u548c\u8de8\u7ebf\u7a0b\u4efb\u52a1 \u653e\u5165\u4efb\u52a1\u961f\u5217\u7b49\u5f85\u6267\u884c&#xff0c;\u5e76\u7531event_fd\u7528\u4e8e\u5524\u9192epoll_wait\u907f\u514d\u963b\u585e&#xff0c;\u9632\u6b62\u961f\u5217\u4efb\u52a1\u65e0\u6cd5\u6267\u884c<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"292\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260115170526-69691e5671389.png\" width=\"770\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"829\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260115170526-69691e568b647.png\" width=\"1194\" \/><\/p>\n<h3>\u4e09.Loop\u7ebf\u7a0b\u521b\u5efa\u4e0e\u7ebf\u7a0b\u7ba1\u7406<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"462\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260115170526-69691e56ec52d.png\" width=\"1048\" \/><\/p>\n<p>loop\u7ebf\u7a0b\u521b\u5efa\u540e\u76f4\u63a5\u5728\u521b\u5efa\u7ebf\u7a0b\u5185\u5faa\u73af\u6267\u884cStart\u5176\u5bf9\u5e94\u7684EventLoop<\/p>\n<p>private:<br \/>\n    void ThreadEntry()<br \/>\n    {<br \/>\n        EventLoop loop;<br \/>\n        {<br \/>\n            std::unique_lock&lt;std::mutex&gt; lock(_mutex);<br \/>\n            _loop &#061; &amp;loop; \/\/ loop\u6709\u503c\u4e86\u76f4\u63a5\u5524\u9192\u963b\u585e<br \/>\n            _cond.notify_all();<br \/>\n        }<br \/>\n        loop.Start();<br \/>\n    } \/\/ \u5b9e\u4f8b\u5316eventloop\u5bf9\u8c61&#xff0c;\u5e76\u8fd0\u884ceventloop\u6a21\u5757 <\/p>\n<h3>\u56db.Connection Acceptor TcpServer\u7c7b<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"961\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260115170527-69691e5731a5b.png\" width=\"898\" \/><\/p>\n<p>\u6bcf\u4e00\u4e2a\u8fde\u63a5\u90fd\u4f1a\u88ab\u5206\u914d\u4e00\u4e2a\u7ebf\u7a0bEventLoop,\u670d\u52a1\u5668\u53ef\u4ee5\u81ea\u5b9a\u4e49\u7ebf\u7a0b\u6570\u91cf&#xff0c;\u7ebf\u7a0b\u8f6e\u8be2\u5206\u914d\u4fdd\u8bc1\u7ebf\u7a0b\u5747\u8861&#xff0c;Connection\u4f7f\u7528share_ptr\u7ba1\u7406&#xff0c;\u8bbe\u5b9a\u8d85\u65f6\u6e05\u7406connecion\u8d44\u6e90&#xff0c;TcpServer\u5b58\u50a8\u6240\u6709\u7684Connection\u667a\u80fd\u6307\u9488&#xff0c;\u5e76\u7531TcpServer\u542f\u52a8\u76d1\u542c\u5efa\u7acb\u4e09\u6b21\u63e1\u624b\u3002<\/p>\n<p>Acceptor\u7528\u4e8e\u83b7\u53d6\u8fde\u63a5&#xff0c;\u5728Tcpserver\u5185\u8c03\u7528\u3002<\/p>\n<h3>\u56db.httpserver\u548c\u5de5\u5177\u7c7b\u4ecb\u7ecd<\/h3>\n<p>\u5de5\u5177\u7c7b:\u5b57\u7b26\u4e32\u5206\u5272&#xff0c;\u6587\u4ef6\u8bfb\u5199&#xff0c;Url\u89e3\u7801\u4e0eUrl\u7f16\u7801&#xff0c;Http\u72b6\u6001\u7801\u63cf\u8ff0&#xff0c;MIME\u7c7b\u578b\u83b7\u53d6&#xff0c;\u6587\u4ef6\u7c7b\u578b\u5224\u65ad&#xff0c;\u8def\u5f84\u6709\u6548\u6027\u9a8c\u8bc1<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"757\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260115170527-69691e577b4e6.png\" width=\"1427\" \/><\/p>\n<p>HttpServer\u4e3b\u8981\u5b9e\u73b0\u5bf9\u8bf7\u6c42\u62a5\u6587\u7684\u5904\u7406\u4e0e\u54cd\u5e94\u62a5\u6587\u7684\u8fd4\u56de<\/p>\n<h3>\u4e94.Reactor\u6a21\u578b\u4f18\u52bf<\/h3>\n<p>\u91c7\u7528\u5355EventLoop\u5355\u7ebf\u7a0b\u67b6\u6784&#xff0c;\u901a\u8fc7\u5b9e\u73b0\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u7ec4\u4ef6\u53ef\u6784\u5efa\u9ad8\u6027\u80fd\u670d\u52a1\u5668\u3002<\/p>\n<p>\u4e3bReactor\u63a5\u6536\u65b0\u8fde\u63a5\u540e&#xff0c;\u5c06\u5176\u5206\u914d\u7ed9\u5b50Reactor\u8fdb\u884c\u901a\u4fe1\u4e8b\u4ef6\u76d1\u63a7\u3002\u5404\u5b50Reactor\u7ebf\u7a0b\u8d1f\u8d23\u76d1\u63a7\u5404\u81ea\u63cf\u8ff0\u7b26\u7684\u8bfb\u5199\u4e8b\u4ef6&#xff0c;\u6267\u884c\u6570\u636e\u8bfb\u5199\u548c\u4e1a\u52a1\u5904\u7406\u64cd\u4f5c\u3002<\/p>\n<p>\u91c7\u7528\u5355EventLoop\u5355\u7ebf\u7a0b\u67b6\u6784&#xff0c;\u901a\u8fc7\u5b9e\u73b0\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u7ec4\u4ef6\u53ef\u6784\u5efa\u9ad8\u6027\u80fd\u670d\u52a1\u5668\u3002<\/p>\n<p>\u4e3bReactor\u63a5\u6536\u65b0\u8fde\u63a5\u540e&#xff0c;\u5c06\u5176\u5206\u914d\u7ed9\u5b50Reactor\u8fdb\u884c\u901a\u4fe1\u4e8b\u4ef6\u76d1\u63a7\u3002\u5404\u5b50Reactor\u7ebf\u7a0b\u8d1f\u8d23\u76d1\u63a7\u5404\u81ea\u63cf\u8ff0\u7b26\u7684\u8bfb\u5199\u4e8b\u4ef6&#xff0c;\u6267\u884c\u6570\u636e\u8bfb\u5199\u548c\u4e1a\u52a1\u5904\u7406\u64cd\u4f5c\u3002<\/p>\n<p>\u5355\u7ebf\u7a0b\u5355\u5faa\u73af\u7684\u8bbe\u8ba1\u7406\u5ff5\u662f\u5c06\u6240\u6709\u64cd\u4f5c\u96c6\u4e2d\u5728\u4e00\u4e2a\u7ebf\u7a0b\u5185\u5904\u7406&#xff0c;\u6bcf\u4e2a\u7ebf\u7a0b\u5bf9\u5e94\u4e00\u4e2a\u72ec\u7acb\u7684\u4e8b\u4ef6\u5904\u7406\u5faa\u73af\u3002<\/p>\n<p>\u5355\u7ebf\u7a0b\u5355\u5faa\u73af\u7684\u8bbe\u8ba1\u7406\u5ff5\u662f\u5c06\u6240\u6709\u64cd\u4f5c\u96c6\u4e2d\u5728\u4e00\u4e2a\u7ebf\u7a0b\u5185\u5904\u7406&#xff0c;\u6bcf\u4e2a\u7ebf\u7a0b\u5bf9\u5e94\u4e00\u4e2a\u72ec\u7acb\u7684\u4e8b\u4ef6\u5904\u7406\u5faa\u73af\u3002<\/p>\n<p>\u6838\u5fc3&#xff1a;\u6bcf\u4e00\u4e2aEventLoop\u90fd\u6709\u5176\u5355\u72ec\u5bf9\u5e94\u7684\u4e00\u4e2a\u7ebf\u7a0b&#xff0c;\u6bcf\u4e00\u4e2a\u5efa\u7acb\u7684\u8fde\u63a5\u90fd\u6709\u4e00\u4e2aChannel\u4e8b\u4ef6\u76d1\u63a7\u6a21\u5757&#xff0c;\u6bcf\u4e00\u4e2aChannel\u5bf9\u5e94\u4e00\u4e2aEventLoop,\u4e5f\u5c31\u662f\u6bcf\u4e00\u4e2a\u8fde\u63a5\u90fd\u6709\u4e00\u4e2a\u7ebf\u7a0b\u4e3a\u5176\u670d\u52a1&#xff0c;\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u80fd\u5bf9\u5e94\u591a\u4e2aConnection\u8fde\u63a5\u3002\u4e8b\u4ef6\u89e6\u53d1\u8c03\u7528\u5bf9\u5e94\u7684\u56de\u8c03\u51fd\u6570&#xff0c;\u975e\u5f53\u524d\u7ebf\u7a0b\u6dfb\u52a0\u7684\u4efb\u52a1\u4f1a\u88ab\u653e\u5165\u4efb\u52a1\u961f\u5217&#xff0c;\u5e76\u5524\u9192\u5176Epoll_wait&#xff0c;\u5e76\u7531\u5176\u5bf9\u5e94\u7684\u7ebf\u7a0b\u6267\u884c\u3002\u4e5f\u5c31\u5b9e\u73b0\u4e86\u591a\u7ebf\u7a0b\u5bf9Connection\u7684\u5904\u7406&#xff0c;\u5b9e\u73b0\u9ad8\u5e76\u53d1\u5355EventLoop\u5355\u7ebf\u7a0b\u7684\u67b6\u6784\u3002<\/p>\n<p>\u4ee3\u7801\u4ed3\u5e93\u94fe\u63a5:\u6c5f\u79bb\u6c60\u96e8\/\u5355\u7ebf\u7a0b\u5355EventLoop\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u7ec4\u4ef6<\/p>\n<\/p>\n<h3>\u516d.\u6027\u80fd\u6d4b\u8bd5<\/h3>\n<p>\u6d4b\u8bd5\u5de5\u5177:webbench<\/p>\n<p>\u6d4b\u8bd5\u73af\u5883:2g2\u6838\u00a0\u5e26\u5bbd 3Mbps \u4e91\u670d\u52a1\u5668<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"358\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260115170527-69691e57d73df.png\" width=\"803\" \/><\/p>\n<p>1000 \u5e76\u53d1\u4e0b\u80fd\u8fbe\u5230 1,816 QPS<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00.\u57fa\u672c\u8bbe\u8ba1\u4e0e\u6a21\u5757 class Buffer<br \/>\n{<br \/>\nprivate:std::vector _buffer;uint64_t _reader_idx; \/\/ \u8bfb\u504f\u79fb\u91cfuint64_t _write_idx;  \/\/ \u5199\u504f\u79fb\u91cf<br \/>\n}class Socket<br \/>\n{<br \/>\nprivate:int _sockfd;<br \/>\n}class Channel<br \/>\n{<br \/>\nprivate:int _fd;uint32_t _events;uint32_t _revents;EventLoop *_loop;<\/p>\n","protected":false},"author":2,"featured_media":60654,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,43,78],"topic":[],"class_list":["post-60662","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-c","tag-43","tag-78"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C++\u4effmuduo\u5e93onethreadoneloop\u9ad8\u5e76\u53d1\u670d\u52a1\u5668 - \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\/60662.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++\u4effmuduo\u5e93onethreadoneloop\u9ad8\u5e76\u53d1\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u4e00.\u57fa\u672c\u8bbe\u8ba1\u4e0e\u6a21\u5757 class Buffer { private:std::vector _buffer;uint64_t _reader_idx; \/\/ \u8bfb\u504f\u79fb\u91cfuint64_t _write_idx; \/\/ \u5199\u504f\u79fb\u91cf }class Socket { private:int _sockfd; }class Channel { private:int _fd;uint32_t _events;uint32_t _revents;EventLoop *_loop;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/60662.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-01-15T17:05:28+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260115170525-69691e55be14a.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=\"5 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/60662.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/60662.html\",\"name\":\"C++\u4effmuduo\u5e93onethreadoneloop\u9ad8\u5e76\u53d1\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-01-15T17:05:28+00:00\",\"dateModified\":\"2026-01-15T17:05:28+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/60662.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/60662.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/60662.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++\u4effmuduo\u5e93onethreadoneloop\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\"}]},{\"@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":"C++\u4effmuduo\u5e93onethreadoneloop\u9ad8\u5e76\u53d1\u670d\u52a1\u5668 - \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\/60662.html","og_locale":"zh_CN","og_type":"article","og_title":"C++\u4effmuduo\u5e93onethreadoneloop\u9ad8\u5e76\u53d1\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u4e00.\u57fa\u672c\u8bbe\u8ba1\u4e0e\u6a21\u5757 class Buffer { private:std::vector _buffer;uint64_t _reader_idx; \/\/ \u8bfb\u504f\u79fb\u91cfuint64_t _write_idx; \/\/ \u5199\u504f\u79fb\u91cf }class Socket { private:int _sockfd; }class Channel { private:int _fd;uint32_t _events;uint32_t _revents;EventLoop *_loop;","og_url":"https:\/\/www.wsisp.com\/helps\/60662.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-01-15T17:05:28+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260115170525-69691e55be14a.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"5 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/60662.html","url":"https:\/\/www.wsisp.com\/helps\/60662.html","name":"C++\u4effmuduo\u5e93onethreadoneloop\u9ad8\u5e76\u53d1\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-01-15T17:05:28+00:00","dateModified":"2026-01-15T17:05:28+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/60662.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/60662.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/60662.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"C++\u4effmuduo\u5e93onethreadoneloop\u9ad8\u5e76\u53d1\u670d\u52a1\u5668"}]},{"@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\/60662","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=60662"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/60662\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/60654"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=60662"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=60662"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=60662"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=60662"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}