{"id":8148,"date":"2025-04-18T16:28:48","date_gmt":"2025-04-18T08:28:48","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/8148.html"},"modified":"2025-04-18T16:28:48","modified_gmt":"2025-04-18T08:28:48","slug":"%e3%80%90%e4%bb%bfmudou%e5%ba%93one-thread-per-loop%e5%bc%8f%e5%b9%b6%e5%8f%91%e6%9c%8d%e5%8a%a1%e5%99%a8%e5%ae%9e%e7%8e%b0%e3%80%91%e9%a1%b9%e7%9b%ae%e4%bb%8b%e7%bb%8d%e5%89%8d%e7%bd%ae%e6%8a%80","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/8148.html","title":{"rendered":"\u3010\u4effMudou\u5e93one thread per loop\u5f0f\u5e76\u53d1\u670d\u52a1\u5668\u5b9e\u73b0\u3011\u9879\u76ee\u4ecb\u7ecd+\u524d\u7f6e\u6280\u672f\u77e5\u8bc6\u70b9"},"content":{"rendered":"<\/p>\n<h4>HTTP\u534f\u8bae\u6a21\u5757\u5b9e\u73b0<\/h4>\n<ul>\n<li>1. \u9879\u76ee\u5b9e\u73b0\u7684\u76ee\u6807<\/li>\n<li>2. \u9879\u76ee\u50a8\u5907\u77e5\u8bc6<\/li>\n<li>\n<ul>\n<li>2.1 HTTP\u670d\u52a1\u5668<\/li>\n<li>2.2 Reactor\u6a21\u578b<\/li>\n<\/ul>\n<\/li>\n<li>3. \u529f\u80fd\u6a21\u5757\u5212\u5206<\/li>\n<li>\n<ul>\n<li>3.1 SERVER\u6a21\u5757<\/li>\n<li>\n<ul>\n<li>3.1.1 Buffer\u6a21\u5757<\/li>\n<li>3.1.2 Socket\u6a21\u5757<\/li>\n<li>3.1.3 Channel\u6a21\u5757<\/li>\n<li>3.1.4 Poller\u6a21\u5757<\/li>\n<li>3.1.5 EventLoop\u6a21\u5757<\/li>\n<li>3.1.6 Connection\u6a21\u5757<\/li>\n<li>3.1.7 7. Acceptor\u6a21\u5757<\/li>\n<li>3.1.8 TimerQueue\u6a21\u5757<\/li>\n<li>3.1.9 \u901a\u4fe1\u6a21\u5757\u603b\u7ed3<\/li>\n<li>3.1.10 TcpServer\u6a21\u5757<\/li>\n<\/ul>\n<\/li>\n<li>3.2 HTTP\u534f\u8bae\u6a21\u5757<\/li>\n<li>\n<ul>\n<li>3.2.1 Util\u6a21\u5757<\/li>\n<li>3.2.2 HttpRequest\u6a21\u5757<\/li>\n<li>3.2.3 HttpResponse\u6a21\u5757<\/li>\n<li>3.2.3 HttpContext\u6a21\u5757<\/li>\n<li>3.2.4 HttpServer\u6a21\u5757<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>4. \u9879\u76ee\u524d\u7f6e\u77e5\u8bc6\u6280\u672f\u70b9<\/li>\n<li>\n<ul>\n<li>4.1 C&#043;&#043;11\u4e2d\u7684bind<\/li>\n<li>4.2 \u7b80\u5355\u7684\u79d2\u7ea7\u5b9a\u65f6\u4efb\u52a1\u5b9e\u73b0<\/li>\n<li>4.3 \u6b63\u5219\u5e93\u7684\u7b80\u5355\u4f7f\u7528<\/li>\n<li>4.5 \u901a\u7528\u7c7b\u578bany\u7c7b\u578b\u7684\u5b9e\u73b0<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418082839-68020d377a872.jpg\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>\u70b9\u8d5e&#x1f44d;&#x1f44d;\u6536\u85cf&#x1f31f;&#x1f31f;\u5173\u6ce8&#x1f496;&#x1f496; <font color=\"red\">\u4f60\u7684\u652f\u6301\u662f\u5bf9\u6211\u6700\u5927\u7684\u9f13\u52b1&#xff0c;\u6211\u4eec\u4e00\u8d77\u52aa\u529b\u5427!&#x1f603;&#x1f603;<\/font><\/p>\n<h2>1. \u9879\u76ee\u5b9e\u73b0\u7684\u76ee\u6807<\/h2>\n<p>\u4effmuduo\u5e93One Thread One Loop\u5f0f\u4e3b\u4eceReactor\u6a21\u578b\u5b9e\u73b0\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u8fd9\u4e2a\u9879\u76ee\u662f\u901a\u8fc7\u54b1\u4eec\u5b9e\u73b0\u7684\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u7ec4\u4ef6&#xff0c;\u53ef\u4ee5\u7b80\u6d01\u5feb\u901f\u7684\u5b8c\u6210\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684\u670d\u52a1\u5668\u642d\u5efa\u3002\u5e76\u4e14&#xff0c;\u901a\u8fc7\u7ec4\u4ef6\u5185\u63d0\u4f9b\u7684\u4e0d\u540c\u5e94\u7528\u5c42\u534f\u8bae\u652f\u6301&#xff0c;\u4e5f\u53ef\u4ee5\u5feb\u901f\u5b8c\u6210\u4e00\u4e2a\u9ad8\u6027\u80fd\u5e94\u7528\u670d\u52a1\u5668\u7684\u642d\u5efa\u3002\u5728\u8fd9\u91cc&#xff0c;\u8981\u660e\u786e\u7684\u662f\u54b1\u4eec\u8981\u5b9e\u73b0\u7684\u662f\u4e00\u4e2a\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u7ec4\u4ef6&#xff0c;\u56e0\u6b64\u5f53\u524d\u7684\u9879\u76ee\u4e2d\u5e76\u4e0d\u5305\u542b\u5b9e\u9645\u7684\u4e1a\u52a1\u5185\u5bb9\u3002<\/p>\n<h2>2. \u9879\u76ee\u50a8\u5907\u77e5\u8bc6<\/h2>\n<h3>2.1 HTTP\u670d\u52a1\u5668<\/h3>\n<p>HTTP&#xff08;Hyper Text Transfer Protocol&#xff09;&#xff0c;\u8d85\u6587\u672c\u4f20\u8f93\u534f\u8bae\u662f\u5e94\u7528\u5c42\u534f\u8bae&#xff0c;\u662f\u4e00\u79cd\u7b80\u5355\u7684\u8bf7\u6c42-\u54cd\u5e94\u534f\u8bae&#xff08;\u5ba2\u6237\u7aef\u6839\u636e\u81ea\u5df1\u7684\u9700\u8981\u5411\u670d\u52a1\u5668\u53d1\u9001\u8bf7\u6c42&#xff0c;\u670d\u52a1\u5668\u9488\u5bf9\u8bf7\u6c42\u63d0\u4f9b\u670d\u52a1&#xff0c;\u5b8c\u6bd5\u540e\u901a\u4fe1\u7ed3\u675f&#xff09;\u3002<\/p>\n<p>\u534f\u8bae\u7ec6\u8282\u5728linux\u7f51\u7edc\u90e8\u5206\u6709\u8be6\u7ec6\u4ecb\u7ecd&#xff0c;\u8fd9\u91cc\u4e0d\u5728\u8d58\u8ff0\u3002\u4f46\u662f\u9700\u8981\u6ce8\u610f\u7684\u662fHTTP\u534f\u8bae\u662f\u4e00\u4e2a\u8fd0\u884c\u5728TCP\u534f\u8bae\u4e4b\u4e0a\u7684\u5e94\u7528\u5c42\u534f\u8bae&#xff0c;\u8fd9\u4e00\u70b9\u672c\u8d28\u4e0a\u662f\u544a\u8bc9\u6211\u4eec&#xff0c;HTTP\u670d\u52a1\u5668\u5176\u5b9e\u5c31\u662f\u4e2aTCP\u670d\u52a1\u5668&#xff0c;\u53ea\u4e0d\u8fc7\u5728\u5e94\u7528\u5c42\u57fa\u4e8eHTTP\u534f\u8bae\u683c\u5f0f\u8fdb\u2f8f\u6570\u636e\u7684\u7ec4\u7ec7\u548c\u89e3\u6790\u6765\u660e\u786e\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42\u5e76\u5b8c\u6210\u4e1a\u52a1\u5904\u7406\u3002<\/p>\n<p>\u56e0\u6b64\u5b9e\u73b0HTTP\u670d\u52a1\u5668\u7b80\u5355\u7406\u89e3&#xff0c;\u53ea\u9700\u8981\u4ee5\u4e0b\u51e0\u6b65\u5373\u53ef<\/p>\n<li>\u642d\u5efa\u4e00\u4e2aTCP\u670d\u52a1\u5668&#xff0c;\u63a5\u6536\u5ba2\u6237\u7aef\u8bf7\u6c42\u3002<\/li>\n<li>\u4ee5HTTP\u534f\u8bae\u683c\u5f0f\u8fdb\u2f8f\u89e3\u6790\u8bf7\u6c42\u6570\u636e&#xff0c;\u660e\u786e\u5ba2\u6237\u7aef\u76ee\u7684\u3002<\/li>\n<li>\u660e\u786e\u5ba2\u6237\u7aef\u8bf7\u6c42\u76ee\u7684\u540e\u63d0\u4f9b\u5bf9\u5e94\u670d\u52a1\u3002<\/li>\n<li>\u5c06\u670d\u52a1\u7ed3\u679c\u4ee5HTTP\u534f\u8bae\u683c\u5f0f\u8fdb\u884c\u7ec4\u7ec7&#xff0c;\u53d1\u9001\u7ed9\u5ba2\u6237\u7aef<\/li>\n<p>\u5b9e\u73b0\u4e00\u4e2aHTTP\u670d\u52a1\u5668\u5f88\u7b80\u5355&#xff0c;\u4f46\u662f\u5b9e\u73b0\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684\u670d\u52a1\u5668\u5e76\u4e0d\u7b80\u5355&#xff0c;\u8fd9\u4e2a\u9879\u76ee\u4e2d\u5c06\u8bb2\u89e3\u57fa\u4e8eReactor\u6a21\u5f0f\u7684\u9ad8\u6027\u80fd\u670d\u52a1\u5668\u5b9e\u73b0\u3002<\/p>\n<p>\u5f53\u7136\u51c6\u786e\u6765\u8bf4&#xff0c;\u56e0\u4e3a\u6211\u4eec\u8981\u5b9e\u73b0\u7684\u670d\u52a1\u5668\u672c\u8eab\u5e76\u4e0d\u5b58\u5728\u4e1a\u52a1&#xff0c;\u54b1\u4eec\u8981\u5b9e\u73b0\u7684\u5e94\u8be5\u7b97\u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u670d\u52a1\u5668\u57fa\u7840\u5e93&#xff0c;\u662f\u4e00\u4e2a\u57fa\u7840\u7ec4\u4ef6\u3002<\/p>\n<h3>2.2 Reactor\u6a21\u578b<\/h3>\n<p>Reactor \u6a21\u5f0f&#xff0c;\u662f\u6307\u901a\u8fc7\u4e00\u4e2a\u6216\u591a\u4e2a\u8f93\u5165\u540c\u65f6\u4f20\u9012\u7ed9\u670d\u52a1\u5668\u8fdb\u884c\u8bf7\u6c42\u5904\u7406\u65f6\u7684\u4e8b\u4ef6\u9a71\u52a8\u5904\u7406\u6a21\u5f0f\u3002<\/p>\n<p>\u670d\u52a1\u7aef\u7a0b\u5e8f\u5904\u7406\u4f20\u5165\u591a\u8def\u8bf7\u6c42&#xff0c;\u5e76\u5c06\u5b83\u4eec\u540c\u6b65\u5206\u6d3e\u7ed9\u8bf7\u6c42\u5bf9\u5e94\u7684\u5904\u7406\u7ebf\u7a0b&#xff0c;Reactor \u6a21\u5f0f\u4e5f\u53ebDispatcher \u6a21\u5f0f\u3002<\/p>\n<p>\u7b80\u5355\u7406\u89e3\u5c31\u662f\u4f7f\u7528 I\/O\u591a\u8def\u590d\u7528 \u7edf\u4e00\u76d1\u542c\u4e8b\u4ef6&#xff0c;\u6536\u5230\u4e8b\u4ef6\u540e\u5206\u53d1\u7ed9\u5904\u7406\u8fdb\u7a0b\u6216\u7ebf\u7a0b&#xff0c;\u662f\u7f16\u5199\u9ad8\u6027\u80fd\u7f51\u7edc\u670d\u52a1\u5668\u7684\u5fc5\u5907\u6280\u672f\u4e4b\u4e00\u3002<\/p>\n<p>\u5206\u7c7b&#xff1a;<\/p>\n<p>\u5355Reactor\u5355\u7ebf\u7a0b&#xff1a;\u5355I\/O\u591a\u8def\u590d\u7528&#043;\u4e1a\u52a1\u5904\u7406<\/p>\n<li>\u901a\u8fc7IO\u591a\u8def\u590d\u7528\u6a21\u578b\u8fdb\u884c\u5ba2\u6237\u7aef\u8bf7\u6c42\u76d1\u63a7<\/li>\n<li>\u89e6\u53d1\u4e8b\u4ef6\u540e&#xff0c;\u8fdb\u884c\u4e8b\u4ef6\u5904\u7406<\/li>\n<ul>\n<li>\n<ul>\n<li>a. \u5982\u679c\u662f\u65b0\u5efa\u8fde\u63a5\u8bf7\u6c42&#xff0c;\u5219\u83b7\u53d6\u65b0\u5efa\u8fde\u63a5&#xff0c;\u5e76\u6dfb\u52a0\u81f3\u591a\u8def\u590d\u7528\u6a21\u578b\u8fdb\u884c\u4e8b\u4ef6\u76d1\u63a7\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<ul>\n<li>b. \u5982\u679c\u662f\u6570\u636e\u901a\u4fe1\u8bf7\u6c42&#xff0c;\u5219\u8fdb\u884c\u5bf9\u5e94\u6570\u636e\u5904\u7406&#xff08;\u63a5\u6536\u6570\u636e&#xff0c;\u5904\u7406\u6570\u636e&#xff0c;\u53d1\u9001\u54cd\u5e94&#xff09;\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u4f18\u70b9&#xff1a;\u6240\u6709\u64cd\u4f5c\u5747\u5728\u540c\u2f00\u7ebf\u7a0b\u4e2d\u5b8c\u6210&#xff0c;\u601d\u60f3\u6d41\u7a0b\u8f83\u4e3a\u7b80\u5355&#xff0c;\u4e0d\u6d89\u53ca\u8fdb\u7a0b\/\u7ebf\u7a0b\u95f4\u901a\u4fe1\u53ca\u8d44\u6e90\u4e89\u62a2\u95ee\u9898\u3002<\/p>\n<p>\u7f3a\u70b9&#xff1a;\u65e0\u6cd5\u6709\u6548\u5229\u7528CPU\u591a\u6838\u8d44\u6e90&#xff0c;\u5f88\u5bb9\u6613\u8fbe\u5230\u6027\u80fd\u74f6\u9888\u3002 \u9002\u7528\u573a\u666f&#xff1a;\u9002\u7528\u4e8e\u5ba2\u6237\u7aef\u6570\u91cf\u8f83\u5c11&#xff0c;\u4e14\u5904\u7406\u901f\u5ea6\u8f83\u4e3a\u5feb\u901f\u7684\u573a\u666f\u3002&#xff08;\u5904\u7406\u8f83\u6162\u6216\u6d3b\u8dc3\u8fde\u63a5\u8f83\u591a&#xff0c;\u4f1a\u5bfc\u81f4\u4e32\u884c\u5904\u7406\u7684\u60c5\u51b5\u4e0b&#xff0c;\u540e\u5904\u7406\u7684\u8fde\u63a5\u957f\u65f6\u95f4\u65e0\u6cd5\u5f97\u5230\u54cd\u5e94&#xff09;\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418082839-68020d378aaaa.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>\u5355Reactor\u591a\u7ebf\u7a0b&#xff1a;\u5355I\/O\u591a\u8def\u590d\u7528&#043;\u7ebf\u7a0b\u6c60&#xff08;\u4e1a\u52a1\u5904\u7406&#xff09;<\/p>\n<li>Reactor\u7ebf\u7a0b\u901a\u8fc7I\/O\u591a\u8def\u590d\u7528\u6a21\u578b\u8fdb\u884c\u5ba2\u6237\u7aef\u8bf7\u6c42\u76d1\u63a7<\/li>\n<li>\u89e6\u53d1\u4e8b\u4ef6\u540e&#xff0c;\u8fdb\u884c\u4e8b\u4ef6\u5904\u7406<\/li>\n<ul>\n<li>\n<ul>\n<li>a. \u5982\u679c\u662f\u65b0\u5efa\u8fde\u63a5\u8bf7\u6c42&#xff0c;\u5219\u83b7\u53d6\u65b0\u5efa\u8fde\u63a5&#xff0c;\u5e76\u6dfb\u52a0\u81f3\u591a\u8def\u590d\u7528\u6a21\u578b\u8fdb\u884c\u4e8b\u4ef6\u76d1\u63a7\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<ul>\n<li>b. \u5982\u679c\u662f\u6570\u636e\u901a\u4fe1\u8bf7\u6c42&#xff0c;\u5219\u63a5\u6536\u6570\u636e\u540e\u5206\u53d1\u7ed9Worker\u7ebf\u7a0b\u6c60\u8fdb\u884c\u4e1a\u52a1\u5904\u7406\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<ul>\n<li>c. \u5de5\u4f5c\u7ebf\u7a0b\u5904\u7406\u5b8c\u6bd5\u540e&#xff0c;\u5c06\u54cd\u5e94\u4ea4\u7ed9Reactor\u7ebf\u7a0b\u8fdb\u884c\u6570\u636e\u54cd\u5e94<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u4f18\u70b9&#xff1a;\u5145\u5206\u5229\u7528CPU\u591a\u6838\u8d44\u6e90<\/p>\n<p>\u7f3a\u70b9&#xff1a;\u591a\u7ebf\u7a0b\u95f4\u7684\u6570\u636e\u5171\u4eab\u8bbf\u95ee\u63a7\u5236\u8f83\u4e3a\u590d\u6742&#xff0c;\u5355\u4e2aReactor \u627f\u62c5\u6240\u6709\u4e8b\u4ef6\u7684\u76d1\u548c\u54cd\u5e94&#xff0c;\u5728\u5355\u7ebf\u7a0b\u4e2d\u8fd0\u884c&#xff0c;\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u5bb9\u6613\u6210\u4e3a\u6027\u80fd\u74f6\u9888\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418082840-68020d380a589.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>\u591aReactor\u591a\u7ebf\u7a0b&#xff1a;\u591aI\/O\u591a\u8def\u590d\u7528&#043;\u7ebf\u7a0b\u6c60&#xff08;\u4e1a\u52a1\u5904\u7406&#xff09;<\/p>\n<li>\u5728\u4e3bReactor\u4e2d\u5904\u7406\u65b0\u8fde\u63a5\u8bf7\u6c42\u4e8b\u4ef6&#xff0c;\u6709\u65b0\u8fde\u63a5\u5230\u6765\u5219\u5206\u53d1\u5230\u5b50Reactor\u4e2d\u76d1\u63a7<\/li>\n<li>\u5728\u5b50Reactor\u4e2d\u8fdb\u884c\u5ba2\u6237\u7aef\u901a\u4fe1\u76d1\u63a7&#xff0c;\u6709\u4e8b\u4ef6\u89e6\u53d1&#xff0c;\u5219\u63a5\u6536\u6570\u636e\u5206\u53d1\u7ed9Worker\u7ebf\u7a0b\u6c60<\/li>\n<li>Worker\u7ebf\u7a0b\u6c60\u5206\u914d\u72ec\u7acb\u7684\u7ebf\u7a0b\u8fdb\u884c\u5177\u4f53\u7684\u4e1a\u52a1\u5904\u7406<\/li>\n<ul>\n<li>\n<ul>\n<li>a. \u5de5\u4f5c\u7ebf\u7a0b\u5904\u7406\u5b8c\u6bd5\u540e&#xff0c;\u5c06\u54cd\u5e94\u4ea4\u7ed9\u5b50Reactor\u7ebf\u7a0b\u8fdb\u884c\u6570\u636e\u54cd\u5e94<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u4f18\u70b9&#xff1a;\u5145\u5206\u5229\u7528CPU\u591a\u6838\u8d44\u6e90&#xff0c;\u4e3b\u4eceReactor\u5404\u53f8\u5176\u804c<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418082840-68020d38a156f.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>\u54b1\u4eec\u8981\u5b9e\u73b0\u7684\u662f\u4e3b\u4eceReactor\u6a21\u578b\u670d\u52a1\u5668&#xff0c;\u4e5f\u5c31\u662f\u4e3bReactor\u7ebf\u7a0b\u4ec5\u4ec5\u76d1\u63a7\u76d1\u542c\u63cf\u8ff0\u7b26&#xff0c;\u83b7\u53d6\u65b0\u5efa\u8fde\u63a5&#xff0c;\u4fdd\u8bc1\u83b7\u53d6\u65b0\u8fde\u63a5\u7684\u9ad8\u6548\u6027&#xff0c;\u63d0\u9ad8\u670d\u52a1\u5668\u7684\u5e76\u53d1\u6027\u80fd\u3002<\/p>\n<p>\u4e3bReactor\u83b7\u53d6\u5230\u65b0\u8fde\u63a5\u540e\u5206\u53d1\u7ed9\u5b50Reactor\u8fdb\u884c\u901a\u4fe1\u4e8b\u4ef6\u76d1\u63a7\u3002\u800c\u5b50Reactor\u7ebf\u7a0b\u76d1\u63a7\u5404\u81ea\u7684\u63cf\u8ff0\u7b26\u7684\u8bfb\u5199\u4e8b\u4ef6\u8fdb\u884c\u6570\u636e\u8bfb\u5199\u4ee5\u53ca\u4e1a\u52a1\u5904\u7406\u3002<\/p>\n<p>One Thread One Loop\u7684\u601d\u60f3\u5c31\u662f\u628a\u6240\u6709\u7684\u64cd\u4f5c\u90fd\u653e\u5230\u4e00\u4e2a\u7ebf\u7a0b\u4e2d\u8fdb\u884c&#xff0c;\u4e00\u4e2a\u7ebf\u7a0b\u5bf9\u5e94\u4e00\u4e2a\u4e8b\u4ef6\u5904\u7406\u7684\u5faa\u73af\u3002<\/p>\n<p>\u5f53\u524d\u5b9e\u73b0\u4e2d&#xff0c;\u56e0\u4e3a\u5e76\u4e0d\u786e\u5b9a\u7ec4\u4ef6\u4f7f\u7528\u8005\u7684\u4f7f\u7528\u610f\u5411&#xff0c;\u56e0\u6b64\u5e76\u4e0d\u63d0\u4f9b\u4e1a\u52a1\u5c42\u5de5\u4f5c\u7ebf\u7a0b\u6c60\u7684\u5b9e\u73b0&#xff0c;\u53ea\u5b9e\u73b0\u4e3b\u4eceReactor&#xff0c;\u800cWorker\u5de5\u4f5c\u7ebf\u7a0b\u6c60&#xff0c;\u53ef\u7531\u7ec4\u4ef6\u5e93\u7684\u4f7f\u7528\u8005\u7684\u9700\u8981\u81ea\u884c\u51b3\u5b9a\u662f\u5426\u4f7f\u7528\u548c\u5b9e\u73b0\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418082841-68020d3982e3b.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h2>3. \u529f\u80fd\u6a21\u5757\u5212\u5206<\/h2>\n<p>\u57fa\u4e8e\u4ee5\u4e0a\u7684\u7406\u89e3&#xff0c;\u6211\u4eec\u8981\u5b9e\u73b0\u7684\u662f\u4e00\u4e2a\u5e26\u6709\u534f\u8bae\u652f\u6301\u7684Reactor\u6a21\u578b\u9ad8\u6027\u80fd\u670d\u52a1\u5668&#xff0c;\u56e0\u6b64\u5c06\u6574\u4e2a\u9879\u76ee\u7684\u5b9e\u73b0\u5212\u5206\u4e3a\u4e24\u4e2a\u5927\u7684\u6a21\u5757&#xff1a;<\/p>\n<ul>\n<li>SERVER\u6a21\u5757&#xff1a;\u5b9e\u73b0Reactor\u6a21\u578b\u7684TCP\u670d\u52a1\u5668&#xff1b;<\/li>\n<li>\u534f\u8bae\u6a21\u5757&#xff1a;\u5bf9\u5f53\u524d\u7684Reactor\u6a21\u578b\u670d\u52a1\u5668\u63d0\u4f9b\u5e94\u7528\u5c42\u534f\u8bae\u652f\u6301<\/li>\n<\/ul>\n<h3>3.1 SERVER\u6a21\u5757<\/h3>\n<p>SERVER\u6a21\u5757\u5c31\u662f\u5bf9\u6240\u6709\u7684\u8fde\u63a5\u4ee5\u53ca\u7ebf\u7a0b\u8fdb\u884c\u7ba1\u7406&#xff0c;\u8ba9\u5b83\u4eec\u5404\u53f8\u5176\u804c&#xff0c;\u5728\u5408\u9002\u7684\u65f6\u5019\u505a\u5408\u9002\u7684\u4e8b&#xff0c;\u6700\u7ec8 \u5b8c\u6210\u9ad8\u6027\u80fd\u670d\u52a1\u5668\u7ec4\u4ef6\u7684\u5b9e\u73b0\u3002<\/p>\n<p>\u800c\u5177\u4f53\u7684\u7ba1\u7406\u4e5f\u5206\u4e3a\u4e09\u4e2a\u65b9\u9762&#xff1a;<\/p>\n<ul>\n<li>\u76d1\u542c\u8fde\u63a5\u7ba1\u7406&#xff1a;\u5bf9\u76d1\u542c\u8fde\u63a5\u8fdb\u884c\u7ba1\u7406\u3002<\/li>\n<li>\u901a\u4fe1\u8fde\u63a5\u7ba1\u7406&#xff1a;\u5bf9\u901a\u4fe1\u8fde\u63a5\u8fdb\u884c\u7ba1\u7406\u3002<\/li>\n<li>\u8d85\u65f6\u8fde\u63a5\u7ba1\u7406&#xff1a;\u5bf9\u8d85\u65f6\u8fde\u63a5\u8fdb\u884c\u7ba1\u7406\u3002<\/li>\n<\/ul>\n<p>\u57fa\u4e8e\u4ee5\u4e0a\u7684\u7ba1\u7406\u601d\u60f3&#xff0c;\u5c06\u8fd9\u4e2a\u6a21\u5757\u8fdb\u884c\u7ec6\u81f4\u7684\u5212\u5206\u53c8\u53ef\u4ee5\u5212\u5206\u4e3a\u591a\u4e2a\u5b50\u6a21<\/p>\n<h4>3.1.1 Buffer\u6a21\u5757<\/h4>\n<p>Buffer\u6a21\u5757\u662f\u4e00\u4e2a\u7f13\u51b2\u533a\u6a21\u5757&#xff0c;\u7528\u4e8e\u5b9e\u73b0\u901a\u4fe1\u4e2d\u7528\u6237\u6001\u7684\u63a5\u6536\u7f13\u51b2\u533a\u548c\u53d1\u9001\u7f13\u51b2\u533a\u529f\u80fd\u3002 <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418082842-68020d3a2ab48.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h4>3.1.2 Socket\u6a21\u5757<\/h4>\n<p>Socket\u6a21\u5757\u662f\u5bf9\u5957\u63a5\u5b57\u64cd\u4f5c\u5c01\u88c5\u7684\u4e00\u4e2a\u6a21\u5757&#xff0c;\u4e3b\u8981\u5b9e\u73b0\u7684socket\u7684\u5404\u9879\u64cd\u4f5c\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418082842-68020d3a60fdd.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h4>3.1.3 Channel\u6a21\u5757<\/h4>\n<p>Channel\u6a21\u5757\u662f\u5bf9\u4e00\u4e2a\u63cf\u8ff0\u7b26\u9700\u8981\u8fdb\u884c\u7684IO\u4e8b\u4ef6\u7ba1\u7406\u7684\u6a21\u5757&#xff0c;\u5b9e\u73b0\u5bf9\u63cf\u8ff0\u7b26\u53ef\u8bfb&#xff0c;\u53ef\u5199&#xff0c;\u9519\u8bef\u2026\u4e8b\u4ef6\u7684\u7ba1\u7406\u64cd\u4f5c&#xff0c;\u4ee5\u53caPoller\u6a21\u5757\u5bf9\u63cf\u8ff0\u7b26\u8fdb\u884cIO\u4e8b\u4ef6\u76d1\u63a7\u5c31\u7eea\u540e&#xff0c;\u6839\u636e\u4e0d\u540c\u7684\u4e8b\u4ef6&#xff0c;\u56de\u8c03\u4e0d\u540c\u7684\u5904\u7406\u51fd\u6570\u529f\u80fd\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418082842-68020d3ab822d.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h4>3.1.4 Poller\u6a21\u5757<\/h4>\n<p>Poller\u6a21\u5757\u662f\u5bf9epoll\u8fdb\u884c\u5c01\u88c5\u7684\u4e00\u4e2a\u6a21\u5757&#xff0c;\u4e3b\u8981\u5b9e\u73b0epoll\u7684IO\u4e8b\u4ef6\u6dfb\u52a0&#xff0c;\u4fee\u6539&#xff0c;\u79fb\u9664&#xff0c;\u83b7\u53d6\u6d3b\u8dc3\u8fde\u63a5\u529f\u80fd\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418082843-68020d3b3647b.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h4>3.1.5 EventLoop\u6a21\u5757<\/h4>\n<p>EventLoop\u6a21\u5757\u53ef\u4ee5\u7406\u89e3\u5c31\u662f\u6211\u4eec\u4e0a\u8fb9\u6240\u8bf4\u7684Reactor\u6a21\u5757&#xff0c;\u5b83\u662f\u5bf9Poller\u6a21\u5757&#xff0c;TimerQueue\u6a21\u5757&#xff0c;\u8fdb\u884c\u6240\u6709\u63cf\u8ff0\u7b26\u7684\u4e8b\u4ef6\u76d1\u63a7\u3002<\/p>\n<p>EventLoop\u6a21\u5757\u662f\u4e00\u4e2aEventLoop\u5bf9\u8c61\u5bf9\u5e94\u4e00\u4e2a\u7ebf\u7a0b\u7684\u6a21\u5757&#xff0c;\u7ebf\u7a0b\u5185\u90e8\u7684\u76ee\u7684\u5c31\u662f\u8fd0\u884cEventLoop\u7684\u542f\u52a8\u51fd\u6570\u3002<\/p>\n<p>EventLoop\u6a21\u5757\u4e3a\u4e86\u4fdd\u8bc1\u6574\u4e2a\u670d\u52a1\u5668\u7684\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898&#xff0c;\u56e0\u6b64\u8981\u6c42\u4f7f\u7528\u8005\u5bf9\u4e8eConnection\u7684\u6240\u6709\u64cd\u4f5c\u4e00\u5b9a\u8981\u5728\u5176\u5bf9\u5e94\u7684EventLoop\u7ebf\u7a0b\u5185\u5b8c\u6210&#xff0c;\u4e0d\u80fd\u5728\u5176\u4ed6\u7ebf\u7a0b\u4e2d\u8fdb\u884c&#xff08;\u6bd4\u5982\u7ec4\u4ef6\u4f7f\u7528\u8005\u4f7f\u7528Connection\u53d1\u9001\u6570\u636e&#xff0c;\u4ee5\u53ca\u5173\u95ed\u8fde\u63a5\u8fd9\u79cd\u64cd\u4f5c&#xff09;\u3002<\/p>\n<p>EventLoop\u6a21\u5757\u4fdd\u8bc1\u81ea\u5df1\u5185\u90e8\u6240\u76d1\u63a7\u7684\u6240\u6709\u63cf\u8ff0\u7b26&#xff0c;\u90fd\u8981\u662f\u6d3b\u8dc3\u8fde\u63a5&#xff0c;\u975e\u6d3b\u8dc3\u8fde\u63a5\u5c31\u8981\u53ca\u65f6\u91ca\u653e\u907f\u514d\u8d44\u6e90\u6d6a\u8d39\u3002<\/p>\n<ul>\n<li>EventLoop\u6a21\u5757\u5185\u90e8\u5305\u542b\u6709\u4e00\u4e2aeventfd&#xff1a;eventfd\u5176\u5b9e\u5c31\u662flinux\u5185\u6838\u63d0\u4f9b\u7684\u4e00\u4e2a\u4e8b\u4ef6fd&#xff0c;\u4e13\u95e8\u7528\u4e8e\u4e8b\u4ef6\u901a\u77e5\u3002<\/li>\n<li>EventLoop\u6a21\u5757\u5185\u90e8\u5305\u542b\u6709\u4e00\u4e2aPoller\u5bf9\u8c61&#xff1a;\u7528\u4e8e\u8fdb\u884c\u63cf\u8ff0\u7b26\u7684IO\u4e8b\u4ef6\u76d1\u63a7\u3002<\/li>\n<li>EventLoop\u6a21\u5757\u5185\u90e8\u5305\u542b\u6709\u4e00\u4e2aTimerQueue\u5bf9\u8c61&#xff1a;\u7528\u4e8e\u8fdb\u884c\u5b9a\u65f6\u4efb\u52a1\u7684\u7ba1\u7406\u3002<\/li>\n<li>EventLoop\u6a21\u5757\u5185\u90e8\u5305\u542b\u6709\u4e00\u4e2aPendingTask\u961f\u5217&#xff1a;\u7ec4\u4ef6\u4f7f\u7528\u8005\u5c06\u5bf9Connection\u8fdb\u884c\u7684\u6240\u6709\u64cd\u4f5c&#xff0c;\u90fd\u52a0\u5165\u5230\u4efb\u52a1\u961f\u5217\u4e2d&#xff0c;\u7531EventLoop\u6a21\u5757\u8fdb\u884c\u7ba1\u7406&#xff0c;\u5e76\u5728EventLoop\u5bf9\u5e94\u7684\u7ebf\u7a0b\u4e2d\u8fdb\u884c\u6267\u884c\u3002<\/li>\n<li>\u6bcf\u4e00\u4e2aConnection\u5bf9\u8c61\u90fd\u4f1a\u7ed1\u5b9a\u5230\u4e00\u4e2aEventLoop\u4e0a&#xff0c;\u8fd9\u6837\u80fd\u4fdd\u8bc1\u5bf9\u8fd9\u4e2a\u8fde\u63a5\u7684\u6240\u6709\u64cd\u4f5c\u90fd\u662f\u5728\u4e00\u4e2a\u7ebf\u7a0b\u4e2d\u5b8c\u6210\u7684\u3002<\/li>\n<\/ul>\n<p>\u5177\u4f53\u64cd\u4f5c\u6d41\u7a0b&#xff1a;<\/p>\n<li>\u901a\u8fc7Poller\u6a21\u5757\u5bf9\u5f53\u524d\u6a21\u5757\u7ba1\u7406\u5185\u7684\u6240\u6709\u63cf\u8ff0\u7b26\u8fdb\u884cIO\u4e8b\u4ef6\u76d1\u63a7&#xff0c;\u6709\u63cf\u8ff0\u7b26\u4e8b\u4ef6\u5c31\u7eea\u540e&#xff0c;\u901a\u8fc7\u63cf\u8ff0\u7b26\u5bf9\u5e94\u7684Channel\u8fdb\u884c\u4e8b\u4ef6\u5904\u7406\u3002<\/li>\n<li>\u6240\u6709\u5c31\u7eea\u7684\u63cf\u8ff0\u7b26IO\u4e8b\u4ef6\u5904\u7406\u5b8c\u6bd5\u540e&#xff0c;\u5bf9\u4efb\u52a1\u961f\u5217\u4e2d\u7684\u6240\u6709\u64cd\u4f5c\u987a\u5e8f\u8fdb\u884c\u6267\u884c\u3002<\/li>\n<li>\u7531\u4e8eepoll\u7684\u4e8b\u4ef6\u76d1\u63a7&#xff0c;\u6709\u53ef\u80fd\u4f1a\u56e0\u4e3a\u6ca1\u6709\u4e8b\u4ef6\u5230\u6765\u800c\u6301\u7eed\u963b\u585e&#xff0c;\u5bfc\u81f4\u4efb\u52a1\u961f\u5217\u4e2d\u7684\u4efb\u52a1\u4e0d\u80fd\u53ca\u65f6\u5f97\u5230\u6267\u884c&#xff0c;\u56e0\u6b64\u521b\u5efa\u4e86eventfd&#xff0c;\u6dfb\u52a0\u5230Poller\u7684\u4e8b\u4ef6\u76d1\u63a7\u4e2d&#xff0c;\u7528\u4e8e\u5b9e\u73b0\u6bcf\u6b21\u5411\u4efb\u52a1\u961f\u5217\u6dfb\u52a0\u4efb\u52a1\u7684\u65f6\u5019&#xff0c;\u901a\u8fc7\u5411eventfd\u5199\u2f0a\u6570\u636e\u6765\u5524\u9192epoll\u7684\u963b\u585e\u3002<\/li>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418082843-68020d3b60f68.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h4>3.1.6 Connection\u6a21\u5757<\/h4>\n<p>Connection\u6a21\u5757\u662f\u5bf9Buffer\u6a21\u5757&#xff0c;Socket\u6a21\u5757&#xff0c;Channel\u6a21\u5757\u7684\u4e00\u4e2a\u6574\u4f53\u5c01\u88c5&#xff0c;\u5b9e\u73b0\u4e86\u5bf9\u4e00\u4e2a\u901a\u4fe1\u5957\u63a5\u5b57\u7684\u6574\u4f53\u7684\u7ba1\u7406&#xff0c;\u6bcf\u4e00\u4e2a\u8fdb\u884c\u6570\u636e\u901a\u4fe1\u7684\u5957\u63a5\u5b57&#xff08;\u4e5f\u5c31\u662faccept\u83b7\u53d6\u5230\u7684\u65b0\u8fde\u63a5&#xff09;\u90fd\u4f1a\u4f7f\u7528Connection\u8fdb\u884c\u7ba1\u7406\u3002<\/p>\n<ul>\n<li>\n<p>Connection\u6a21\u5757\u5185\u90e8\u5305\u542b\u6709\u4e09\u4e2a\u7531\u7ec4\u4ef6\u4f7f\u7528\u8005\u4f20\u5165\u7684\u56de\u8c03\u51fd\u6570&#xff1a;\u8fde\u63a5\u5efa\u7acb\u5b8c\u6210\u7684\u56de\u8c03&#xff0c;\u4efb\u610f\u4e8b\u4ef6\u7684\u56de\u8c03&#xff0c;\u65b0\u6570\u636e\u6765\u5230\u7684\u56de\u8c03&#xff0c;\u5173\u95ed\u8fde\u63a5\u7684\u56de\u8c03\u3002<\/p>\n<\/li>\n<li>\n<p>Connection\u6a21\u5757\u5185\u90e8\u5305\u542b\u6709\u4e24\u4e2a\u7ec4\u4ef6\u4f7f\u7528\u8005\u63d0\u4f9b\u7684\u63a5\u53e3&#xff1a;\u6570\u636e\u53d1\u9001\u63a5\u53e3&#xff0c;\u8fde\u63a5\u5173\u95ed\u63a5\u53e3<\/p>\n<\/li>\n<li>\n<p>Connection\u6a21\u5757\u5185\u90e8\u5305\u542b\u6709\u4e24\u4e2a\u7528\u6237\u6001\u7f13\u51b2\u533a&#xff1a;\u7528\u6237\u6001\u63a5\u6536\u7f13\u51b2\u533a&#xff0c;\u7528\u6237\u6001\u53d1\u9001\u7f13\u51b2\u533a<\/p>\n<\/li>\n<li>\n<p>Connection\u6a21\u5757\u5185\u90e8\u5305\u542b\u6709\u4e00\u4e2aSocket\u5bf9\u8c61&#xff1a;\u5b8c\u6210\u63cf\u8ff0\u7b26\u9762\u5411\u7cfb\u7edf\u7684IO\u64cd\u4f5c<\/p>\n<\/li>\n<li>\n<p>Connection\u6a21\u5757\u5185\u90e8\u5305\u542b\u6709\u4e00\u4e2aChannel\u5bf9\u8c61&#xff1a;\u5b8c\u6210\u63cf\u8ff0\u7b26IO\u4e8b\u4ef6\u5c31\u7eea\u7684\u5904\u7406<\/p>\n<\/li>\n<\/ul>\n<p>\u5177\u4f53\u5904\u7406\u6d41\u7a0b\u5982\u4e0b&#xff1a;<\/p>\n<li>\u5b9e\u73b0\u5411Channel\u63d0\u4f9b\u53ef\u8bfb&#xff0c;\u53ef\u5199&#xff0c;\u9519\u8bef\u7b49\u4e0d\u540c\u4e8b\u4ef6\u7684IO\u4e8b\u4ef6\u56de\u8c03\u51fd\u6570&#xff0c;\u7136\u540e\u5c06Channel\u548c\u5bf9\u5e94\u7684\u63cf\u8ff0\u7b26\u6dfb\u52a0\u5230Poller\u4e8b\u4ef6\u76d1\u63a7\u4e2d\u3002<\/li>\n<li>\u5f53\u63cf\u8ff0\u7b26\u5728Poller\u6a21\u5757\u4e2d\u5c31\u7eea\u4e86IO\u53ef\u8bfb\u4e8b\u4ef6&#xff0c;\u5219\u8c03\u7528\u63cf\u8ff0\u7b26\u5bf9\u5e94Channel\u4e2d\u4fdd\u5b58\u7684\u8bfb\u4e8b\u4ef6\u5904\u7406\u51fd\u6570&#xff0c;\u8fdb\u884c\u6570\u636e\u8bfb\u53d6&#xff0c;\u5c06socket\u63a5\u6536\u7f13\u51b2\u533a\u5168\u90e8\u8bfb\u53d6\u5230Connection\u7ba1\u7406\u7684\u7528\u6237\u6001\u63a5\u6536\u7f13\u51b2\u533a\u4e2d\u3002\u7136\u540e\u8c03\u7528\u7531\u7ec4\u4ef6\u4f7f\u7528\u8005\u4f20\u5165\u7684\u65b0\u6570\u636e\u5230\u6765\u56de\u8c03\u51fd\u6570\u8fdb\u884c\u5904\u7406\u3002<\/li>\n<li>\u7ec4\u4ef6\u4f7f\u8005\u8005\u8fdb\u884c\u6570\u636e\u7684\u4e1a\u52a1\u5904\u7406\u5b8c\u6bd5\u540e&#xff0c;\u901a\u8fc7Connection\u5411\u4f7f\u7528\u8005\u63d0\u4f9b\u7684\u6570\u636e\u53d1\u9001\u63a5\u2f1d&#xff0c;\u5c06\u6570\u636e\u5199\u5165Connection\u7684\u53d1\u9001\u7f13\u51b2\u533a\u4e2d\u3002<\/li>\n<li>\u542f\u52a8\u63cf\u8ff0\u7b26\u5728Poll\u6a21\u5757\u4e2d\u7684IO\u5199\u4e8b\u4ef6\u76d1\u63a7&#xff0c;\u5c31\u7eea\u540e&#xff0c;\u8c03\u7528Channel\u4e2d\u4fdd\u5b58\u7684\u5199\u4e8b\u4ef6\u5904\u7406\u51fd\u6570&#xff0c;\u5c06\u53d1\u9001\u7f13\u51b2\u533a\u4e2d\u7684\u6570\u636e\u901a\u8fc7Socket\u8fdb\u884c\u9762\u5411\u7cfb\u7edf\u7684\u5b9e\u9645\u6570\u636e\u53d1\u9001\u3002<\/li>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418082843-68020d3be6264.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h4>3.1.7 7. Acceptor\u6a21\u5757<\/h4>\n<p>Acceptor\u6a21\u5757\u662f\u5bf9Socket\u6a21\u5757&#xff0c;Channel\u6a21\u5757\u7684\u4e00\u4e2a\u6574\u4f53\u5c01\u88c5&#xff0c;\u5b9e\u73b0\u4e86\u5bf9\u4e00\u4e2a\u76d1\u542c\u5957\u63a5\u5b57\u7684\u6574\u4f53\u7684\u7ba1\u7406\u3002<\/p>\n<ul>\n<li>Acceptor\u6a21\u5757\u5185\u90e8\u5305\u542b\u6709\u4e00\u4e2aSocket\u5bf9\u8c61&#xff1a;\u5b9e\u73b0\u76d1\u542c\u5957\u63a5\u5b57\u7684\u64cd\u4f5c<\/li>\n<li>Acceptor\u6a21\u5757\u5185\u90e8\u5305\u542b\u6709\u4e00\u4e2aChannel\u5bf9\u8c61&#xff1a;\u5b9e\u73b0\u76d1\u542c\u5957\u63a5\u5b57IO\u4e8b\u4ef6\u5c31\u7eea\u7684\u5904\u7406<\/li>\n<\/ul>\n<p>\u5177\u4f53\u5904\u7406\u6d41\u7a0b\u5982\u4e0b&#xff1a;<\/p>\n<li>\u5b9e\u73b0\u5411Channel\u63d0\u4f9b\u53ef\u8bfb\u4e8b\u4ef6\u7684IO\u4e8b\u4ef6\u5904\u7406\u56de\u8c03\u51fd\u6570&#xff0c;\u51fd\u6570\u7684\u529f\u80fd\u5176\u5b9e\u4e5f\u5c31\u662f\u83b7\u53d6\u65b0\u8fde\u63a5<\/li>\n<li>\u4e3a\u65b0\u8fde\u63a5\u6784\u5efa\u4e00\u4e2aConnection\u5bf9\u8c61\u51fa\u6765<\/li>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418082844-68020d3c81e31.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h4>3.1.8 TimerQueue\u6a21\u5757<\/h4>\n<p>TimerQueue\u6a21\u5757\u662f\u5b9e\u73b0\u56fa\u5b9a\u65f6\u95f4\u5b9a\u65f6\u4efb\u52a1\u7684\u6a21\u5757&#xff0c;\u53ef\u4ee5\u7406\u89e3\u5c31\u662f\u8981\u7ed9\u5b9a\u65f6\u4efb\u52a1\u7ba1\u7406\u5668&#xff0c;\u5411\u5b9a\u65f6\u4efb\u52a1\u7ba1\u7406\u5668\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u4efb\u52a1&#xff0c;\u4efb\u52a1\u5c06\u5728\u56fa\u5b9a\u65f6\u95f4\u540e\u88ab\u6267\u884c&#xff0c;\u540c\u65f6\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5237\u65b0\u5b9a\u65f6\u4efb\u52a1\u6765\u5ef6\u8fdf\u4efb\u52a1\u7684\u6267\u884c\u3002<\/p>\n<p>\u8fd9\u4e2a\u6a21\u5757\u4e3b\u8981\u662f\u5bf9Connection\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\u7ba1\u7406&#xff0c;\u5bf9\u975e\u6d3b\u8dc3\u8fde\u63a5\u8fdb\u884c\u8d85\u65f6\u540e\u7684\u91ca\u653e\u529f\u80fd\u3002<\/p>\n<p>TimerQueue\u6a21\u5757\u5185\u90e8\u5305\u542b\u6709\u4e00\u4e2atimerfd&#xff1a;linux\u7cfb\u7edf\u63d0\u4f9b\u7684\u5b9a\u65f6\u5668\u3002 TimerQueue\u6a21\u5757\u5185\u90e8\u5305\u542b\u6709\u4e00\u4e2aChannel\u5bf9\u8c61&#xff1a;\u5b9e\u73b0\u5bf9timerfd\u7684IO\u65f6\u95f4\u5c31\u7eea\u56de\u8c03\u5904\u7406\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418082844-68020d3cbca93.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h4>3.1.9 \u901a\u4fe1\u6a21\u5757\u603b\u7ed3<\/h4>\n<p>\u901a\u8fc7Accpect\u6a21\u5757\u521b\u5efa\u4e00\u4e2a\u76d1\u542c\u5957\u63a5\u5b57listsock&#xff0c;\u5e76\u7ed9listsock\u5bf9\u5e94\u7684channel\u8bbe\u7f6e\u8bfb\u4e8b\u4ef6\u56de\u8c03&#xff0c;\u7136\u540e\u6dfb\u52a0\u5230\u4e3bEventLoop\u7ebf\u7a0b\u4e2d\u7684Poller\u6dfb\u52a0\u8bfb\u4e8b\u4ef6\u76d1\u63a7&#xff0c;\u5f53\u8bfb\u4e8b\u4ef6\u5c31\u7eea\u540e&#xff0c;\u901a\u8fc7Accpect\u6a21\u5757\u7ed9listsock\u8bbe\u7f6e\u8bfb\u56de\u8c03\u51fd\u6570\u83b7\u53d6\u65b0\u8fde\u63a5&#xff0c;\u83b7\u53d6\u65b0\u8fde\u63a5\u4e4b\u540e&#xff0c;\u7136\u540e\u5728\u8c03\u7528\u4e3bEventLoop\u7ebf\u7a0b\u7ed9\u5b83\u8bbe\u7f6e\u7684\u65b0\u8fde\u63a5\u83b7\u53d6\u4e4b\u540e\u7684\u56de\u8c03\u51fd\u6570&#xff0c;\u521b\u5efa\u4e00\u4e2aConnection\u5bf9\u8c61&#xff0c;\u5b83\u5185\u90e8\u4e5f\u6709\u5bf9\u5e94\u7684Channel\u5bf9\u8c61&#xff0c;\u521b\u5efaConnection\u5bf9\u8c61\u540e\u5b83\u6784\u9020\u51fd\u6570\u5185\u90e8\u5bf9\u5e94\u7684Channel\u5bf9\u8c61\u5df2\u7ecf\u8bbe\u7f6e\u597d\u5bf9\u5e94\u7684\u8bfb\u3001\u5199\u3001\u9519\u8bef\u3001\u6302\u65ad\u4e8b\u4ef6\u7684\u56de\u8c03&#xff0c;\u6240\u4ee5\u5c31\u4e0d\u7528\u7ba1\u3002\u7136\u540e\u8bbe\u7f6e\u7ed9\u8fd9\u4e2aConnection\u5bf9\u8c61\u5206\u914d\u7ed9\u67d0\u4e2a\u4eceEventLoop\u7ebf\u7a0b&#xff0c;\u5e76\u4e14\u7ed9\u5b83\u8bbe\u7f6e\u8c03\u7528\u8005\u8bbe\u7f6e\u7684\u8fde\u63a5\u5efa\u7acb\u597d\u540e\u7684\u56de\u8c03\u3001\u65b0\u6570\u636e\u6765\u4e86\u540e\u7684\u56de\u8c03&#xff0c;\u4efb\u610f\u4e8b\u4ef6\u7684\u56de\u8c03\u3001\u5173\u95ed\u8fde\u63a5\u7684\u56de\u8c03\u3002\u518d\u770b\u662f\u5426\u542f\u52a8\u975e\u6d3b\u8dc3\u8fde\u63a5&#xff0c;\u7136\u540e\u7ed9Connection\u542f\u52a8\u8bfb\u4e8b\u4ef6\u76d1\u63a7\u3002<\/p>\n<p>\u5982\u679c\u8be5Connection\u5bf9\u5e94\u7684fd\u8bfb\u4e8b\u4ef6\u5c31\u7eea\u540e&#xff0c;\u5c31\u53ef\u4ee5\u901a\u8fc7\u7ba1\u7406\u8be5Connection\u7684\u4eceEventLoop\u4e2d\u7684Poller\u6a21\u5757\u4e2d\u62ff\u5230&#xff0c;\u7136\u540e\u8c03\u7528\u7ed9\u5b83\u5185\u90e8Channel\u8bbe\u7f6e\u8bfb\u4e8b\u4ef6\u56de\u8c03&#xff0c;\u4eceConnection\u4e2d\u7684Socket\u6a21\u5757\u8bfb\u53d6\u6570\u636e\u5230Buffer\u6a21\u5757\u63a5\u6536\u7f13\u5b58\u533a&#xff0c;\u6709\u65b0\u6570\u636e\u5c31\u8c03\u7528\u8bbe\u7f6e\u597d\u7684\u65b0\u6570\u636e\u6765\u4e86\u4e4b\u540e\u7684\u56de\u8c03\u51fd\u6570\u3002\u5982\u679c\u4e1a\u52a1\u5904\u7406\u597d\u4e4b\u540e&#xff0c;\u628a\u8fd4\u56de\u7684\u6570\u636e\u5199\u5230Buffer\u8f93\u51fa\u7f13\u5b58\u533a&#xff0c;\u7136\u540e\u5c06\u8be5fd\u5199\u4e8b\u4ef6\u7684\u5173\u5fc3\u6dfb\u52a0\u5230\u7ba1\u7406\u5b83\u7684\u4eceEventLoop\u4e2d\u7684Poller\u4e2d&#xff0c;\u7b49\u4e0b\u6b21\u5199\u4e8b\u4ef6\u5c31\u7eea\u4e4b\u540e&#xff0c;\u5c31\u8c03\u7528\u5bf9\u5e94Channel\u5199\u4e8b\u4ef6\u56de\u8c03\u5c06Buffer\u8f93\u51fa\u7f13\u5b58\u533a\u7684\u6570\u636e\u901a\u8fc7Connection\u4e2d\u7684socket\u53d1\u9001\u7ed9\u5bf9\u65b9\u3002<\/p>\n<p>\u6b64\u65f6\u5c31\u5b8c\u6210\u4e86\u4e00\u6b21\u5f80\u8fd4\u901a\u4fe1\u7684\u8fc7\u7a0b\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418082844-68020d3ce64cc.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h4>3.1.10 TcpServer\u6a21\u5757<\/h4>\n<p>\u8fd9\u4e2a\u6a21\u5757\u662f\u4e00\u4e2a\u6574\u4f53Tcp\u670d\u52a1\u5668\u6a21\u5757\u7684\u5c01\u88c5&#xff0c;\u5185\u90e8\u5c01\u88c5\u4e86Acceptor\u6a21\u5757&#xff0c;EventLoopThreadPool\u6a21\u5757\u3002<\/p>\n<ul>\n<li>\n<p>TcpServer\u4e2d\u5305\u542b\u6709\u4e00\u4e2aEventLoop\u5bf9\u8c61&#xff1a;\u4ee5\u5907\u5728\u8d85\u8f7b\u91cf\u4f7f\u7528\u573a\u666f\u4e2d\u4e0d\u9700\u8981EventLoop\u7ebf\u7a0b\u6c60&#xff0c;\u53ea\u9700\u8981\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u5b8c\u6210\u6240\u6709\u64cd\u4f5c\u7684\u60c5\u51b5\u3002<\/p>\n<\/li>\n<li>\n<p>TcpServer\u6a21\u5757\u5185\u90e8\u5305\u542b\u6709\u4e00\u4e2aEventLoopThreadPool\u5bf9\u8c61&#xff1a;\u5176\u5b9e\u5c31\u662fEventLoop\u7ebf\u7a0b\u6c60&#xff0c;\u4e5f\u5c31\u662f\u5b50Reactor\u7ebf\u7a0b\u6c60<\/p>\n<\/li>\n<li>\n<p>TcpServer\u6a21\u5757\u5185\u90e8\u5305\u542b\u6709\u4e00\u4e2aAcceptor\u5bf9\u8c61&#xff1a;\u4e00\u4e2aTcpServer\u670d\u52a1\u5668&#xff0c;\u5fc5\u7136\u5bf9\u5e94\u6709\u2f00\u4e2a\u76d1\u542c\u5957\u63a5\u5b57&#xff0c;\u80fd\u591f\u5b8c\u6210\u83b7\u53d6\u5ba2\u2f3e\u7aef\u65b0\u8fde\u63a5&#xff0c;\u5e76\u5904\u7406\u7684\u4efb\u52a1\u3002<\/p>\n<\/li>\n<li>\n<p>TcpServer\u6a21\u5757\u5185\u90e8\u5305\u542b\u6709\u4e00\u4e2astd::shared_ptr\u7684hash\u8868&#xff1a;\u4fdd\u5b58\u4e86\u6240\u6709\u7684\u65b0\u5efa\u8fde\u63a5\u5bf9\u5e94\u7684Connection&#xff0c;\u6ce8\u610f&#xff0c;\u6240\u6709\u7684Connection\u4f7f\u7528shared_ptr\u8fdb\u884c\u7ba1\u7406&#xff0c;\u8fd9\u6837\u80fd\u591f\u4fdd\u8bc1\u5728hash\u8868\u4e2d\u5220\u9664\u4e86Connection\u4fe1\u606f\u540e&#xff0c;\u5728shared_ptr\u8ba1\u6570\u5668\u4e3a0\u7684\u60c5\u51b5\u4e0b\u5b8c\u6210\u5bf9Connection\u8d44\u6e90\u7684\u91ca\u653e\u64cd\u4f5c\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u5177\u4f53\u64cd\u4f5c\u6d41\u7a0b\u5982\u4e0b&#xff1a;<\/p>\n<li>\u5728\u5b9e\u4f8b\u5316TcpServer\u5bf9\u8c61\u8fc7\u7a0b\u4e2d&#xff0c;\u5b8c\u6210BaseLoop(\u4e3bReactor)\u7684\u8bbe\u7f6e&#xff0c;Acceptor\u5bf9\u8c61\u7684\u5b9e\u4f8b\u5316&#xff0c;\u4ee5\u53caEventLoop\u7ebf\u7a0b\u6c60\u7684\u5b9e\u4f8b\u5316&#xff0c;\u4ee5\u53castd::shared_ptr\u7684hash\u8868\u7684\u5b9e\u4f8b\u5316\u3002<\/li>\n<li>\u4e3aAcceptor\u5bf9\u8c61\u8bbe\u7f6e\u56de\u8c03\u51fd\u6570&#xff1a;\u83b7\u53d6\u5230\u65b0\u8fde\u63a5\u540e&#xff0c;\u4e3a\u65b0\u8fde\u63a5\u6784\u5efaConnection\u5bf9\u8c61&#xff0c;\u8bbe\u7f6eConnection\u7684\u5404\u9879\u56de\u8c03&#xff0c;\u5e76\u4f7f\u7528shared_ptr\u8fdb\u884c\u7ba1\u7406&#xff0c;\u5e76\u6dfb\u52a0\u5230hash\u8868\u4e2d\u8fdb\u884c\u7ba1\u7406&#xff0c;\u5e76\u4e3aConnection\u9009\u62e9\u4e00\u4e2aEventLoop\u7ebf\u7a0b&#xff0c;\u4e3aConnection\u6dfb\u52a0\u4e00\u4e2a\u5b9a\u65f6\u9500\u6bc1\u4efb\u52a1&#xff0c;\u4e3aConnection\u6dfb\u52a0\u4e8b\u4ef6\u76d1\u63a7&#xff0c;<\/li>\n<li>\u542f\u52a8BaseLoop\u8fdb\u884c\u76d1\u542c\u5957\u63a5\u5b57\u4e8b\u4ef6\u76d1\u63a7\u3002<\/li>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418082846-68020d3e3783b.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h3>3.2 HTTP\u534f\u8bae\u6a21\u5757<\/h3>\n<p>HTTP\u534f\u8bae\u6a21\u5757\u7528\u4e8e\u5bf9\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u6a21\u5757\u8fdb\u884c\u534f\u8bae\u2f40\u6301&#xff0c;\u57fa\u4e8e\u63d0\u4f9b\u7684\u534f\u8bae\u652f\u6301\u80fd\u591f\u66f4\u65b9\u4fbf\u7684\u5b8c\u6210\u6307\u5b9a\u534f\u8bae\u670d\u52a1\u5668\u7684\u642d\u5efa\u3002<\/p>\n<p>\u800cHTTP\u534f\u8bae\u652f\u6301\u6a21\u5757\u7684\u5b9e\u73b0&#xff0c;\u53ef\u4ee5\u7ec6\u5206\u4e3a\u4ee5\u4e0b\u51e0\u4e2a\u6a21\u5757<\/p>\n<h4>3.2.1 Util\u6a21\u5757<\/h4>\n<p>\u8fd9\u4e2a\u6a21\u5757\u662f\u4e00\u4e2a\u5de5\u5177\u6a21\u5757&#xff0c;\u4e3b\u8981\u63d0\u4f9bHTTP\u534f\u8bae\u6a21\u5757\u6240\u7528\u5230\u7684\u4e00\u4e9b\u5de5\u5177\u51fd\u6570&#xff0c;\u6bd4\u5982url\u7f16\u89e3\u7801&#xff0c;\u6587\u4ef6\u8bfb\u5199\u2026\u7b49\u3002<\/p>\n<h4>3.2.2 HttpRequest\u6a21\u5757<\/h4>\n<p>\u8fd9\u4e2a\u6a21\u5757\u662fHTTP\u8bf7\u6c42\u6570\u636e\u6a21\u5757&#xff0c;\u7528\u4e8e\u4fdd\u5b58HTTP\u8bf7\u6c42\u6570\u636e\u88ab\u89e3\u6790\u540e\u7684\u5404\u9879\u8bf7\u6c42\u5143\u7d20\u4fe1\u606f\u3002<\/p>\n<h4>3.2.3 HttpResponse\u6a21\u5757<\/h4>\n<p>\u8fd9\u4e2a\u6a21\u5757\u662fHTTP\u54cd\u5e94\u6570\u636e\u6a21\u5757&#xff0c;\u7528\u4e8e\u4e1a\u52a1\u5904\u7406\u540e\u8bbe\u7f6e\u5e76\u4fdd\u5b58HTTP\u54cd\u5e94\u6570\u636e\u7684\u7684\u5404\u9879\u5143\u7d20\u4fe1\u606f&#xff0c;\u6700\u7ec8\u4f1a\u88ab\u6309\u7167HTTP\u534f\u8bae\u54cd\u5e94\u683c\u5f0f\u7ec4\u7ec7\u6210\u4e3a\u54cd\u5e94\u4fe1\u606f\u53d1\u9001\u7ed9\u5ba2\u6237\u7aef\u3002<\/p>\n<h4>3.2.3 HttpContext\u6a21\u5757<\/h4>\n<p>\u8fd9\u4e2a\u6a21\u5757\u662f\u4e00\u4e2aHTTP\u8bf7\u6c42\u63a5\u6536\u7684\u4e0a\u4e0b\u6587\u6a21\u5757&#xff0c;\u4e3b\u8981\u662f\u4e3a\u4e86\u9632\u6b62\u5728\u4e00\u6b21\u63a5\u6536\u7684\u6570\u636e\u4e2d&#xff0c;\u4e0d\u662f\u4e00\u4e2a\u5b8c\u6574\u7684HTTP\u8bf7\u6c42&#xff0c;\u5219\u89e3\u6790\u8fc7\u7a0b\u5e76\u672a\u5b8c\u6210&#xff0c;\u65e0\u6cd5\u8fdb\u884c\u5b8c\u6574\u7684\u8bf7\u6c42\u5904\u7406&#xff0c;\u9700\u8981\u5728\u4e0b\u6b21\u63a5\u6536\u5230\u65b0\u6570\u636e\u540e\u7ee7\u7eed\u6839\u636e\u4e0a\u4e0b\u6587\u8fdb\u884c\u89e3\u6790&#xff0c;\u6700\u7ec8\u5f97\u5230\u2f00\u4e2aHttpRequest\u8bf7\u6c42\u4fe1\u606f\u5bf9\u8c61&#xff0c;\u56e0\u6b64\u5728\u8bf7\u6c42\u6570\u636e\u7684\u63a5\u6536\u4ee5\u53ca\u89e3\u6790\u90e8\u5206\u9700\u8981\u4e00\u4e2a\u4e0a\u4e0b\u6587\u6765\u8fdb\u884c\u63a7\u5236\u63a5\u6536\u548c\u5904\u7406\u8282\u594f\u3002<\/p>\n<h4>3.2.4 HttpServer\u6a21\u5757<\/h4>\n<p>\u8fd9\u4e2a\u6a21\u5757\u662f\u6700\u7ec8\u7ed9\u7ec4\u4ef6\u4f7f\u7528\u8005\u63d0\u4f9b\u7684HTTP\u670d\u52a1\u5668\u6a21\u5757\u4e86&#xff0c;\u7528\u4e8e\u4ee5\u7b80\u5355\u7684\u63a5\u53e3\u5b9e\u73b0HTTP\u670d\u52a1\u5668\u7684\u642d\u5efa\u3002<\/p>\n<p>HttpServer\u6a21\u5757\u5185\u90e8\u5305\u542b\u6709\u2f00\u4e2aTcpServer\u5bf9\u8c61&#xff1a;TcpServer\u5bf9\u8c61\u5b9e\u73b0\u670d\u52a1\u5668\u7684\u642d\u5efa<\/p>\n<p>HttpServer\u6a21\u5757\u5185\u90e8\u5305\u542b\u6709\u4e24\u4e2a\u63d0\u4f9b\u7ed9TcpServer\u5bf9\u8c61\u7684\u63a5\u53e3&#xff1a;\u8fde\u63a5\u5efa\u7acb\u6210\u529f\u8bbe\u7f6e\u4e0a\u4e0b\u6587\u63a5\u53e3&#xff0c;\u6570\u636e\u5904\u7406\u63a5\u53e3\u3002<\/p>\n<p>HttpServer\u6a21\u5757\u5185\u90e8\u5305\u542b\u6709\u4e00\u4e2ahash-map\u8868\u5b58\u50a8\u8bf7\u6c42\u4e0e\u5904\u7406\u51fd\u6570\u7684\u6620\u5c04\u8868&#xff1a;\u7ec4\u4ef6\u4f7f\u7528\u8005\u5411HttpServer\u8bbe\u7f6e\u54ea\u4e9b\u8bf7\u6c42\u5e94\u8be5\u4f7f\u7528\u54ea\u4e9b\u51fd\u6570\u8fdb\u884c\u5904\u7406&#xff0c;\u7b49TcpServer\u6536\u5230\u5bf9\u5e94\u7684\u8bf7\u6c42\u5c31\u4f1a\u4f7f\u7528\u5bf9\u5e94\u7684\u51fd\u6570\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<h2>4. \u9879\u76ee\u524d\u7f6e\u77e5\u8bc6\u6280\u672f\u70b9<\/h2>\n<h3>4.1 C&#043;&#043;11\u4e2d\u7684bind<\/h3>\n<p><span class=\"token function\">bind<\/span> <span class=\"token punctuation\">(<\/span>Fn<span class=\"token operator\">&amp;&amp;<\/span> fn<span class=\"token punctuation\">,<\/span> Args<span class=\"token operator\">&amp;&amp;<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span> args<span class=\"token punctuation\">)<\/span><\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u5c06bind\u63a5\u53e3\u770b\u4f5c\u662f\u4e00\u4e2a\u901a\u7528\u7684\u51fd\u6570\u9002\u914d\u5668&#xff0c;\u5b83\u63a5\u53d7\u4e00\u4e2a\u51fd\u6570\u5bf9\u8c61&#xff0c;\u4ee5\u53ca\u51fd\u6570\u7684\u5404\u9879\u53c2\u6570&#xff0c;\u7136\u540e\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\u5bf9\u8c61&#xff0c;\u4f46\u662f\u8fd9\u4e2a\u51fd\u6570\u5bf9\u8c61\u7684\u53c2\u6570\u5df2\u7ecf\u88ab\u7ed1\u5b9a\u4e3a\u8bbe\u7f6e\u7684\u53c2\u6570\u3002\u8fd0\u884c\u7684\u65f6\u5019\u76f8\u5f53\u4e8e\u603b\u662f\u8c03\u7528\u4f20\u5165\u56fa\u5b9a\u53c2\u6570\u7684\u539f\u51fd\u6570\u3002<\/p>\n<p>\u4f46\u662f\u5982\u679c\u8fdb\u884c\u7ed1\u5b9a\u7684\u65f6\u5019&#xff0c;\u7ed9\u4e0e\u7684\u53c2\u6570\u4e3a std::placeholders::_1, _2&#8230; \u5219\u76f8\u5f53\u4e8e\u4e3a\u65b0\u9002\u914d\u751f\u6210\u7684\u51fd\u6570\u5bf9\u8c61\u7684\u8c03\u7528\u9884\u7559\u4e00\u4e2a\u53c2\u6570\u8fdb\u884c\u4f20\u9012\u3002<\/p>\n<p><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;iostream&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;string&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;vector&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;functional&gt;<\/span><\/span><\/p>\n<p><span class=\"token keyword\">void<\/span> <span class=\"token function\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">const<\/span> std<span class=\"token operator\">::<\/span>string <span class=\"token operator\">&amp;<\/span>str<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">int<\/span> num<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n    std<span class=\"token operator\">::<\/span>cout <span class=\"token operator\">&lt;&lt;<\/span> str <span class=\"token operator\">&lt;&lt;<\/span> num <span class=\"token operator\">&lt;&lt;<\/span> std<span class=\"token operator\">::<\/span>endl<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token keyword\">int<\/span> <span class=\"token function\">main<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">using<\/span> Task <span class=\"token operator\">&#061;<\/span> std<span class=\"token operator\">::<\/span>function<span class=\"token operator\">&lt;<\/span><span class=\"token keyword\">void<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token operator\">&gt;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    std<span class=\"token operator\">::<\/span>vector<span class=\"token operator\">&lt;<\/span>Task<span class=\"token operator\">&gt;<\/span> arry<span class=\"token punctuation\">;<\/span><br \/>\n    arry<span class=\"token punctuation\">.<\/span><span class=\"token function\">push_back<\/span><span class=\"token punctuation\">(<\/span>std<span class=\"token operator\">::<\/span><span class=\"token function\">bind<\/span><span class=\"token punctuation\">(<\/span>print<span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;hello&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">10<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    arry<span class=\"token punctuation\">.<\/span><span class=\"token function\">push_back<\/span><span class=\"token punctuation\">(<\/span>std<span class=\"token operator\">::<\/span><span class=\"token function\">bind<\/span><span class=\"token punctuation\">(<\/span>print<span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;leihou&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">20<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    arry<span class=\"token punctuation\">.<\/span><span class=\"token function\">push_back<\/span><span class=\"token punctuation\">(<\/span>std<span class=\"token operator\">::<\/span><span class=\"token function\">bind<\/span><span class=\"token punctuation\">(<\/span>print<span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;nihao&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">30<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token keyword\">for<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">auto<\/span> <span class=\"token operator\">&amp;<\/span>f<span class=\"token operator\">:<\/span>arry<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token function\">f<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u57fa\u4e8ebind\u7684\u4f5c\u7528&#xff0c;\u5f53\u6211\u4eec\u5728\u8bbe\u8ba1\u4e00\u4e9b\u7ebf\u7a0b\u6c60&#xff0c;\u6216\u8005\u4efb\u52a1\u6c60\u7684\u65f6\u5019&#xff0c;\u5c31\u53ef\u4ee5\u5c06\u5c06\u4efb\u52a1\u6c60\u4e2d\u7684\u4efb\u52a1\u8bbe\u7f6e\u4e3a\u51fd\u6570\u7c7b\u578b&#xff0c;\u51fd\u6570\u7684\u53c2\u6570\u7531\u6dfb\u52a0\u4efb\u52a1\u8005\u76f4\u63a5\u4f7f\u7528bind\u8fdb\u884c\u9002\u914d\u7ed1\u5b9a\u8bbe\u7f6e&#xff0c;\u800c\u4efb\u52a1\u6c60\u4e2d\u7684\u4efb\u52a1\u88ab\u5904\u7406&#xff0c;\u53ea\u9700\u8981\u53d6\u51fa\u4e00\u4e2a\u4e2a\u7684\u51fd\u6570\u8fdb\u884c\u6267\u884c\u5373\u53ef\u3002<\/p>\n<p>\u8fd9\u6837\u505a\u6709\u4e2a\u597d\u5904\u5c31\u662f&#xff0c;\u8fd9\u79cd\u4efb\u52a1\u6c60\u5728\u8bbe\u8ba1\u7684\u65f6\u5019&#xff0c;\u4e0d\u7528\u8003\u8651\u90fd\u6709\u54ea\u4e9b\u4efb\u52a1\u5904\u7406\u65b9\u5f0f\u4e86&#xff0c;\u5904\u7406\u51fd\u6570\u8be5\u5982\u4f55\u8bbe\u8ba1&#xff0c;\u6709\u591a\u5c11\u4e2a\u4ec0\u4e48\u6837\u7684\u53c2\u6570&#xff0c;\u8fd9\u4e9b\u90fd\u4e0d\u7528\u8003\u8651\u4e86&#xff0c;\u964d\u4f4e\u4e86\u4ee3\u7801\u4e4b\u95f4\u7684\u8026\u5408\u5ea6\u3002<\/p>\n<h3>4.2 \u7b80\u5355\u7684\u79d2\u7ea7\u5b9a\u65f6\u4efb\u52a1\u5b9e\u73b0<\/h3>\n<p>\u5728\u5f53\u524d\u7684\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u4e2d&#xff0c;\u6211\u4eec\u4e0d\u5f97\u4e0d\u8003\u8651\u4e00\u4e2a\u95ee\u9898&#xff0c;\u90a3\u5c31\u662f\u8fde\u63a5\u7684\u8d85\u65f6\u5173\u95ed\u95ee\u9898\u3002\u6211\u4eec\u9700\u8981\u907f\u514d\u4e00\u4e2a\u8fde\u63a5\u957f\u65f6\u95f4\u4e0d\u901a\u4fe1&#xff0c;\u4f46\u662f\u4e5f\u4e0d\u5173\u95ed&#xff0c;\u7a7a\u8017\u8d44\u6e90\u7684\u60c5\u51b5\u3002<\/p>\n<p>\u8fd9\u65f6\u5019\u6211\u4eec\u5c31\u9700\u8981\u4e00\u4e2a\u5b9a\u65f6\u4efb\u52a1&#xff0c;\u5b9a\u65f6\u7684\u5c06\u8d85\u65f6\u8fc7\u671f\u7684\u8fde\u63a5\u8fdb\u884c\u91ca\u653e\u3002<\/p>\n<p>Linux\u63d0\u4f9b\u7ed9\u6211\u4eec\u7684\u5b9a\u65f6\u5668<\/p>\n<p><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;iostream&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;sys\/timerfd.h&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;stdint.h&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;unistd.h&gt;<\/span><\/span><\/p>\n<p><span class=\"token comment\">\/\/linux\u4e0b\u7684\u5b9a\u65f6\u5668<\/span><\/p>\n<p><span class=\"token comment\">\/\/\u521b\u5efa\u5b9a\u65f6\u5668<\/span><br \/>\n<span class=\"token comment\">\/\/ int timerfd_create(int clockid, int flags);<\/span><\/p>\n<p><span class=\"token comment\">\/\/clockid:<\/span><br \/>\n<span class=\"token comment\">\/\/CLOCK_REALTIM   &#8212; \u4ee5\u7cfb\u7edf\u65f6\u95f4\u4f5c\u4e3a\u8bb0\u65f6\u57fa\u51c6\u503c(\u5982\u679c\u7cfb\u7edf\u65f6\u95f4\u53d1\u751f\u6539\u53d8\u5c31\u4f1a\u51fa\u95ee\u9898)<\/span><br \/>\n<span class=\"token comment\">\/\/CLOCK_MONOTONIC &#8212; \u4ee5\u7cfb\u7edf\u542f\u52a8\u65f6\u95f4\u8fdb\u884c\u9012\u589e\u7684\u4e00\u4e2a\u57fa\u51c6\u503c(\u5b9a\u65f6\u4e0d\u4f1a\u968f\u7740\u7cfb\u7edf\u65f6\u95f4\u6539\u53d8\u800c\u6539\u53d8)<\/span><\/p>\n<p><span class=\"token comment\">\/\/flags:\u5bf9\u5b9a\u65f6\u5668\u8bfb\u53d6\u64cd\u4f5c\u662f\u963b\u585e\/\u975e\u963b\u585e 0 \u9ed8\u8ba4\u963b\u585e\u64cd\u4f5c<\/span><br \/>\n<span class=\"token comment\">\/\/linux\u4e0b\u4e00\u5207\u7686\u6587\u4ef6,\u5b9a\u65f6\u5668\u7684\u64cd\u4f5c\u8ddf\u6587\u4ef6\u64cd\u4f5c\u6ca1\u4ec0\u4e48\u533a\u522b<\/span><br \/>\n<span class=\"token comment\">\/\/\u5b9a\u65f6\u5668\u5b9a\u65f6\u539f\u7406,\u6bcf\u9694\u4e00\u6bb5\u65f6\u95f4(\u5b9a\u65f6\u5668\u7684\u8d85\u65f6\u65f6\u95f4),\u7cfb\u7edf\u5c31\u4f1a\u7ed9\u8fd9\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u5bf9\u5e94\u7684\u5b9a\u65f6\u5668\u5199\u5165\u4e00\u4e2a8\u5b57\u8282\u7684\u6570\u636e<\/span><br \/>\n<span class=\"token comment\">\/\/\u6ca1\u5230\u65f6\u95f4\u5c31\u963b\u585e,\u5230\u65f6\u95f4\u5c31\u53ef\u4ee5\u8bfb\u53d6\u6210\u529f\u4e86<\/span><\/p>\n<p><span class=\"token comment\">\/\/\u542f\u52a8\u5b9a\u65f6\u5668<\/span><br \/>\n<span class=\"token comment\">\/\/int timerfd_settime(int fd, int flags,   <\/span><br \/>\n<span class=\"token comment\">\/\/        const struct itimerspec *new_value,     <\/span><br \/>\n<span class=\"token comment\">\/\/       struct itimerspec *_Nullable old_value);<\/span><\/p>\n<p><span class=\"token comment\">\/\/fd: timerfd_create\u8fd4\u56de\u503c<\/span><br \/>\n<span class=\"token comment\">\/\/ flags: 0-\u76f8\u5bf9\u65f6\u95f4&#xff0c; 1-\u7edd\u5bf9\u65f6\u95f4&#xff1b;\u9ed8\u8ba4\u8bbe\u7f6e\u4e3a0\u5373\u53ef.<\/span><br \/>\n<span class=\"token comment\">\/\/  new&#xff1a; \u2f64\u4e8e\u8bbe\u7f6e\u5b9a\u65f6\u5668\u7684\u65b0\u8d85\u65f6\u65f6\u95f4<\/span><br \/>\n<span class=\"token comment\">\/\/  old&#xff1a; \u2f64\u4e8e\u63a5\u6536\u539f\u6765\u7684\u8d85\u65f6\u65f6\u95f4<\/span><\/p>\n<p><span class=\"token comment\">\/\/  struct timespec {<\/span><br \/>\n<span class=\"token comment\">\/\/      time_t tv_sec; \/* Seconds *\/  \u79d2<\/span><br \/>\n<span class=\"token comment\">\/\/      long tv_nsec; \/* Nanoseconds *\/ \u7eb3\u79d2<\/span><br \/>\n<span class=\"token comment\">\/\/  };<\/span><br \/>\n<span class=\"token comment\">\/\/<\/span><br \/>\n<span class=\"token comment\">\/\/  struct itimerspec {<\/span><br \/>\n<span class=\"token comment\">\/\/      struct timespec it_interval; \/* \u7b2c\u2f00\u6b21\u4e4b\u540e\u7684\u8d85\u65f6\u95f4\u9694\u65f6\u95f4 *\/<\/span><br \/>\n<span class=\"token comment\">\/\/      struct timespec it_value; \/* \u7b2c\u2f00\u6b21\u8d85\u65f6\u65f6\u95f4 *\/<\/span><br \/>\n<span class=\"token comment\">\/\/  };<\/span><\/p>\n<p><span class=\"token keyword\">int<\/span> <span class=\"token function\">main<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> timefd <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">timerfd_create<\/span><span class=\"token punctuation\">(<\/span>CLOCK_MONOTONIC<span class=\"token punctuation\">,<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>timefd <span class=\"token operator\">&lt;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        std<span class=\"token operator\">::<\/span>cout<span class=\"token operator\">&lt;&lt;<\/span><span class=\"token string\">&#034;open file fail&#034;<\/span><span class=\"token operator\">&lt;&lt;<\/span>std<span class=\"token operator\">::<\/span>endl<span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">itimerspec<\/span> item<span class=\"token punctuation\">;<\/span><br \/>\n    item<span class=\"token punctuation\">.<\/span>it_value<span class=\"token punctuation\">.<\/span>tv_sec <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    item<span class=\"token punctuation\">.<\/span>it_value<span class=\"token punctuation\">.<\/span>tv_nsec <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/\u7b2c\u4e00\u6b21\u8d85\u65f6\u65f6\u95f4<\/span><br \/>\n    item<span class=\"token punctuation\">.<\/span>it_interval<span class=\"token punctuation\">.<\/span>tv_sec <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    item<span class=\"token punctuation\">.<\/span>it_interval<span class=\"token punctuation\">.<\/span>tv_nsec <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><span class=\"token comment\">\/\/\u7b2c\u2f00\u6b21\u4e4b\u540e\u7684\u8d85\u65f6\u95f4\u9694\u65f6\u95f4<\/span><\/p>\n<p>    <span class=\"token function\">timerfd_settime<\/span><span class=\"token punctuation\">(<\/span>timefd<span class=\"token punctuation\">,<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">,<\/span><span class=\"token operator\">&amp;<\/span>item<span class=\"token punctuation\">,<\/span><span class=\"token keyword\">nullptr<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/sleep(10);<\/span><\/p>\n<p>    <span class=\"token keyword\">while<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">uint64_t<\/span> cnt <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">int<\/span> ret <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">read<\/span><span class=\"token punctuation\">(<\/span>timefd<span class=\"token punctuation\">,<\/span><span class=\"token operator\">&amp;<\/span>cnt<span class=\"token punctuation\">,<\/span><span class=\"token number\">8<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>ret <span class=\"token operator\">&lt;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token punctuation\">{<\/span><br \/>\n            std<span class=\"token operator\">::<\/span>cout<span class=\"token operator\">&lt;&lt;<\/span><span class=\"token string\">&#034;read fail&#034;<\/span><span class=\"token operator\">&lt;&lt;<\/span>std<span class=\"token operator\">::<\/span>endl<span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token keyword\">break<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><\/p>\n<p>        std<span class=\"token operator\">::<\/span>cout<span class=\"token operator\">&lt;&lt;<\/span><span class=\"token string\">&#034;\u8d85\u65f6\u4e86,\u8ddd\u79bb\u4e0a\u6b21\u8d85\u65f6\u6b21\u6570: &#034;<\/span><span class=\"token operator\">&lt;&lt;<\/span>cnt<span class=\"token operator\">&lt;&lt;<\/span>std<span class=\"token operator\">::<\/span>endl<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token function\">close<\/span><span class=\"token punctuation\">(<\/span>timefd<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u4e0a\u8fb9\u4f8b\u5b50&#xff0c;\u662f\u4e00\u4e2a\u5b9a\u65f6\u5668\u7684\u4f7f\u7528\u793a\u4f8b&#xff0c;\u662f\u6bcf\u96941s\u949f\u89e6\u53d1\u4e00\u6b21\u5b9a\u65f6\u5668\u8d85\u65f6\u3002<\/p>\n<p>\u57fa\u4e8e\u8fd9\u4e2a\u4f8b\u5b50&#xff0c;\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u6bcf\u9694n\u79d2&#xff0c;\u68c0\u6d4b\u4e00\u4e0b\u54ea\u4e9b\u8fde\u63a5\u8d85\u65f6\u4e86&#xff0c;\u7136\u540e\u5c06\u8d85\u65f6\u7684\u8fde\u63a5\u91ca\u653e\u6389\u3002<\/p>\n<p>\u65f6\u95f4\u8f6e\u601d\u60f3<\/p>\n<p>\u4e0a\u8ff0\u7684\u4f8b\u5b50&#xff0c;\u5b58\u5728\u4e00\u4e2a\u5f88\u5927\u7684\u95ee\u9898&#xff0c;\u6bcf\u6b21\u8d85\u65f6\u90fd\u8981\u5c06\u6240\u6709\u7684\u8fde\u63a5\u904d\u5386\u4e00\u904d&#xff0c;\u5982\u679c\u6709\u4e0a\u4e07\u4e2a\u8fde\u63a5&#xff0c;\u6548\u7387\u65e0\u7591\u662f\u8f83\u4e3a\u4f4e\u4e0b\u7684\u3002<\/p>\n<p>\u8fd9\u65f6\u5019\u5927\u5bb6\u5c31\u4f1a\u60f3\u5230&#xff0c;\u6211\u4eec\u53ef\u4ee5\u9488\u5bf9\u6240\u6709\u7684\u8fde\u63a5&#xff0c;\u6839\u636e\u6bcf\u4e2a\u8fde\u63a5\u6700\u8fd1\u4e00\u6b21\u901a\u4fe1\u7684\u7cfb\u7edf\u65f6\u95f4\u5efa\u7acb\u4e00\u4e2a\u5c0f\u6839\u5806&#xff0c;\u8fd9\u6837\u53ea\u9700\u8981\u6bcf\u6b21\u9488\u5bf9\u5806\u9876\u90e8\u5206\u7684\u8fde\u63a5\u9010\u4e2a\u91ca\u653e&#xff0c;\u76f4\u5230\u6ca1\u6709\u8d85\u65f6\u7684\u8fde\u63a5\u4e3a\u6b62&#xff0c;\u8fd9\u6837\u4e5f\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u5904\u7406\u7684\u6548\u7387\u3002<\/p>\n<p>\u4e0a\u8ff0\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u5b9a\u65f6\u4efb\u52a1&#xff0c;\u4f46\u662f\u8fd9\u7acb\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u53e6\u4e00\u79cd\u65b9\u6848&#xff1a;\u65f6\u95f4\u8f6e<\/p>\n<p>\u65f6\u95f4\u8f6e\u7684\u601d\u60f3\u6765\u6e90\u4e8e\u949f\u8868&#xff0c;\u5982\u679c\u6211\u4eec\u5b9a\u4e86\u4e00\u4e2a3\u70b9\u949f\u7684\u95f9\u94c3&#xff0c;\u5219\u5f53\u65f6\u9488\u8d70\u52303\u7684\u65f6\u5019&#xff0c;\u5c31\u4ee3\u8868\u65f6\u95f4\u5230\u4e86\u3002<\/p>\n<p>\u540c\u6837\u7684\u9053\u7406&#xff0c;\u5982\u679c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6570\u7ec4&#xff0c;\u5e76\u4e14\u6709\u4e00\u4e2a\u6307\u9488&#xff0c;\u6307\u5411\u6570\u7ec4\u8d77\u59cb\u4f4d\u7f6e&#xff0c;\u8fd9\u4e2a\u6307\u9488\u6bcf\u79d2\u949f\u5411\u540e\u8d70\u52a8\u4e00\u6b65&#xff0c;\u8d70\u5230\u54ea\u91cc&#xff0c;\u5219\u4ee3\u8868\u54ea\u91cc\u7684\u4efb\u52a1\u8be5\u88ab\u6267\u884c\u4e86&#xff0c;\u90a3\u4e48\u5982\u679c\u6211\u4eec\u60f3\u8981\u5b9a\u4e00\u4e2a3s\u540e\u7684\u4efb\u52a1&#xff0c;\u5219\u53ea\u9700\u8981\u5c06\u4efb\u52a1\u6dfb\u52a0\u5230tick&#043;3\u4f4d\u7f6e&#xff0c;\u5219\u6bcf\u79d2\u4e2d\u8d70\u4e00\u6b65&#xff0c;\u4e09\u79d2\u949f\u540etick\u8d70\u5230\u5bf9\u5e94\u4f4d\u7f6e&#xff0c;\u8fd9\u65f6\u5019\u6267\u884c\u5bf9\u5e94\u4f4d\u7f6e\u7684\u4efb\u52a1\u5373\u53ef\u3002<\/p>\n<p>\u4f46\u662f&#xff0c;\u540c\u4e00\u65f6\u95f4\u53ef\u80fd\u4f1a\u6709\u5927\u6279\u91cf\u7684\u5b9a\u65f6\u4efb\u52a1&#xff0c;\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u7ed9\u6570\u7ec4\u5bf9\u5e94\u4f4d\u7f6e\u4e0b\u62c9\u4e00\u4e2a\u6570\u7ec4&#xff0c;\u8fd9\u6837\u5c31\u53ef\u4ee5\u5728\u540c\u4e00\u4e2a\u65f6\u523b\u4e0a\u6dfb\u52a0\u591a\u4e2a\u5b9a\u65f6\u4efb\u52a1\u4e86\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418082846-68020d3e9da49.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>\u5f53\u7136&#xff0c;\u4e0a\u8ff0\u64cd\u4f5c\u4e5f\u6709\u4e00\u4e9b\u7f3a\u9677&#xff0c;\u6bd4\u5982\u6211\u4eec\u5982\u679c\u8981\u5b9a\u4e49\u4e00\u4e2a60s\u540e\u7684\u4efb\u52a1&#xff0c;\u5219\u9700\u8981\u5c06\u6570\u7ec4\u7684\u5143\u7d20\u4e2a\u6570\u8bbe\u7f6e\u4e3a60\u624d\u53ef\u4ee5&#xff0c;\u5982\u679c\u8bbe\u7f6e\u4e00\u5c0f\u65f6\u540e\u7684\u5b9a\u65f6\u4efb\u52a1&#xff0c;\u5219\u9700\u8981\u5b9a\u4e493600\u4e2a\u5143\u7d20\u7684\u6570\u7ec4&#xff0c;\u8fd9\u6837\u65e0\u7591\u662f\u6bd4\u8f83\u9ebb\u70e6\u7684\u3002<\/p>\n<p>\u56e0\u6b64&#xff0c;\u53ef\u4ee5\u91c7\u7528\u591a\u5c42\u7ea7\u7684\u65f6\u95f4\u8f6e&#xff0c;\u6709\u79d2\u9488\u8f6e&#xff0c;\u5206\u9488\u8f6e&#xff0c;\u65f6\u9488\u8f6e&#xff0c; 60&lt;time&lt;3600\u5219time\/60\u5c31\u662f\u5206\u9488\u8f6e\u5bf9\u5e94\u5b58\u50a8\u7684\u4f4d\u7f6e&#xff0c;\u5f53tick\/3600\u7b49\u4e8e\u5bf9\u5e94\u4f4d\u7f6e\u7684\u65f6\u5019&#xff0c;\u5c06\u5176\u4f4d\u7f6e\u7684\u4efb\u52a1\u5411\u5206\u9488&#xff0c;\u79d2\u9488\u8f6e\u8fdb\u884c\u79fb\u52a8\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418082846-68020d3ee541e.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>\u56e0\u4e3a\u5f53\u524d\u6211\u4eec\u7684\u5e94\u7528\u4e2d&#xff0c;\u5012\u662f\u4e0d\u7528\u8bbe\u8ba1\u7684\u8fd9\u4e48\u9ebb\u70e6&#xff0c;\u56e0\u4e3a\u6211\u4eec\u7684\u5b9a\u65f6\u4efb\u52a1\u901a\u5e38\u8bbe\u7f6e\u768430s\u4ee5\u5185&#xff0c;\u6240\u4ee5\u7b80\u5355\u7684\u79d2\u7ea7\u65f6\u95f4\u8f6e\u5c31\u591f\u7528\u4e86\u3002<\/p>\n<p>\u4f46\u662f&#xff0c;\u6211\u4eec\u4e5f\u5f97\u8003\u8651\u4e00\u4e2a\u95ee\u9898&#xff0c;\u5f53\u524d\u7684\u8bbe\u8ba1\u662f\u65f6\u95f4\u5230\u4e86&#xff0c;\u5219\u4e3b\u52a8\u53bb\u6267\u884c\u5b9a\u65f6\u4efb\u52a1&#xff0c;\u91ca\u653e\u8fde\u63a5&#xff0c;\u90a3\u80fd\u4e0d\u80fd\u5728\u65f6\u95f4\u5230\u4e86\u540e&#xff0c;\u81ea\u52a8\u6267\u884c\u5b9a\u65f6\u4efb\u52a1\u5462&#xff0c;\u8fd9\u65f6\u5019\u6211\u4eec\u5c31\u60f3\u5230\u4e00\u4e2a\u64cd\u4f5c\u2014\u7c7b\u7684\u6790\u6784\u51fd\u6570\u3002\u4e00\u4e2a\u7c7b\u7684\u6790\u6784\u51fd\u6570&#xff0c;\u5728\u5bf9\u8c61\u88ab\u91ca\u653e\u65f6\u4f1a\u81ea\u52a8\u88ab\u6267\u884c&#xff0c;\u90a3\u4e48\u6211\u4eec\u5982\u679c\u5c06\u4e00\u4e2a\u5b9a\u65f6\u4efb\u52a1\u4f5c\u4e3a\u4e00\u4e2a\u7c7b\u7684\u6790\u6784\u51fd\u6570\u5185\u7684\u64cd\u4f5c&#xff0c;\u5219\u8fd9\u4e2a\u5b9a\u65f6\u4efb\u52a1\u5728\u5bf9\u8c61\u88ab\u91ca\u653e\u7684\u65f6\u5019\u5c31\u4f1a\u6267\u884c\u3002<\/p>\n<p>\u4f46\u662f\u4ec5\u4ec5\u4e3a\u4e86\u8fd9\u4e2a\u76ee\u7684&#xff0c;\u800c\u8bbe\u8ba1\u4e00\u4e2a\u989d\u5916\u7684\u4efb\u52a1\u7c7b&#xff0c;\u597d\u50cf\u6709\u4e9b\u4e0d\u5212\u7b97&#xff0c;\u4f46\u662f&#xff0c;\u8fd9\u91cc\u6211\u4eec\u53c8\u8981\u8003\u8651\u53e6\u4e00\u4e2a\u95ee\u9898&#xff0c;\u90a3\u5c31\u662f\u5047\u5982\u6709\u4e00\u4e2a\u8fde\u63a5\u7acb\u6210\u529f\u4e86&#xff0c;\u6211\u4eec\u7ed9\u8fd9\u4e2a\u8fde\u63a5\u8bbe\u7f6e\u4e86\u4e00\u4e2a30s\u540e\u7684\u5b9a\u65f6\u9500\u6bc1\u4efb\u52a1&#xff0c;\u4f46\u662f\u5728\u7b2c10s\u7684\u65f6\u5019&#xff0c;\u8fd9\u4e2a\u8fde\u63a5\u8fdb\u884c\u4e86\u4e00\u6b21\u901a\u4fe1&#xff0c;\u90a3\u4e48\u6211\u4eec\u5e94\u8be5\u65f6\u5728\u7b2c30s\u7684\u65f6\u5019\u5173\u95ed&#xff0c;\u8fd8\u662f\u7b2c40s\u7684\u65f6\u5019\u5173\u95ed\u5462&#xff1f;\u65e0\u7591\u5e94\u8be5\u662f\u7b2c40s\u7684\u65f6\u5019\u3002\u4e5f\u5c31\u662f\u8bf4&#xff0c;\u8fd9\u65f6\u5019&#xff0c;\u6211\u4eec\u9700\u8981\u8ba9\u8fd9\u4e2a\u7b2c30s\u7684\u4efb\u52a1\u5931\u6548&#xff0c;\u4f46\u662f\u6211\u4eec\u8be5\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e2a\u64cd\u4f5c\u5462&#xff1f;<\/p>\n<p>\u8fd9\u91cc&#xff0c;\u6211\u4eec\u5c31\u7528\u5230\u4e86\u667a\u80fd\u6307\u9488shared_ptr&#xff0c;shared_ptr\u6709\u4e2a\u8ba1\u6570\u5668&#xff0c;\u5f53\u8ba1\u6570\u4e3a0\u7684\u65f6\u5019&#xff0c;\u624d\u4f1a\u771f\u6b63\u91ca\u653e\u4e00\u4e2a\u5bf9\u8c61&#xff0c;\u90a3\u4e48\u5982\u679c\u8fde\u63a5\u5728\u7b2c10s\u8fdb\u884c\u4e86\u4e00\u6b21\u901a\u4fe1&#xff0c;\u5219\u6211\u4eec\u7ee7\u7eed\u5411\u5b9a\u65f6\u4efb\u52a1\u4e2d&#xff0c;\u6dfb\u52a0\u4e00\u4e2a30s\u540e&#xff08;\u4e5f\u5c31\u662f\u7b2c40s&#xff09;\u7684\u4efb\u52a1\u7c7b\u5bf9\u8c61\u7684shared_ptr&#xff0c;\u5219\u8fd9\u65f6\u5019\u4e24\u4e2a\u4efb\u52a1shared_ptr\u8ba1\u6570\u4e3a2&#xff0c;\u5219\u7b2c30s\u7684\u5b9a\u65f6\u4efb\u52a1\u88ab\u91ca\u653e\u7684\u65f6\u5019&#xff0c;\u8ba1\u6570-1&#xff0c;\u53d8\u4e3a1&#xff0c;\u5e76\u4e0d\u4e3a0&#xff0c;\u5219\u5e76\u4e0d\u4f1a\u6267\u884c\u5b9e\u9645\u7684\u6790\u6784\u51fd\u6570&#xff0c;\u90a3\u4e48\u5c31\u76f8\u5f53\u4e8e\u8fd9\u4e2a\u7b2c30s\u7684\u4efb\u52a1\u5931\u6548\u4e86&#xff0c;\u53ea\u6709\u5728\u7b2c40s\u7684\u65f6\u5019&#xff0c;\u8fd9\u4e2a\u4efb\u52a1\u624d\u4f1a\u88ab\u771f\u6b63\u91ca\u653e\u3002<\/p>\n<p><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;iostream&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;stdint.h&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;functional&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;vector&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;memory&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;unordered_map&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;unistd.h&gt;<\/span><\/span><\/p>\n<p><span class=\"token keyword\">using<\/span> TaskFunc <span class=\"token operator\">&#061;<\/span> std<span class=\"token operator\">::<\/span>function<span class=\"token operator\">&lt;<\/span><span class=\"token keyword\">void<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token operator\">&gt;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">using<\/span> ReleaseFunc  <span class=\"token operator\">&#061;<\/span> std<span class=\"token operator\">::<\/span>function<span class=\"token operator\">&lt;<\/span><span class=\"token keyword\">void<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token operator\">&gt;<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">class<\/span> <span class=\"token class-name\">TimerTask<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token keyword\">private<\/span><span class=\"token operator\">:<\/span><br \/>\n    <span class=\"token keyword\">uint64_t<\/span> _id<span class=\"token punctuation\">;<\/span>   <span class=\"token comment\">\/\/ \u5b9a\u65f6\u5668\u4efb\u52a1\u5bf9\u8c61ID<\/span><br \/>\n    <span class=\"token keyword\">uint32_t<\/span> _timeout<span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/\u5b9a\u65f6\u4efb\u52a1\u7684\u8d85\u65f6\u65f6\u95f4<\/span><br \/>\n    <span class=\"token keyword\">bool<\/span> _cancel<span class=\"token punctuation\">;<\/span>      <span class=\"token comment\">\/\/ \u5b9a\u65f6\u4efb\u52a1\u662f\u5426\u53d6\u6d88,false\u4e0d\u53d6\u6d88,true\u53d6\u6d88<\/span><br \/>\n    TaskFunc _task_cb<span class=\"token punctuation\">;<\/span>      <span class=\"token comment\">\/\/ \u5b9a\u65f6\u5668\u5bf9\u8c61\u8981\u6267\u884c\u7684\u5b9a\u65f6\u4efb\u52a1<\/span><br \/>\n    ReleaseFunc _release_cb<span class=\"token punctuation\">;<\/span>   <span class=\"token comment\">\/\/\/ \u7528\u4e8e\u5220\u9664TimerWheel\u4e2d\u4fdd\u5b58\u7684\u5b9a\u65f6\u5668\u5bf9\u8c61\u4fe1\u606f<\/span><\/p>\n<p><span class=\"token keyword\">public<\/span><span class=\"token operator\">:<\/span><br \/>\n    <span class=\"token function\">TimerTask<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">uint64_t<\/span> id<span class=\"token punctuation\">,<\/span><span class=\"token keyword\">uint32_t<\/span> time<span class=\"token punctuation\">,<\/span><span class=\"token keyword\">const<\/span> TaskFunc<span class=\"token operator\">&amp;<\/span> cb<span class=\"token punctuation\">)<\/span><span class=\"token operator\">:<\/span><span class=\"token function\">_id<\/span><span class=\"token punctuation\">(<\/span>id<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><span class=\"token function\">_timeout<\/span><span class=\"token punctuation\">(<\/span>time<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><span class=\"token function\">_task_cb<\/span><span class=\"token punctuation\">(<\/span>cb<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><span class=\"token function\">_cancel<\/span><span class=\"token punctuation\">(<\/span><span class=\"token boolean\">false<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">{<\/span><span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token operator\">~<\/span><span class=\"token function\">TimerTask<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>_cancel <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token boolean\">false<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token function\">_task_cb<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n        <span class=\"token function\">_release_cb<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token keyword\">uint32_t<\/span> <span class=\"token function\">DelayTime<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> _timeout<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token keyword\">void<\/span> <span class=\"token function\">SetRelease<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">const<\/span> ReleaseFunc <span class=\"token operator\">&amp;<\/span> rb<span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        _release_cb <span class=\"token operator\">&#061;<\/span> rb<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token keyword\">void<\/span> <span class=\"token function\">Cancel<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        _cancel <span class=\"token operator\">&#061;<\/span> <span class=\"token boolean\">true<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">using<\/span> SharedPtr <span class=\"token operator\">&#061;<\/span> std<span class=\"token operator\">::<\/span>shared_ptr<span class=\"token operator\">&lt;<\/span>TimerTask<span class=\"token operator\">&gt;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">using<\/span> WeakPtr <span class=\"token operator\">&#061;<\/span> std<span class=\"token operator\">::<\/span>weak_ptr<span class=\"token operator\">&lt;<\/span>TimerTask<span class=\"token operator\">&gt;<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">class<\/span> <span class=\"token class-name\">TimeWheel<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token keyword\">private<\/span><span class=\"token operator\">:<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> _tick<span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/\u6ef4\u7b54\u6307\u9488,\u5f53\u524d\u7684\u79d2,\u8d70\u5230\u54ea\u91cc\u91ca\u653e\u54ea\u91cc,\u91ca\u653e\u54ea\u91cc,\u5c31\u76f8\u5f53\u4e8e\u6267\u884c\u54ea\u91cc\u7684\u4efb\u52a1<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> _capacity<span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/\u8868\u76d8\u6700\u5927\u6570\u91cf&#8212;\u5176\u5b9e\u5c31\u662f\u6700\u5927\u5ef6\u8fdf\u65f6\u95f4<\/span><br \/>\n    std<span class=\"token operator\">::<\/span>vector<span class=\"token operator\">&lt;<\/span>std<span class=\"token operator\">::<\/span>vector<span class=\"token operator\">&lt;<\/span>SharedPtr<span class=\"token operator\">&gt;&gt;<\/span> _wheel<span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/\u65f6\u95f4\u8f6e<\/span><br \/>\n    std<span class=\"token operator\">::<\/span>unordered_map<span class=\"token operator\">&lt;<\/span><span class=\"token keyword\">uint64_t<\/span><span class=\"token punctuation\">,<\/span>WeakPtr<span class=\"token operator\">&gt;<\/span> _timers<span class=\"token punctuation\">;<\/span><span class=\"token comment\">\/\/\u901a\u8fc7id\u627e\u5230\u4efb\u52a1\u5bf9\u8c61,\u65b9\u4fbf\u540e\u7eed\u5237\u65b0\u4efb\u52a1<\/span><br \/>\n    <span class=\"token comment\">\/\/unordered_map\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e0d\u80fd\u7528shared_ptr,\u5982\u679c\u8fd8\u7528\u7528shared_ptr\u53bb\u6307\u5411\u539f\u59cb\u5bf9\u8c61,\u90a3\u4e48\u5c31\u548c\u4e4b\u524d\u7528shared_ptr\u6307\u5411\u539f\u59cb\u5bf9\u8c61<\/span><br \/>\n    <span class=\"token comment\">\/\/\u800c\u4ea7\u751f\u7684\u5f15\u7528\u8ba1\u6570\u6839\u672c\u5c31\u4e0d\u662f\u540c\u4e00\u4e2a\u5f15\u7528\u8ba1\u6570,\u8fd9\u6837\u5982\u679cunordered_map\u7b2c\u4e8c\u4e2a\u53c2\u6570\u5f15\u7528\u8ba1\u6570\u662f1,\u800c\u5176\u4ed6\u6307\u5411\u5b83\u5f15\u7528\u8ba1\u6570\u662f2<\/span><br \/>\n    <span class=\"token comment\">\/\/\u4e07\u4e00unordered_map\u7b2c\u4e8c\u4e2a\u53c2\u6570shared_ptr\u88ab\u91ca\u653e\u4e86,\u8fd9\u4e2a\u539f\u59cb\u5bf9\u8c61\u5c31\u4f1a\u88ab\u91ca\u653e,\u8fd9\u662f\u7531\u95ee\u9898\u7684.<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/\u91ca\u653e\u65f6\u95f4\u8f6e\u4e0a\u4efb\u52a1\u5bf9\u8c61<\/span><br \/>\n    <span class=\"token keyword\">void<\/span> <span class=\"token function\">RemoveTimer<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">uint64_t<\/span> id<span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">auto<\/span> it <span class=\"token operator\">&#061;<\/span> _timers<span class=\"token punctuation\">.<\/span><span class=\"token function\">find<\/span><span class=\"token punctuation\">(<\/span>id<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>it <span class=\"token operator\">&#061;&#061;<\/span> _timers<span class=\"token punctuation\">.<\/span><span class=\"token function\">end<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token keyword\">return<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n        _timers<span class=\"token punctuation\">.<\/span><span class=\"token function\">erase<\/span><span class=\"token punctuation\">(<\/span>it<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token keyword\">public<\/span><span class=\"token operator\">:<\/span><br \/>\n    <span class=\"token function\">TimeWheel<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token operator\">:<\/span><span class=\"token function\">_tick<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><span class=\"token function\">_capacity<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">60<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><span class=\"token function\">_wheel<\/span><span class=\"token punctuation\">(<\/span>_capacity<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">{<\/span><span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token operator\">~<\/span><span class=\"token function\">TimeWheel<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">{<\/span><span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/\u8bbe\u7f6e\u5b9a\u65f6\u4efb\u52a1<\/span><br \/>\n    <span class=\"token keyword\">void<\/span> <span class=\"token function\">TimerAdd<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">uint64_t<\/span> id<span class=\"token punctuation\">,<\/span><span class=\"token keyword\">uint32_t<\/span> delay<span class=\"token punctuation\">,<\/span><span class=\"token keyword\">const<\/span> TaskFunc<span class=\"token operator\">&amp;<\/span> cb<span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        SharedPtr <span class=\"token function\">spt<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">new<\/span> <span class=\"token function\">TimerTask<\/span><span class=\"token punctuation\">(<\/span>id<span class=\"token punctuation\">,<\/span>delay<span class=\"token punctuation\">,<\/span>cb<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        spt<span class=\"token operator\">-&gt;<\/span><span class=\"token function\">SetRelease<\/span><span class=\"token punctuation\">(<\/span>std<span class=\"token operator\">::<\/span><span class=\"token function\">bind<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>TimeWheel<span class=\"token operator\">::<\/span>RemoveTimer<span class=\"token punctuation\">,<\/span><span class=\"token keyword\">this<\/span><span class=\"token punctuation\">,<\/span>id<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">int<\/span> pos <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">(<\/span>_tick <span class=\"token operator\">&#043;<\/span> delay<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">%<\/span> _capacity<span class=\"token punctuation\">;<\/span><br \/>\n        _wheel<span class=\"token punctuation\">[<\/span>pos<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">push_back<\/span><span class=\"token punctuation\">(<\/span>spt<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        _timers<span class=\"token punctuation\">[<\/span>id<span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">WeakPtr<\/span><span class=\"token punctuation\">(<\/span>spt<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/\u5237\u65b0\/\u5ef6\u8fdf\u5b9a\u65f6\u4efb\u52a1<\/span><br \/>\n    <span class=\"token keyword\">void<\/span> <span class=\"token function\">TimerRefresh<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">uint64_t<\/span> id<span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">auto<\/span> it <span class=\"token operator\">&#061;<\/span> _timers<span class=\"token punctuation\">.<\/span><span class=\"token function\">find<\/span><span class=\"token punctuation\">(<\/span>id<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>it <span class=\"token operator\">&#061;&#061;<\/span> _timers<span class=\"token punctuation\">.<\/span><span class=\"token function\">end<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token keyword\">return<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n        <span class=\"token comment\">\/\/spt\u662f\u4e34\u65f6\u5bf9\u8c61,\u51fa\u6808\u540e\u4f1a\u81ea\u52a8\u9500\u6bc1,\u4e0d\u4f1a\u589e\u52a0\u5f15\u7528\u8ba1\u6570<\/span><br \/>\n        SharedPtr spt <span class=\"token operator\">&#061;<\/span> it<span class=\"token operator\">-&gt;<\/span>second<span class=\"token punctuation\">.<\/span><span class=\"token function\">lock<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><span class=\"token comment\">\/\/lock\u83b7\u53d6weak_ptr\u7ba1\u7406\u7684\u5bf9\u8c61\u5bf9\u5e94\u7684shared_ptr<\/span><br \/>\n        <span class=\"token keyword\">int<\/span> delaytime <span class=\"token operator\">&#061;<\/span> spt<span class=\"token operator\">-&gt;<\/span><span class=\"token function\">DelayTime<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">int<\/span> pos <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">(<\/span>_tick <span class=\"token operator\">&#043;<\/span> delaytime<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">%<\/span> _capacity<span class=\"token punctuation\">;<\/span><br \/>\n        _wheel<span class=\"token punctuation\">[<\/span>pos<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">push_back<\/span><span class=\"token punctuation\">(<\/span>spt<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/\u8fd9\u4e2a\u51fd\u6570\u5e94\u8be5\u6bcf\u79d2\u949f\u88ab\u6267\u884c\u4e00\u6b21&#xff0c;\u76f8\u5f53\u4e8e\u79d2\u9488\u5411\u540e\u8d70\u4e86\u4e00\u6b65<\/span><br \/>\n    <span class=\"token keyword\">void<\/span> <span class=\"token function\">RunTimerTask<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        _tick <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">(<\/span>_tick <span class=\"token operator\">&#043;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">%<\/span> _capacity<span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token comment\">\/\/\u6e05\u7a7a\u6307\u5b9a\u4f4d\u7f6e\u7684\u6570\u7ec4&#xff0c;\u5c31\u4f1a\u628a\u6570\u7ec4\u4e2d\u4fdd\u5b58\u7684\u6240\u6709\u7ba1\u7406\u5b9a\u65f6\u5668\u5bf9\u8c61\u7684shared_ptr\u91ca\u653e\u6389<\/span><br \/>\n        _wheel<span class=\"token punctuation\">[<\/span>_tick<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">clear<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><span class=\"token comment\">\/\/\u6267\u884c\u4efb\u52a1,\u91ca\u653eshared_ptr\u6307\u9488,\u5f53\u5f15\u7528\u8ba1\u6570\u52300,\u8c03\u7528Task\u6790\u6784,\u6267\u884c\u5b9a\u65f6\u4efb\u52a1<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/\u53d6\u6d88\u5b9a\u65f6\u4efb\u52a1,\u7b49\u65f6\u95f4\u5230\u4e86,\u91ca\u653e\u4efb\u52a1\u5bf9\u8c61,\u4f46\u662f\u6790\u6784\u91cc\u9762\u7684\u8d85\u65f6\u4efb\u52a1\u4e0d\u53bb\u6267\u884c<\/span><br \/>\n    <span class=\"token keyword\">void<\/span> <span class=\"token function\">TimerCancel<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">uint64_t<\/span> id<span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">auto<\/span> it <span class=\"token operator\">&#061;<\/span> _timers<span class=\"token punctuation\">.<\/span><span class=\"token function\">find<\/span><span class=\"token punctuation\">(<\/span>id<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>it <span class=\"token operator\">&#061;&#061;<\/span> _timers<span class=\"token punctuation\">.<\/span><span class=\"token function\">end<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token keyword\">return<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n        <span class=\"token comment\">\/\/spt\u662f\u4e34\u65f6\u5bf9\u8c61,\u51fa\u6808\u540e\u4f1a\u81ea\u52a8\u9500\u6bc1,\u4e0d\u4f1a\u589e\u52a0\u5f15\u7528\u8ba1\u6570<\/span><br \/>\n        SharedPtr spt <span class=\"token operator\">&#061;<\/span> it<span class=\"token operator\">-&gt;<\/span>second<span class=\"token punctuation\">.<\/span><span class=\"token function\">lock<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>spt<span class=\"token punctuation\">)<\/span> spt<span class=\"token operator\">-&gt;<\/span><span class=\"token function\">Cancel<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">class<\/span> <span class=\"token class-name\">Test<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">public<\/span><span class=\"token operator\">:<\/span><br \/>\n        <span class=\"token function\">Test<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span>std<span class=\"token operator\">::<\/span>cout <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;\u6784\u9020&#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> std<span class=\"token operator\">::<\/span>endl<span class=\"token punctuation\">;<\/span><span class=\"token punctuation\">}<\/span><br \/>\n        <span class=\"token operator\">~<\/span><span class=\"token function\">Test<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span>std<span class=\"token operator\">::<\/span>cout <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;\u6790\u6784&#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> std<span class=\"token operator\">::<\/span>endl<span class=\"token punctuation\">;<\/span><span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">void<\/span> <span class=\"token function\">DelTest<\/span><span class=\"token punctuation\">(<\/span>Test <span class=\"token operator\">*<\/span>t<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">delete<\/span> t<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token keyword\">int<\/span> <span class=\"token function\">main<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n    TimeWheel tw<span class=\"token punctuation\">;<\/span><\/p>\n<p>    Test <span class=\"token operator\">*<\/span>t <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token function\">Test<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    tw<span class=\"token punctuation\">.<\/span><span class=\"token function\">TimerAdd<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">888<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">5<\/span><span class=\"token punctuation\">,<\/span> std<span class=\"token operator\">::<\/span><span class=\"token function\">bind<\/span><span class=\"token punctuation\">(<\/span>DelTest<span class=\"token punctuation\">,<\/span> t<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token keyword\">for<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> i <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span> i <span class=\"token operator\">&lt;<\/span> <span class=\"token number\">5<\/span><span class=\"token punctuation\">;<\/span> i<span class=\"token operator\">&#043;&#043;<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token function\">sleep<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        tw<span class=\"token punctuation\">.<\/span><span class=\"token function\">TimerRefresh<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">888<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><span class=\"token comment\">\/\/\u5237\u65b0\u5b9a\u65f6\u4efb\u52a1<\/span><br \/>\n        tw<span class=\"token punctuation\">.<\/span><span class=\"token function\">RunTimerTask<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><span class=\"token comment\">\/\/\u5411\u540e\u79fb\u52a8\u79d2\u9488<\/span><br \/>\n        std<span class=\"token operator\">::<\/span>cout <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;\u5237\u65b0\u4e86\u4e00\u4e0b\u5b9a\u65f6\u4efb\u52a1&#xff0c;\u91cd\u65b0\u9700\u89815s\u4e2d\u540e\u624d\u4f1a\u9500\u6bc1\\\\n&#034;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    tw<span class=\"token punctuation\">.<\/span><span class=\"token function\">TimerCancel<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">888<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token keyword\">while<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token function\">sleep<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        std<span class=\"token operator\">::<\/span>cout <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-\\\\n&#034;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        tw<span class=\"token punctuation\">.<\/span><span class=\"token function\">RunTimerTask<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><span class=\"token comment\">\/\/\u5411\u540e\u79fb\u52a8\u79d2\u9488<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h3>4.3 \u6b63\u5219\u5e93\u7684\u7b80\u5355\u4f7f\u7528<\/h3>\n<p>\u6b63\u5219\u8868\u8fbe\u5f0f(regular expression)\u63cf\u8ff0\u4e86\u4e00\u79cd\u5b57\u7b26\u4e32\u5339\u914d\u7684\u6a21\u5f0f&#xff08;pattern&#xff09;&#xff0c;\u53ef\u4ee5\u7528\u6765\u68c0\u67e5\u4e00\u4e2a\u4e32\u662f\u5426\u542b\u6709\u67d0\u79cd\u5b50\u4e32\u3001\u5c06\u5339\u914d\u7684\u5b50\u4e32\u66ff\u6362\u6216\u8005\u4ece\u67d0\u4e2a\u4e32\u4e2d\u53d6\u51fa\u7b26\u5408\u67d0\u4e2a\u6761\u4ef6\u7684\u5b50\u4e32\u7b49\u3002<\/p>\n<p>\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u4f7f\u7528&#xff0c;\u53ef\u4ee5\u4f7f\u5f97HTTP\u8bf7\u6c42\u7684\u89e3\u6790\u66f4\u52a0\u7b80\u5355&#xff08;\u8fd9\u7528\u6307\u7684\u65f6\u7a0b\u5e8f\u5458\u7684\u5de5\u4f5c\u53d8\u5f97\u7684\u7b80\u5355&#xff0c;\u8fd9\u5e76\u4e0d\u4ee3\u8868\u5904\u7406\u6548\u7387\u4f1a\u53d8\u9ad8&#xff0c;\u5b9e\u9645\u4e0a\u6548\u7387\u4e0a\u662f\u4f4e\u4e8e\u76f4\u63a5\u7684\u5b57\u7b26\u4e32\u5904\u7406\u7684&#xff09;&#xff0c;\u4f7f\u6211\u4eec\u5b9e\u73b0\u7684HTTP\u7ec4\u4ef6\u5e93\u4f7f\u7528\u8d77\u6765\u66f4\u52a0\u7075\u6d3b\u3002<\/p>\n<p><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;iostream&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;regex&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;string&gt;<\/span><\/span><\/p>\n<p><span class=\"token keyword\">int<\/span> <span class=\"token function\">main<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/\/HTTP\u8bf7\u6c42\u884c\u683c\u5f0f&#xff1a;  GET \/bitejiuyeke\/login?user&#061;xiaoming&amp;pass&#061;123123 HTTP\/1.1\\\\r\\\\n<\/span><br \/>\n    <span class=\"token comment\">\/\/std::string str &#061; &#034;GET \/bitejiuyeke\/login?user&#061;xiaoming&amp;pass&#061;123123 HTTP\/1.1&#034;;<\/span><\/p>\n<p>    std<span class=\"token operator\">::<\/span>smatch matches<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token comment\">\/\/\u8bf7\u6c42\u65b9\u6cd5\u7684\u5339\u914d  GET HEAD POST PUT DELETE &#8230;.<\/span><br \/>\n    <span class=\"token comment\">\/\/std::regex e(&#034;(GET|HEAD|POST|PUT|DELETE) .*&#034;);\/\/\u9996\u5148\u5b57\u7b26\u4e32\u683c\u5f0f\u4e00\u5b9a\u8981\u5339\u914d,\u7136\u540e\u624d\u80fd\u63d0\u53d6\u5185\u5bb9<\/span><br \/>\n    <span class=\"token comment\">\/\/std::regex e(&#034;(GET|HEAD|POST|PUT|DELETE) ([^?]*).*&#034;);<\/span><br \/>\n    <span class=\"token comment\">\/\/std::regex e(&#034;(GET|HEAD|POST|PUT|DELETE) ([^?]*)\\\\\\\\?(.*) .*&#034;);<\/span><br \/>\n    <span class=\"token comment\">\/\/std::regex e(&#034;(GET|HEAD|POST|PUT|DELETE) ([^?]*)\\\\\\\\?(.*) (HTTP\/1\\\\\\\\.[01])&#034;);<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/std::string str &#061; &#034;GET \/bitejiuyeke\/login?user&#061;xiaoming&amp;pass&#061;123123 HTTP\/1.1\\\\r\\\\n&#034;;<\/span><br \/>\n    std<span class=\"token operator\">::<\/span>string str <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;GET \/bitejiuyeke\/login HTTP\/1.1\\\\r\\\\n&#034;<\/span> <span class=\"token punctuation\">;<\/span><br \/>\n    std<span class=\"token operator\">::<\/span>regex <span class=\"token function\">e<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;(GET|HEAD|POST|PUT|DELETE) ([^?]*)(?:\\\\\\\\?(.*))? (HTTP\/1\\\\\\\\.[01])(?:n|\\\\r\\\\n)?&#034;<\/span><span class=\"token punctuation\">,<\/span> std<span class=\"token operator\">::<\/span>regex<span class=\"token operator\">::<\/span>icase<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/() \u8868\u793a\u6355\u83b7\u7ec4,\u6ee1\u8db3\u91cc\u9762\u6761\u4ef6\u5c31\u6355\u83b7,\u4e0d\u7b26\u5408\u6761\u4ef6\u6355\u83b7\u7ec4\u5c31\u4e3a\u7a7a<\/span><br \/>\n    <span class=\"token comment\">\/\/(?:) \u8868\u793a\u975e\u6355\u83b7\u7ec4,\u5373\u4f7f\u6ee1\u8db3\u6761\u4ef6\u4f46\u4e5f\u4e0d\u6355\u83b7,\u4e0d\u6ee1\u8db3\u66f4\u4e0d\u6355\u83b7<\/span><br \/>\n    <span class=\"token comment\">\/\/ ? \u8868\u793a\u5339\u914d\u524d\u9762\u8868\u8fbe\u5f0f0\u6b21\u6216\u80051\u6b21<\/span><br \/>\n    <span class=\"token comment\">\/\/()? \u8868\u793a\u6ee1\u8db3()\u91cc\u6761\u4ef6\u5c31\u53ea\u53bb\u5339\u914d\u4e00\u6b21,\u5e76\u4e14\u7b26\u5408\u6761\u4ef6\u5c31\u53bb\u6355\u83b7,\u4e0d\u7b26\u5408\u6761\u4ef6\u6355\u83b7\u7ec4\u4e3a\u7a7a,\u4e0d\u6ee1\u8db3()\u91cc\u6761\u4ef6\u6355\u83b7\u7ec4\u4e5f\u662f\u4e3a\u7a7a<\/span><br \/>\n    <span class=\"token comment\">\/\/(?:)? \u8868\u793a\u6ee1\u8db3\u6216\u8005\u4e0d\u6ee1\u8db3\u6761\u4ef6\u90fd\u4e0d\u6355\u83b7<\/span><br \/>\n    <span class=\"token comment\">\/\/(?:\\\\\\\\?())? \u8868\u793a\u6709\u4ee5?\u4f5c\u4e3a\u5f00\u5934\u7684\u5b57\u7b26\u4e32&#xff0c;\u6ee1\u8db3()\u91cc\u9762\u6761\u4ef6\u5c31\u53bb\u6355\u83b7?\u540e\u9762\u7684,\u4e0d\u6ee1\u8db3()\u6355\u83b7\u7ec4\u4e3a\u7a7a<\/span><br \/>\n    <span class=\"token comment\">\/\/ . \u70b9\u5339\u914d\u9664&#034;\\\\n&#034;\u548c&#034;\\\\r&#034;\u4e4b\u5916\u7684\u4efb\u4f55\u5355\u4e2a\u5b57\u7b26<\/span><br \/>\n    <span class=\"token comment\">\/\/ [] \u53ea\u5141\u8bb8\u5b57\u7b26\u4e32\u4e2d\u4e00\u4e2a\u5b57\u7b26,\u53bb\u5339\u914d[]\u62ec\u53f7\u91cc\u9762\u7684\u4efb\u610f\u4e00\u4e2a\u5b57\u7b26, \\\\r [\\\\r\\\\n] \u53ef\u4ee5, \\\\r\\\\n [\\\\r\\\\n] \u4e0d\u884c<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ GET|HEAD|POST|PUT|DELETE   |\u8868\u793a\u5339\u914d\u5e76\u63d0\u53d6\u5176\u4e2d\u4efb\u610f\u4e00\u4e2a\u5b57\u7b26\u4e32<\/span><br \/>\n    <span class=\"token comment\">\/\/ [^?]*     [^?]\u5339\u914d\u975e\u95ee\u53f7\u5b57\u7b26&#xff0c; \u540e\u8fb9\u7684*\u8868\u793a0\u6b21\u6216\u591a\u6b21, &#043;\u8868\u793a1\u6b21\u6216\u591a\u6b21<\/span><br \/>\n    <span class=\"token comment\">\/\/ \\\\\\\\?(.*)   \\\\\\\\?  \u8868\u793a\u539f\u59cb\u7684&#xff1f;\u5b57\u7b26 (.*)\u8868\u793a\u63d0\u53d6?\u4e4b\u540e\u7684\u4efb\u610f\u5b57\u7b260\u6b21\u6216\u591a\u6b21&#xff0c;\u76f4\u5230\u9047\u5230\u7a7a\u683c<\/span><\/p>\n<p>    <span class=\"token comment\">\/*(?:\\\\\\\\?(.*?))? \u6700\u540e\u7684?\u8868\u2f70\u5339\u914d\u524d\u8fb9\u7684\u8868\u8fbe\u5f0f0\u6b21\u62161\u6b21&#xff0c;\u56e0\u4e3a\u6709\u7684\u8bf7\u6c42\u53ef\u80fd\u6ca1\u6709\u67e5\u8be2\u5b57\u7b26\u4e32<br \/>\n      (?:\\\\\\\\?(.*?)) (?:pattern)\u8868\u2f70\u5339\u914dpattern\u4f46\u662f\u4e0d\u83b7\u53d6\u5339\u914d\u7ed3\u679c<br \/>\n      \\\\\\\\?(.*?) \\\\\\\\?\u8868\u2f70\u539f\u59cb\u7684?\u5b57\u7b26&#xff0c;\u8fd9\u2fa5\u8868\u2f70\u4ee5?\u5b57\u7b26\u4f5c\u4e3a\u8d77\u59cb<br \/>\n      . \u8868\u2f70\\\\n\u4e4b\u5916\u4efb\u610f\u5355\u5b57\u7b26&#xff0c;<br \/>\n      * \u8868\u2f70\u5339\u914d\u524d\u8fb9\u7684\u5b57\u7b260\u6b21\u6216\u591a\u6b21,<br \/>\n   ?\u8ddf\u5728*\u6216&#043;\u4e4b\u540e\u8868\u2f70\u61d2\u60f0\u6a21\u5f0f, \u4e5f\u5c31\u662f\u8bf4\u4ee5?\u5f00\u59cb\u7684\u5b57\u7b26\u4e32\u5c31\u53ea\u5339\u914d\u8fd9\u2f00\u6b21\u5c31\u2f8f&#xff0c;\u540e\u8fb9\u8fd8\u6709\u4ee5?\u5f00\u59cb\u7684\u540c\u683c\u5f0f\u5b57\u7b26\u4e32\u4e5f\u4e0d\u4e0d\u4f1a\u5339\u914d<br \/>\n () \u8868\u2f70\u6355\u6349\u83b7\u53d6\u7b26\u5408\u5185\u90e8\u683c\u5f0f\u7684\u6570\u636e<br \/>\n     \u5408\u5e76\u8d77\u6765\u8868\u2f70\u7684\u5c31\u662f&#xff0c;\u5339\u914d\u4ee5?\u5f00\u59cb\u7684\u5b57\u7b26\u4e32&#xff0c;\u4f46\u662f\u5b57\u7b26\u4e32\u6574\u4f53\u4e0d\u8981&#xff0c;<br \/>\n     \u53ea\u6355\u6349\u83b7\u53d6?\u4e4b\u540e\u7684\u5b57\u7b26\u4e32,\u4e14\u53ea\u5339\u914d\u2f00\u6b21&#xff0c;\u5c31\u7b97\u540e\u8fb9\u8fd8\u6709\u4ee5?\u5f00\u59cb\u7684\u540c\u683c\u5f0f\u5b57\u7b26\u4e32\u4e5f\u4e0d\u4e0d\u4f1a\u5339\u914d<br \/>\n   *\/<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ HTTP\/1\\\\\\\\.[01]  \u8868\u793a\u5339\u914d\u4ee5HTTP\/1.\u5f00\u59cb&#xff0c;\u540e\u8fb9\u6709\u4e2a0\u62161\u7684\u5b57\u7b26\u4e32<\/span><br \/>\n    <span class=\"token comment\">\/\/(?:\\\\n|\\\\r\\\\n)?   &#xff08;?: &#8230;&#xff09; \u8868\u793a\u5339\u914d\u67d0\u4e2a\u683c\u5f0f\u5b57\u7b26\u4e32&#xff0c;\u4f46\u662f\u4e0d\u63d0\u53d6&#xff0c; \u6700\u540e\u7684&#xff1f;\u8868\u793a\u7684\u662f\u5339\u914d\u524d\u8fb9\u7684\u8868\u8fbe\u5f0f0\u6b21\u62161\u6b21(\u524d\u9762\u6709\u5b57\u7b26\u4e32\u6216\u8005\u6ca1\u6709\u5b57\u7b26\u4e32\u90fd\u53ef\u4ee5)<\/span><\/p>\n<p><span class=\"token comment\">\/*bool std:regex_match (const std:string &amp;src, std:smatch &amp;matches, std:regex &amp;e)<br \/>\nsrc:\u539f\u59cb\u5b57\u7b26\u4e32<br \/>\nmatches&#xff1a;\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u4ece\u539f\u59cb\u5b57\u7b26\u4e32\u4e2d\u5339\u914d\u5e76\u63d0\u53d6\u7b26\u5408\u67d0\u79cd\u89c4\u5219\u7684\u6570\u636e&#xff0c;\u63d0\u53d6\u7684\u6570\u636e\u5c31\u653e\u5728matches\u4e2d\u662f\u4e00\u4e2a\u7c7b\u4f3c\u4e8e\u6570\u7ec4\u7684\u5bb9\u5668<br \/>\ne:\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5339\u914d\u89c4\u5219<br \/>\n\u8fd4\u56de\u503c&#xff1a;\u7528\u4e8e\u786e\u5b9a\u5339\u914d\u662f\u5426\u6210\u529f*\/<\/span><\/p>\n<p>    <span class=\"token keyword\">bool<\/span> ret <span class=\"token operator\">&#061;<\/span> std<span class=\"token operator\">::<\/span><span class=\"token function\">regex_match<\/span><span class=\"token punctuation\">(<\/span>str<span class=\"token punctuation\">,<\/span> matches<span class=\"token punctuation\">,<\/span> e<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>ret <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token boolean\">false<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token keyword\">for<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> i <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span> i <span class=\"token operator\">&lt;<\/span> matches<span class=\"token punctuation\">.<\/span><span class=\"token function\">size<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token operator\">&#043;&#043;<\/span>i<span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        std<span class=\"token operator\">::<\/span>cout <span class=\"token operator\">&lt;&lt;<\/span> i <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034; : &#034;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        std<span class=\"token operator\">::<\/span>cout <span class=\"token operator\">&lt;&lt;<\/span> matches<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&lt;&lt;<\/span> std<span class=\"token operator\">::<\/span>endl<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token keyword\">return<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h3>4.5 \u901a\u7528\u7c7b\u578bany\u7c7b\u578b\u7684\u5b9e\u73b0<\/h3>\n<p>\u6bcf\u4e00\u4e2aConnection\u5bf9\u8fde\u63a5\u8fdb\u884c\u7ba1\u7406&#xff0c;\u6700\u7ec8\u90fd\u4e0d\u53ef\u907f\u514d\u9700\u8981\u6d89\u53ca\u5230\u5e94\u7528\u5c42\u534f\u8bae\u7684\u5904\u7406&#xff0c;\u56e0\u6b64\u5728Connection\u4e2d\u9700\u8981\u8bbe\u7f6e\u534f\u8bae\u5904\u7406\u7684\u4e0a\u4e0b\u6587\u6765\u63a7\u5236\u5904\u7406\u8282\u594f\u3002\u4f46\u662f\u5e94\u7528\u5c42\u534f\u8bae\u5343\u5343\u4e07&#xff0c;\u4e3a\u4e86\u964d\u4f4e\u8026\u5408\u5ea6&#xff0c;\u8fd9\u4e2a\u534f\u8bae\u63a5\u6536\u89e3\u6790\u4e0a\u4e0b\u6587\u5c31\u4e0d\u80fd\u6709\u660e\u663e\u7684\u534f\u8bae\u503e\u5411&#xff0c;\u5b83\u53ef\u4ee5\u662f\u4efb\u610f\u534f\u8bae\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f&#xff0c;\u56e0\u6b64\u5c31\u9700\u8981\u4e00\u4e2a\u901a\u7528\u7684\u7c7b\u578b\u6765\u4fdd\u5b58\u5404\u79cd\u4e0d\u540c\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<p>\u5728C\u8bed\u8a00\u4e2d&#xff0c;\u901a\u2f64\u7c7b\u578b\u53ef\u4ee5\u4f7f\u7528void*\u6765\u7ba1\u7406&#xff0c;\u4f46\u662f\u5728C&#043;&#043;\u4e2d&#xff0c;boost\u5e93\u548cC&#043;&#043;17\u7ed9\u6211\u4eec\u63d0\u4f9b\u4e86\u2f00\u4e2a\u901a\u7528\u7c7b\u578bany\u6765\u7075\u6d3b\u4f7f\u7528&#xff0c;\u5982\u679c\u8003\u8651\u589e\u52a0\u4ee3\u7801\u7684\u79fb\u690d\u6027&#xff0c;\u5c3d\u91cf\u51cf\u5c11\u7b2c\u4e09\u65b9\u5e93\u7684\u4f9d\u8d56&#xff0c;\u5219\u53ef\u4ee5\u4f7f\u7528C&#043;&#043;17\u7279\u6027\u4e2d\u7684any&#xff0c;\u6216\u8005\u81ea\u5df1\u6765\u5b9e\u73b0\u3002\u800c\u8fd9\u4e2aany\u901a\u7528\u7c7b\u578b\u7c7b\u7684\u5b9e\u73b0\u5176\u5b9e\u5e76\u4e0d\u590d\u6742&#xff0c;\u4ee5\u4e0b\u662f\u7b80\u5355\u7684\u90e8\u5206\u5b9e\u73b0\u3002<\/p>\n<p><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;iostream&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;typeinfo&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;cassert&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;unistd.h&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;any&gt;<\/span><\/span><\/p>\n<p><span class=\"token comment\">\/*Any\u7c7b\u4e3b\u8981\u662f\u5b9e\u73b0\u2f00\u4e2a\u901a\u2f64\u7c7b\u578b\u51fa\u6765&#xff0c;\u5728c&#043;&#043;17\u548cboost\u5e93\u4e2d\u90fd\u6709\u73b0\u6210\u7684\u53ef\u4ee5\u4f7f\u2f64&#xff0c;\u4f46\u662f\u8fd9\u2fa5\u5b9e\u73b0\u2f00\u4e0b\u4e86\u89e3\u5176\u601d\u60f3&#xff0c;\u8fd9\u6837\u4e5f\u5c31\u907f\u514d\u4e86\u7b2c\u4e09\u2f45\u5e93\u7684\u4f7f\u2f64\u4e86*\/<\/span><\/p>\n<p><span class=\"token comment\">\/*\u2fb8\u5148Any\u7c7b\u80af\u5b9a\u4e0d\u80fd\u662f\u2f00\u4e2a\u6a21\u677f\u7c7b&#xff0c;\u5426\u5219\u7f16\u8bd1\u7684\u65f6\u5019 Any&lt;int&gt; a, Any&lt;float&gt;b,\u9700\u8981\u4f20\u7c7b\u578b\u4f5c\u4e3a\u6a21\u677f\u53c2\u6570&#xff0c;\u4e5f\u5c31\u662f\u8bf4\u5728\u4f7f\u2f64\u7684\u65f6\u5019\u5c31\u8981\u786e\u5b9a\u5176\u7c7b\u578b*\/<\/span><\/p>\n<p><span class=\"token comment\">\/*\u56e0\u6b64\u8003\u8651Any\u5185\u90e8\u8bbe\u8ba1\u2f00\u4e2a\u6a21\u677f\u5bb9\u5668placehoder\u7c7b&#xff0c;\u53ef\u4ee5\u4fdd\u5b58\u5404\u79cd\u7c7b\u578b\u6570\u636e*\/<\/span><\/p>\n<p><span class=\"token comment\">\/*\u4f46\u5728Any\u7c7b\u4e2d\u2f46\u6cd5\u5b9a\u4e49\u8fd9\u4e2aplacehoder\u5bf9\u8c61\u6216\u6307\u9488&#xff0c;\u56e0\u4e3aany\u4e5f\u4e0d\u77e5\u9053\u8fd9\u4e2a\u7c7b\u8981\u4fdd\u5b58\u4ec0\u4e48\u7c7b\u578b\u7684\u6570\u636e&#xff0c;\u56e0\u6b64\u2f46\u6cd5\u4f20\u9012\u7c7b\u578b\u53c2\u6570*\/<\/span><\/p>\n<p><span class=\"token comment\">\/*\u6240\u4ee5&#xff0c;\u5b9a\u4e49\u2f00\u4e2a\u57fa\u7c7b\u8ba9holder&#xff0c;\u8ba9placeholde\u7ee7\u627f\u4e8e\u8ba9holder&#xff0c;\u2f7dAny\u7c7b\u4fdd\u5b58\u2f57\u7c7b\u6307\u9488\u5373\u53ef*\/<\/span><\/p>\n<p><span class=\"token comment\">\/*\u5f53\u9700\u8981\u4fdd\u5b58\u6570\u636e\u65f6&#xff0c;\u5219new\u2f00\u4e2a\u5e26\u6709\u6a21\u677f\u53c2\u6570\u7684\u2f26\u7c7bholder\u5bf9\u8c61\u51fa\u6765\u4fdd\u5b58\u6570\u636e&#xff0c;\u7136\u540e\u8ba9Any\u7c7b\u4e2d\u7684\u2f57\u7c7b\u6307\u9488&#xff0c;\u6307\u5411\u8fd9\u4e2a\u2f26\u7c7b\u5bf9\u8c61\u5c31\u641e\u5b9a\u4e86*\/<\/span><\/p>\n<p><span class=\"token keyword\">class<\/span> <span class=\"token class-name\">Any<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">class<\/span> <span class=\"token class-name\">holder<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">public<\/span><span class=\"token operator\">:<\/span><br \/>\n            <span class=\"token keyword\">virtual<\/span> <span class=\"token operator\">~<\/span><span class=\"token function\">holder<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">{<\/span><span class=\"token punctuation\">}<\/span><br \/>\n            <span class=\"token keyword\">virtual<\/span> <span class=\"token keyword\">const<\/span> std<span class=\"token operator\">::<\/span>type_info<span class=\"token operator\">&amp;<\/span> <span class=\"token function\">type<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token keyword\">virtual<\/span> holder <span class=\"token operator\">*<\/span><span class=\"token function\">clone<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token keyword\">template<\/span><span class=\"token operator\">&lt;<\/span><span class=\"token keyword\">class<\/span> <span class=\"token class-name\">T<\/span><span class=\"token operator\">&gt;<\/span><br \/>\n    <span class=\"token keyword\">class<\/span> <span class=\"token class-name\">placeholder<\/span> <span class=\"token operator\">:<\/span> <span class=\"token base-clause\"><span class=\"token keyword\">public<\/span> <span class=\"token class-name\">holder<\/span><\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">public<\/span><span class=\"token operator\">:<\/span><\/p>\n<p>        <span class=\"token function\">placeholder<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">const<\/span> T<span class=\"token operator\">&amp;<\/span> val<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">:<\/span> <span class=\"token function\">_val<\/span><span class=\"token punctuation\">(<\/span>val<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><span class=\"token punctuation\">}<\/span><\/p>\n<p>        <span class=\"token operator\">~<\/span><span class=\"token function\">placeholder<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">{<\/span><span class=\"token punctuation\">}<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ \u83b7\u53d6\u5b50\u7c7b\u5bf9\u8c61\u4fdd\u5b58\u7684\u6570\u636e\u7c7b\u578b<\/span><br \/>\n        <span class=\"token keyword\">virtual<\/span> <span class=\"token keyword\">const<\/span> std<span class=\"token operator\">::<\/span>type_info<span class=\"token operator\">&amp;<\/span> <span class=\"token function\">type<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token keyword\">return<\/span> <span class=\"token keyword\">typeid<\/span><span class=\"token punctuation\">(<\/span>T<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ \u9488\u5bf9\u5f53\u524d\u7684\u5bf9\u8c61\u81ea\u8eab&#xff0c;\u514b\u9686\u51fa\u4e00\u4e2a\u65b0\u7684\u5b50\u7c7b\u5bf9\u8c61<\/span><br \/>\n        <span class=\"token keyword\">virtual<\/span> holder <span class=\"token operator\">*<\/span><span class=\"token function\">clone<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token keyword\">return<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token function\">placeholder<\/span><span class=\"token punctuation\">(<\/span>_val<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token keyword\">public<\/span><span class=\"token operator\">:<\/span><br \/>\n        T _val<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">private<\/span><span class=\"token operator\">:<\/span><br \/>\n    holder<span class=\"token operator\">*<\/span> _content<span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">public<\/span><span class=\"token operator\">:<\/span><br \/>\n    <span class=\"token function\">Any<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token operator\">:<\/span><span class=\"token function\">_content<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">nullptr<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">{<\/span><span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token operator\">~<\/span><span class=\"token function\">Any<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">delete<\/span> _content<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token keyword\">template<\/span><span class=\"token operator\">&lt;<\/span><span class=\"token keyword\">class<\/span> <span class=\"token class-name\">T<\/span><span class=\"token operator\">&gt;<\/span><br \/>\n    <span class=\"token function\">Any<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">const<\/span> T<span class=\"token operator\">&amp;<\/span> val<span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        _content <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">new<\/span> placeholder<span class=\"token operator\">&lt;<\/span>T<span class=\"token operator\">&gt;<\/span><span class=\"token punctuation\">(<\/span>val<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token function\">Any<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">const<\/span> Any<span class=\"token operator\">&amp;<\/span> other<span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        _content <span class=\"token operator\">&#061;<\/span> _content <span class=\"token operator\">!&#061;<\/span> <span class=\"token keyword\">nullptr<\/span> <span class=\"token operator\">?<\/span> other<span class=\"token punctuation\">.<\/span>_content<span class=\"token operator\">-&gt;<\/span><span class=\"token function\">clone<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">:<\/span> <span class=\"token keyword\">nullptr<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u8fd4\u56de\u5b50\u7c7b\u5bf9\u8c61\u4fdd\u5b58\u7684\u6570\u636e\u7684\u6307\u9488<\/span><br \/>\n    <span class=\"token keyword\">template<\/span><span class=\"token operator\">&lt;<\/span><span class=\"token keyword\">class<\/span> <span class=\"token class-name\">T<\/span><span class=\"token operator\">&gt;<\/span><br \/>\n    T<span class=\"token operator\">*<\/span> <span class=\"token function\">Get<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token function\">assert<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">typeid<\/span><span class=\"token punctuation\">(<\/span>T<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;&#061;<\/span> _content<span class=\"token operator\">-&gt;<\/span><span class=\"token function\">type<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token comment\">\/\/\u7236\u7c7b\u6307\u9488\u6307\u5411\u5b50\u7c7b\u4e2d\u5c5e\u4e8e\u7236\u7c7b\u7684,\u627e\u4e0d\u5230\u5b50\u7c7b\u4e2d\u6210\u5458,\u56e0\u6b64\u9700\u8981\u8f6c\u6210\u5b50\u7c7b\u6307\u9488<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token operator\">&amp;<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">(<\/span>placeholder<span class=\"token operator\">&lt;<\/span>T<span class=\"token operator\">&gt;<\/span><span class=\"token operator\">*<\/span><span class=\"token punctuation\">)<\/span>_content<span class=\"token punctuation\">)<\/span><span class=\"token operator\">-&gt;<\/span>_val<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    Any <span class=\"token operator\">&amp;<\/span><span class=\"token function\">swap<\/span><span class=\"token punctuation\">(<\/span>Any <span class=\"token operator\">&amp;<\/span>other<span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        std<span class=\"token operator\">::<\/span><span class=\"token function\">swap<\/span><span class=\"token punctuation\">(<\/span>_content<span class=\"token punctuation\">,<\/span> other<span class=\"token punctuation\">.<\/span>_content<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token operator\">*<\/span><span class=\"token keyword\">this<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/\u8d4b\u503c\u8fd0\u7b97\u7b26\u7684\u91cd\u8f7d\u51fd\u6570<\/span><br \/>\n    <span class=\"token keyword\">template<\/span><span class=\"token operator\">&lt;<\/span><span class=\"token keyword\">class<\/span> <span class=\"token class-name\">T<\/span><span class=\"token operator\">&gt;<\/span><br \/>\n    Any<span class=\"token operator\">&amp;<\/span> <span class=\"token keyword\">operator<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">const<\/span> T <span class=\"token operator\">&amp;<\/span>val<span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token function\">Any<\/span><span class=\"token punctuation\">(<\/span>val<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">swap<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">*<\/span><span class=\"token keyword\">this<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token operator\">*<\/span><span class=\"token keyword\">this<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    Any<span class=\"token operator\">&amp;<\/span> <span class=\"token keyword\">operator<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">const<\/span> Any <span class=\"token operator\">&amp;<\/span>other<span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token function\">Any<\/span><span class=\"token punctuation\">(<\/span>other<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">swap<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">*<\/span><span class=\"token keyword\">this<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token operator\">*<\/span><span class=\"token keyword\">this<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">class<\/span> <span class=\"token class-name\">Test<\/span><span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">public<\/span><span class=\"token operator\">:<\/span><br \/>\n        <span class=\"token function\">Test<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span>std<span class=\"token operator\">::<\/span>cout <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;\u6784\u9020&#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> std<span class=\"token operator\">::<\/span>endl<span class=\"token punctuation\">;<\/span><span class=\"token punctuation\">}<\/span><br \/>\n        <span class=\"token function\">Test<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">const<\/span> Test <span class=\"token operator\">&amp;<\/span>t<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span>std<span class=\"token operator\">::<\/span>cout <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;\u62f7\u8d1d&#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> std<span class=\"token operator\">::<\/span>endl<span class=\"token punctuation\">;<\/span><span class=\"token punctuation\">}<\/span><br \/>\n        <span class=\"token operator\">~<\/span><span class=\"token function\">Test<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span>std<span class=\"token operator\">::<\/span>cout <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;\u6790\u6784&#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> std<span class=\"token operator\">::<\/span>endl<span class=\"token punctuation\">;<\/span><span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">int<\/span> <span class=\"token function\">main<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><\/p>\n<p>    std<span class=\"token operator\">::<\/span>any a<span class=\"token punctuation\">;<\/span><br \/>\n    a <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">10<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> <span class=\"token operator\">*<\/span>pi <span class=\"token operator\">&#061;<\/span> std<span class=\"token operator\">::<\/span>any_cast<span class=\"token operator\">&lt;<\/span><span class=\"token keyword\">int<\/span><span class=\"token operator\">&gt;<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>a<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    std<span class=\"token operator\">::<\/span>cout <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token operator\">*<\/span>pi <span class=\"token operator\">&lt;&lt;<\/span> std<span class=\"token operator\">::<\/span>endl<span class=\"token punctuation\">;<\/span><\/p>\n<p>    a <span class=\"token operator\">&#061;<\/span> std<span class=\"token operator\">::<\/span><span class=\"token function\">string<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;hello&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    std<span class=\"token operator\">::<\/span>string <span class=\"token operator\">*<\/span>ps <span class=\"token operator\">&#061;<\/span> std<span class=\"token operator\">::<\/span>any_cast<span class=\"token operator\">&lt;<\/span>std<span class=\"token operator\">::<\/span>string<span class=\"token operator\">&gt;<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>a<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    std<span class=\"token operator\">::<\/span>cout <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token operator\">*<\/span>ps <span class=\"token operator\">&lt;&lt;<\/span> std<span class=\"token operator\">::<\/span>endl<span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ Any a;<\/span><br \/>\n    <span class=\"token comment\">\/\/ {<\/span><br \/>\n    <span class=\"token comment\">\/\/     Test t;<\/span><br \/>\n    <span class=\"token comment\">\/\/     a &#061; t;<\/span><br \/>\n    <span class=\"token comment\">\/\/ }<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ a &#061; 10;<\/span><br \/>\n    <span class=\"token comment\">\/\/ int *pa &#061; a.Get&lt;int&gt;();<\/span><br \/>\n    <span class=\"token comment\">\/\/ std::cout &lt;&lt; *pa &lt;&lt; std::endl;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ a &#061; std::string(&#034;nihao&#034;);<\/span><br \/>\n    <span class=\"token comment\">\/\/ std::string *ps &#061; a.Get&lt;std::string&gt;();<\/span><br \/>\n    <span class=\"token comment\">\/\/ std::cout &lt;&lt; *ps &lt;&lt; std::endl;<\/span><\/p>\n<p>    <span class=\"token keyword\">while<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token function\">sleep<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.5k\u6b21\uff0c\u70b9\u8d5e57\u6b21\uff0c\u6536\u85cf64\u6b21\u3002\u4effmuduo\u5e93One Thread One Loop\u5f0f\u4e3b\u4eceReactor\u6a21\u578b\u5b9e\u73b0\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u8fd9\u4e2a\u9879\u76ee\u662f\u901a\u8fc7\u54b1\u4eec\u5b9e\u73b0\u7684\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u7ec4\u4ef6\uff0c\u53ef\u4ee5\u7b80\u6d01\u5feb\u901f\u7684\u5b8c\u6210\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684\u670d\u52a1\u5668\u642d\u5efa\u3002\u5e76\u4e14\uff0c\u901a\u8fc7\u7ec4\u4ef6\u5185\u63d0\u4f9b\u7684\u4e0d\u540c\u5e94\u7528\u5c42\u534f\u8bae\u652f\u6301\uff0c\u4e5f\u53ef\u4ee5\u5feb\u901f\u5b8c\u6210\u4e00\u4e2a\u9ad8\u6027\u80fd\u5e94\u7528\u670d\u52a1\u5668\u7684\u642d\u5efa\u3002\u5728\u8fd9\u91cc\uff0c\u8981\u660e\u786e\u7684\u662f\u54b1\u4eec\u8981\u5b9e\u73b0\u7684\u662f\u4e00\u4e2a\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u7ec4\u4ef6\uff0c\u56e0\u6b64\u5f53\u524d\u7684\u9879\u76ee\u4e2d\u5e76\u4e0d\u5305\u542b\u5b9e\u9645\u7684\u4e1a\u52a1\u5185\u5bb9\u3002<\/p>\n","protected":false},"author":2,"featured_media":8131,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[54,55,43],"topic":[],"class_list":["post-8148","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-muduo","tag-c","tag-43"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010\u4effMudou\u5e93one thread per loop\u5f0f\u5e76\u53d1\u670d\u52a1\u5668\u5b9e\u73b0\u3011\u9879\u76ee\u4ecb\u7ecd+\u524d\u7f6e\u6280\u672f\u77e5\u8bc6\u70b9 - \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\/8148.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010\u4effMudou\u5e93one thread per loop\u5f0f\u5e76\u53d1\u670d\u52a1\u5668\u5b9e\u73b0\u3011\u9879\u76ee\u4ecb\u7ecd+\u524d\u7f6e\u6280\u672f\u77e5\u8bc6\u70b9 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.5k\u6b21\uff0c\u70b9\u8d5e57\u6b21\uff0c\u6536\u85cf64\u6b21\u3002\u4effmuduo\u5e93One Thread One Loop\u5f0f\u4e3b\u4eceReactor\u6a21\u578b\u5b9e\u73b0\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u8fd9\u4e2a\u9879\u76ee\u662f\u901a\u8fc7\u54b1\u4eec\u5b9e\u73b0\u7684\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u7ec4\u4ef6\uff0c\u53ef\u4ee5\u7b80\u6d01\u5feb\u901f\u7684\u5b8c\u6210\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684\u670d\u52a1\u5668\u642d\u5efa\u3002\u5e76\u4e14\uff0c\u901a\u8fc7\u7ec4\u4ef6\u5185\u63d0\u4f9b\u7684\u4e0d\u540c\u5e94\u7528\u5c42\u534f\u8bae\u652f\u6301\uff0c\u4e5f\u53ef\u4ee5\u5feb\u901f\u5b8c\u6210\u4e00\u4e2a\u9ad8\u6027\u80fd\u5e94\u7528\u670d\u52a1\u5668\u7684\u642d\u5efa\u3002\u5728\u8fd9\u91cc\uff0c\u8981\u660e\u786e\u7684\u662f\u54b1\u4eec\u8981\u5b9e\u73b0\u7684\u662f\u4e00\u4e2a\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u7ec4\u4ef6\uff0c\u56e0\u6b64\u5f53\u524d\u7684\u9879\u76ee\u4e2d\u5e76\u4e0d\u5305\u542b\u5b9e\u9645\u7684\u4e1a\u52a1\u5185\u5bb9\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/8148.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-18T08:28:48+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418082839-68020d377a872.jpg\" \/>\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=\"12 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/8148.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/8148.html\",\"name\":\"\u3010\u4effMudou\u5e93one thread per loop\u5f0f\u5e76\u53d1\u670d\u52a1\u5668\u5b9e\u73b0\u3011\u9879\u76ee\u4ecb\u7ecd+\u524d\u7f6e\u6280\u672f\u77e5\u8bc6\u70b9 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-18T08:28:48+00:00\",\"dateModified\":\"2025-04-18T08:28:48+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/8148.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/8148.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/8148.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010\u4effMudou\u5e93one thread per loop\u5f0f\u5e76\u53d1\u670d\u52a1\u5668\u5b9e\u73b0\u3011\u9879\u76ee\u4ecb\u7ecd+\u524d\u7f6e\u6280\u672f\u77e5\u8bc6\u70b9\"}]},{\"@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":"\u3010\u4effMudou\u5e93one thread per loop\u5f0f\u5e76\u53d1\u670d\u52a1\u5668\u5b9e\u73b0\u3011\u9879\u76ee\u4ecb\u7ecd+\u524d\u7f6e\u6280\u672f\u77e5\u8bc6\u70b9 - \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\/8148.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010\u4effMudou\u5e93one thread per loop\u5f0f\u5e76\u53d1\u670d\u52a1\u5668\u5b9e\u73b0\u3011\u9879\u76ee\u4ecb\u7ecd+\u524d\u7f6e\u6280\u672f\u77e5\u8bc6\u70b9 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.5k\u6b21\uff0c\u70b9\u8d5e57\u6b21\uff0c\u6536\u85cf64\u6b21\u3002\u4effmuduo\u5e93One Thread One Loop\u5f0f\u4e3b\u4eceReactor\u6a21\u578b\u5b9e\u73b0\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u8fd9\u4e2a\u9879\u76ee\u662f\u901a\u8fc7\u54b1\u4eec\u5b9e\u73b0\u7684\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u7ec4\u4ef6\uff0c\u53ef\u4ee5\u7b80\u6d01\u5feb\u901f\u7684\u5b8c\u6210\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684\u670d\u52a1\u5668\u642d\u5efa\u3002\u5e76\u4e14\uff0c\u901a\u8fc7\u7ec4\u4ef6\u5185\u63d0\u4f9b\u7684\u4e0d\u540c\u5e94\u7528\u5c42\u534f\u8bae\u652f\u6301\uff0c\u4e5f\u53ef\u4ee5\u5feb\u901f\u5b8c\u6210\u4e00\u4e2a\u9ad8\u6027\u80fd\u5e94\u7528\u670d\u52a1\u5668\u7684\u642d\u5efa\u3002\u5728\u8fd9\u91cc\uff0c\u8981\u660e\u786e\u7684\u662f\u54b1\u4eec\u8981\u5b9e\u73b0\u7684\u662f\u4e00\u4e2a\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u7ec4\u4ef6\uff0c\u56e0\u6b64\u5f53\u524d\u7684\u9879\u76ee\u4e2d\u5e76\u4e0d\u5305\u542b\u5b9e\u9645\u7684\u4e1a\u52a1\u5185\u5bb9\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/8148.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-18T08:28:48+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418082839-68020d377a872.jpg"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"12 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/8148.html","url":"https:\/\/www.wsisp.com\/helps\/8148.html","name":"\u3010\u4effMudou\u5e93one thread per loop\u5f0f\u5e76\u53d1\u670d\u52a1\u5668\u5b9e\u73b0\u3011\u9879\u76ee\u4ecb\u7ecd+\u524d\u7f6e\u6280\u672f\u77e5\u8bc6\u70b9 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-18T08:28:48+00:00","dateModified":"2025-04-18T08:28:48+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/8148.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/8148.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/8148.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010\u4effMudou\u5e93one thread per loop\u5f0f\u5e76\u53d1\u670d\u52a1\u5668\u5b9e\u73b0\u3011\u9879\u76ee\u4ecb\u7ecd+\u524d\u7f6e\u6280\u672f\u77e5\u8bc6\u70b9"}]},{"@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\/8148","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=8148"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/8148\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/8131"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=8148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=8148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=8148"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=8148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}