{"id":11967,"date":"2025-04-18T19:07:07","date_gmt":"2025-04-18T11:07:07","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/11967.html"},"modified":"2025-04-18T19:07:07","modified_gmt":"2025-04-18T11:07:07","slug":"%e8%ae%a1%e7%bd%91%e5%ba%95%e5%b1%82%e5%b0%8f%e6%8e%a2%e7%b4%a2%e5%ae%9e%e7%8e%b0%e5%b9%b6%e9%83%a8%e7%bd%b2%e5%a4%9a%e7%ba%bf%e7%a8%8b%e5%b9%b6%e5%8f%91tcp%e6%9c%8d%e5%8a%a1%e5%99%a8%e6%a1%86","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/11967.html","title":{"rendered":"[\u8ba1\u7f51\u5e95\u5c42\u5c0f\u63a2\u7d22]:\u5b9e\u73b0\u5e76\u90e8\u7f72\u591a\u7ebf\u7a0b\u5e76\u53d1Tcp\u670d\u52a1\u5668\u6846\u67b6(\u57fa\u4e8e\u751f\u4ea7\u8005\u6d88\u8d39\u8005\u6a21\u578b\u7684\u7ebf\u7a0b\u6c60\u7ed3\u6784)"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418110703-6802325702876.gif\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\"><\/p>\n<\/p>\n<h4>\u6587\u7ae0\u76ee\u5f55<\/h4>\n<ul>\n<li>\u4e00.\u7f51\u7edc\u5c42\u4e0e\u4f20\u8f93\u5c42\u534f\u8bae<\/li>\n<li>\n<ul>\n<li>sockaddr\u7ed3\u6784\u4f53\u7ee7\u627f\u4f53\u7cfb(Linux\u4f53\u7cfb)<\/li>\n<li>\u8d2f\u7a7f\u8ba1\u7b97\u673a\u7cfb\u7edf\u7684\u7f51\u7edc\u901a\u4fe1\u67b6\u6784\u56fe\u793a:<\/li>\n<\/ul>\n<\/li>\n<li>\u4e8c.\u5b9e\u73b0\u5e76\u90e8\u7f72\u591a\u7ebf\u7a0b\u5e76\u53d1Tcp\u670d\u52a1\u5668\u6846\u67b6<\/li>\n<li>\n<ul>\n<li>\u7ebf\u7a0b\u6c60\u6a21\u5757<\/li>\n<li>\u5e8f\u5217\u5316\u53cd\u5e8f\u5217\u5316\u5de5\u5177\u6a21\u5757<\/li>\n<li>\u901a\u4fe1\u4fe1\u9053\u5efa\u7acb\u6a21\u5757<\/li>\n<li>\u670d\u52a1\u5668\u4e3b\u4f53\u6a21\u5757<\/li>\n<li>\u4efb\u52a1\u56de\u8c03\u6a21\u5757(\u6839\u636e\u5177\u4f53\u5e94\u7528\u573a\u666f\u53ef\u91cd\u6784)<\/li>\n<li>Tips:DebugC++\u4ee3\u7801\u8fc7\u7a0b\u4e2d\u9047\u5230\u7684\u95ee\u9898\u8bb0\u5f55<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418110703-680232572251b.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\"> <\/p>\n<h2>\u4e00.\u7f51\u7edc\u5c42\u4e0e\u4f20\u8f93\u5c42\u534f\u8bae<\/h2>\n<ul>\n<li>\u7f51\u7edc\u5c42\u4e0e\u4f20\u8f93\u5c42\u5185\u7f6e\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u7684\u5185\u6838\u4e2d,\u7f51\u7edc\u5c42\u4e00\u822c\u4f7f\u7528ip\u534f\u8bae,\u4f20\u8f93\u5c42\u5e38\u7528\u534f\u8bae\u4e3aTcp\u534f\u8bae\u548cUdp\u534f\u8bae,Tcp\u534f\u8bae\u548cUdp\u534f\u8bae\u62e5\u6709\u5404\u81ea\u7684\u7279\u70b9\u548c\u5e94\u7528\u573a\u666f: <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418110703-6802325759349.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\"><\/li>\n<\/ul>\n<h3>sockaddr\u7ed3\u6784\u4f53\u7ee7\u627f\u4f53\u7cfb(Linux\u4f53\u7cfb)<\/h3>\n<ul>\n<li>sockaddr_in\u7ed3\u6784\u4f53\u7528\u4e8e\u5b58\u50a8\u7f51\u7edc\u901a\u4fe1\u4e3b\u673a\u8fdb\u7a0b\u7684ip\u548c\u7aef\u53e3\u53f7\u7b49\u4fe1\u606f <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418110703-68023257a00c6.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\"><\/li>\n<\/ul>\n<h3>\u8d2f\u7a7f\u8ba1\u7b97\u673a\u7cfb\u7edf\u7684\u7f51\u7edc\u901a\u4fe1\u67b6\u6784\u56fe\u793a:<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418110704-6802325884626.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\"><\/p>\n<h2>\u4e8c.\u5b9e\u73b0\u5e76\u90e8\u7f72\u591a\u7ebf\u7a0b\u5e76\u53d1Tcp\u670d\u52a1\u5668\u6846\u67b6<\/h2>\n<p> \u5c0f\u9879\u76ee\u7684\u5b8c\u6574\u6587\u4ef6\u7684gittee\u94fe\u63a5<\/p>\n<ul>\n<li>Tcp\u670d\u52a1\u5668\u67b6\u6784: <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418110705-6802325985068.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\"><\/li>\n<\/ul>\n<h3>\u7ebf\u7a0b\u6c60\u6a21\u5757<\/h3>\n<p><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">pragma<\/span> <span class=\"token expression\">once<\/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;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;pthread.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\">\\&#8221;log.hpp\\&#8221;<\/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;semaphore.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;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;cstdio&gt;<\/span><\/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\">RingQueue<\/span><span class=\"token punctuation\">{<br \/>\n   <\/span><br \/>\n<span class=\"token keyword\">private<\/span><span class=\"token operator\">:<\/span><br \/>\n    pthread_mutex_t Clock_<span class=\"token punctuation\">;<\/span><br \/>\n    pthread_mutex_t Plock_<span class=\"token punctuation\">;<\/span><br \/>\n    sem_t Psem_<span class=\"token punctuation\">;<\/span><br \/>\n    sem_t Csem_<span class=\"token punctuation\">;<\/span><br \/>\n    std<span class=\"token double-colon punctuation\">::<\/span>vector<span class=\"token operator\">&lt;<\/span>T<span class=\"token operator\">&gt;<\/span> Queue_<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> Pptr_<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> Cptr_<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> capacity_<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">public<\/span><span class=\"token operator\">:<\/span><br \/>\n    <span class=\"token function\">RingQueue<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> capacity <span class=\"token operator\">=<\/span> <span class=\"token number\">10<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">:<\/span> <span class=\"token function\">Queue_<\/span><span class=\"token punctuation\">(<\/span>capacity<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><span class=\"token function\">Pptr_<\/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\">Cptr_<\/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>capacity<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">{<br \/>\n   <\/span><br \/>\n        <span class=\"token function\">sem_init<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>Psem_<span class=\"token punctuation\">,<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">,<\/span>capacity<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token function\">sem_init<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>Csem_<span class=\"token punctuation\">,<\/span><span class=\"token number\">0<\/span><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 function\">pthread_mutex_init<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>Clock_<span class=\"token punctuation\">,<\/span><span class=\"token keyword\">nullptr<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token function\">pthread_mutex_init<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>Plock_<span class=\"token punctuation\">,<\/span><span class=\"token keyword\">nullptr<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token operator\">~<\/span><span class=\"token function\">RingQueue<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">{<br \/>\n   <\/span><br \/>\n        <span class=\"token function\">sem_destroy<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>Psem_<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token function\">sem_destroy<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>Csem_<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token function\">pthread_mutex_destroy<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>Clock_<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token function\">pthread_mutex_destroy<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>Plock_<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    T <span class=\"token function\">Pop<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">{<br \/>\n   <\/span><br \/>\n        <span class=\"token function\">sem_wait<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>Csem_<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token function\">pthread_mutex_lock<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>Clock_<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        T tem <span class=\"token operator\">=<\/span> Queue_<span class=\"token punctuation\">[<\/span>Cptr_<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        Cptr_<span class=\"token operator\">++<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        Cptr_ <span class=\"token operator\">%=<\/span> capacity_<span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token function\">pthread_mutex_unlock<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>Clock_<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token function\">sem_post<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>Psem_<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> tem<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token keyword\">void<\/span> <span class=\"token function\">Push<\/span><span class=\"token punctuation\">(<\/span>T t<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">{<br \/>\n   <\/span><br \/>\n        <span class=\"token function\">sem_wait<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>Psem_<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token function\">pthread_mutex_lock<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>Plock_<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        Queue_<span class=\"token punctuation\">[<\/span>Pptr_<span class=\"token punctuation\">]<\/span> <span class=\"token operator\">=<\/span> t<span class=\"token punctuation\">;<\/span><br \/>\n        Pptr_<span class=\"token operator\">++<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        Pptr_<span class=\"token operator\">%=<\/span> capacity_<span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token function\">pthread_mutex_unlock<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>Plock_<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token function\">sem_post<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>Csem_<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">pragma<\/span> <span class=\"token expression\">once<\/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\">\\&#8221;sem_cp.cpp\\&#8221;<\/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;pthread.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;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;mutex&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\">\\&#8221;CalTask.cpp\\&#8221;<\/span><\/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\">Task<\/span><span class=\"token operator\">&gt;<\/span><br \/>\n<span class=\"token keyword\">class<\/span> <span class=\"token class-name\">Thread_Pool<\/span><span class=\"token punctuation\">{<br \/>\n   <\/span><br \/>\n    <span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">Thread_Data<\/span><span class=\"token punctuation\">{<br \/>\n   <\/span><br \/>\n        <span class=\"token keyword\">int<\/span> Thread_num<span class=\"token punctuation\">;<\/span><br \/>\n        pthread_t tid<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">private<\/span><span class=\"token operator\">:<\/span><br \/>\n    RingQueue<span class=\"token operator\">&lt;<\/span>Task<span class=\"token operator\">&gt;<\/span> Queue_<span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">\/\/\u7ebf\u7a0b\u5b89\u5168\u7684\u73af\u5f62\u961f\u5217<\/span><br \/>\n    std<span class=\"token double-colon punctuation\">::<\/span>vector<span class=\"token operator\">&lt;<\/span>Thread_Data<span class=\"token operator\">&gt;<\/span> thread_arr<span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/\u7ba1\u7406\u7ebf\u7a0b\u7684\u5bb9\u5668<\/span><br \/>\n    <span class=\"token keyword\">static<\/span> std<span class=\"token double-colon punctuation\">::<\/span>mutex lock_<span class=\"token punctuation\">;<\/span>            <span class=\"token comment\">\/\/\u5355\u4f8b\u9501<\/span><br \/>\n    <span class=\"token keyword\">static<\/span> Thread_Pool<span class=\"token operator\">&lt;<\/span>Task<span class=\"token operator\">&gt;<\/span> <span class=\"token operator\">*<\/span> ptr_<span class=\"token punctuation\">;<\/span>    <span class=\"token comment\">\/\/\u5355\u4f8b\u6307\u9488<\/span><br \/>\n<span class=\"token keyword\">private<\/span><span class=\"token operator\">:<\/span><br \/>\n    <span class=\"token function\">Thread_Pool<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> capacity_Of_queue <span class=\"token operator\">=<\/span> <span class=\"token number\">20<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">:<\/span> <span class=\"token function\">Queue_<\/span><span class=\"token punctuation\">(<\/span>capacity_Of_queue<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">{<br \/>\n   <\/span><span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token function\">Thread_Pool<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">const<\/span> Thread_Pool<span class=\"token operator\">&lt;<\/span>Task<span class=\"token operator\">&gt;<\/span><span class=\"token operator\">&amp;<\/span> Tp<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">=<\/span> <span class=\"token keyword\">delete<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    Thread_Pool<span class=\"token operator\">&lt;<\/span>Task<span class=\"token operator\">&gt;<\/span><span class=\"token operator\">&amp;<\/span> <span class=\"token keyword\">operator<\/span><span class=\"token operator\">=<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">const<\/span> Thread_Pool<span class=\"token operator\">&lt;<\/span>Task<span class=\"token operator\">&gt;<\/span> <span class=\"token operator\">&amp;<\/span> Tp<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">=<\/span> <span class=\"token keyword\">delete<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">public<\/span><span class=\"token operator\">:<\/span><br \/>\n    <span class=\"token operator\">~<\/span><span class=\"token function\">Thread_Pool<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">{<br \/>\n   <\/span><span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token comment\">\/\/\u83b7\u53d6\u7ebf\u7a0b\u6c60\u5355\u4f8b&#8211;&gt;\u6ce8\u610fC++\u7684\u7c7b\u6a21\u677f\u9759\u6001\u6210\u5458\u51fd\u6570\u9700\u8981\u5728\u7c7b\u4f53\u5916\u8fdb\u884c\u5b9a\u4e49<\/span><br \/>\n    <span class=\"token keyword\">static<\/span> Thread_Pool<span class=\"token operator\">&lt;<\/span>Task<span class=\"token operator\">&gt;<\/span> <span class=\"token operator\">*<\/span> <span class=\"token function\">Getinstance<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token comment\">\/\/\u521b\u5efa\u591a\u7ebf\u7a0b<\/span><br \/>\n    <span class=\"token keyword\">void<\/span> <span class=\"token function\">Create_thread<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> thread_num <span class=\"token operator\">=<\/span> <span class=\"token number\">10<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">{<br \/>\n   <\/span><br \/>\n        Thread_Data T_data<span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">for<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> i <span class=\"token operator\">=<\/span> <span class=\"token number\">0<\/span> <span class=\"token punctuation\">;<\/span> i <span class=\"token operator\">&lt;<\/span> thread_num <span class=\"token punctuation\">;<\/span> <span class=\"token operator\">++<\/span>i<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">{<br \/>\n   <\/span><br \/>\n            <span class=\"token comment\">\/\/\u6ce8\u610f\u7ebf\u7a0b\u6c60\u5bf9\u8c61\u7684this\u6307\u9488\u4f20\u9012\u7ed9\u7ebf\u7a0b<\/span><br \/>\n            <span class=\"token function\">pthread_create<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>T_data<span class=\"token punctuation\">.<\/span>tid<span class=\"token punctuation\">,<\/span><span class=\"token keyword\">nullptr<\/span><span class=\"token punctuation\">,<\/span>Routine<span class=\"token punctuation\">,<\/span><span class=\"token keyword\">this<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            T_data<span class=\"token punctuation\">.<\/span>Thread_num <span class=\"token operator\">=<\/span> i <span class=\"token operator\">+<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            thread_arr<span class=\"token punctuation\">.<\/span><span class=\"token function\">push_back<\/span><span class=\"token punctuation\">(<\/span>T_data<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token comment\">\/\/\u7ebf\u7a0b\u7b49\u5f85<\/span><br \/>\n    <span class=\"token keyword\">void<\/span> <span class=\"token function\">Thread_join<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">{<br \/>\n   <\/span><br \/>\n        <span class=\"token keyword\">for<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> i <span class=\"token operator\">=<\/span> <span class=\"token number\">0<\/span> <span class=\"token punctuation\">;<\/span>i <span class=\"token operator\">&lt;<\/span> thread_arr<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\">++<\/span>i<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">{<br \/>\n   <\/span><br \/>\n            <span class=\"token function\">pthread_join<\/span><span class=\"token punctuation\">(<\/span>thread_arr<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">.<\/span>tid<span class=\"token punctuation\">,<\/span><span class=\"token keyword\">nullptr<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb4.6k\u6b21\uff0c\u70b9\u8d5e73\u6b21\uff0c\u6536\u85cf61\u6b21\u3002\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5728Linux\u4f53\u7cfb\u4e0b\uff0c\u5982\u4f55\u4f7f\u7528C++\u6784\u5efa\u4e00\u4e2a\u5e76\u53d1\u7684Tcp\u670d\u52a1\u5668\u6846\u67b6\uff0c\u5305\u62ec\u7f51\u7edc\u5c42\u4e0e\u4f20\u8f93\u5c42\u7684sockaddr\u7ed3\u6784\uff0c\u7ebf\u7a0b\u6c60\u7684\u8bbe\u8ba1\uff0c\u4ee5\u53ca\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u5de5\u5177\u5728\u4fdd\u8bc1\u901a\u4fe1\u5b8c\u6574\u6027\u4e2d\u7684\u5e94\u7528\u3002<\/p>\n","protected":false},"author":2,"featured_media":11961,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[205,43,191,211,626],"topic":[],"class_list":["post-11967","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-tcp-ip","tag-43","tag-191","tag-211","tag-linux"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[\u8ba1\u7f51\u5e95\u5c42\u5c0f\u63a2\u7d22]:\u5b9e\u73b0\u5e76\u90e8\u7f72\u591a\u7ebf\u7a0b\u5e76\u53d1Tcp\u670d\u52a1\u5668\u6846\u67b6(\u57fa\u4e8e\u751f\u4ea7\u8005\u6d88\u8d39\u8005\u6a21\u578b\u7684\u7ebf\u7a0b\u6c60\u7ed3\u6784) - \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\/11967.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[\u8ba1\u7f51\u5e95\u5c42\u5c0f\u63a2\u7d22]:\u5b9e\u73b0\u5e76\u90e8\u7f72\u591a\u7ebf\u7a0b\u5e76\u53d1Tcp\u670d\u52a1\u5668\u6846\u67b6(\u57fa\u4e8e\u751f\u4ea7\u8005\u6d88\u8d39\u8005\u6a21\u578b\u7684\u7ebf\u7a0b\u6c60\u7ed3\u6784) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb4.6k\u6b21\uff0c\u70b9\u8d5e73\u6b21\uff0c\u6536\u85cf61\u6b21\u3002\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5728Linux\u4f53\u7cfb\u4e0b\uff0c\u5982\u4f55\u4f7f\u7528C++\u6784\u5efa\u4e00\u4e2a\u5e76\u53d1\u7684Tcp\u670d\u52a1\u5668\u6846\u67b6\uff0c\u5305\u62ec\u7f51\u7edc\u5c42\u4e0e\u4f20\u8f93\u5c42\u7684sockaddr\u7ed3\u6784\uff0c\u7ebf\u7a0b\u6c60\u7684\u8bbe\u8ba1\uff0c\u4ee5\u53ca\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u5de5\u5177\u5728\u4fdd\u8bc1\u901a\u4fe1\u5b8c\u6574\u6027\u4e2d\u7684\u5e94\u7528\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/11967.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-18T11:07:07+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418110703-6802325702876.gif\" \/>\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=\"2 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/11967.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/11967.html\",\"name\":\"[\u8ba1\u7f51\u5e95\u5c42\u5c0f\u63a2\u7d22]:\u5b9e\u73b0\u5e76\u90e8\u7f72\u591a\u7ebf\u7a0b\u5e76\u53d1Tcp\u670d\u52a1\u5668\u6846\u67b6(\u57fa\u4e8e\u751f\u4ea7\u8005\u6d88\u8d39\u8005\u6a21\u578b\u7684\u7ebf\u7a0b\u6c60\u7ed3\u6784) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-18T11:07:07+00:00\",\"dateModified\":\"2025-04-18T11:07:07+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/11967.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/11967.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/11967.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[\u8ba1\u7f51\u5e95\u5c42\u5c0f\u63a2\u7d22]:\u5b9e\u73b0\u5e76\u90e8\u7f72\u591a\u7ebf\u7a0b\u5e76\u53d1Tcp\u670d\u52a1\u5668\u6846\u67b6(\u57fa\u4e8e\u751f\u4ea7\u8005\u6d88\u8d39\u8005\u6a21\u578b\u7684\u7ebf\u7a0b\u6c60\u7ed3\u6784)\"}]},{\"@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":"[\u8ba1\u7f51\u5e95\u5c42\u5c0f\u63a2\u7d22]:\u5b9e\u73b0\u5e76\u90e8\u7f72\u591a\u7ebf\u7a0b\u5e76\u53d1Tcp\u670d\u52a1\u5668\u6846\u67b6(\u57fa\u4e8e\u751f\u4ea7\u8005\u6d88\u8d39\u8005\u6a21\u578b\u7684\u7ebf\u7a0b\u6c60\u7ed3\u6784) - \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\/11967.html","og_locale":"zh_CN","og_type":"article","og_title":"[\u8ba1\u7f51\u5e95\u5c42\u5c0f\u63a2\u7d22]:\u5b9e\u73b0\u5e76\u90e8\u7f72\u591a\u7ebf\u7a0b\u5e76\u53d1Tcp\u670d\u52a1\u5668\u6846\u67b6(\u57fa\u4e8e\u751f\u4ea7\u8005\u6d88\u8d39\u8005\u6a21\u578b\u7684\u7ebf\u7a0b\u6c60\u7ed3\u6784) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb4.6k\u6b21\uff0c\u70b9\u8d5e73\u6b21\uff0c\u6536\u85cf61\u6b21\u3002\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5728Linux\u4f53\u7cfb\u4e0b\uff0c\u5982\u4f55\u4f7f\u7528C++\u6784\u5efa\u4e00\u4e2a\u5e76\u53d1\u7684Tcp\u670d\u52a1\u5668\u6846\u67b6\uff0c\u5305\u62ec\u7f51\u7edc\u5c42\u4e0e\u4f20\u8f93\u5c42\u7684sockaddr\u7ed3\u6784\uff0c\u7ebf\u7a0b\u6c60\u7684\u8bbe\u8ba1\uff0c\u4ee5\u53ca\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u5de5\u5177\u5728\u4fdd\u8bc1\u901a\u4fe1\u5b8c\u6574\u6027\u4e2d\u7684\u5e94\u7528\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/11967.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-18T11:07:07+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418110703-6802325702876.gif"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"2 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/11967.html","url":"https:\/\/www.wsisp.com\/helps\/11967.html","name":"[\u8ba1\u7f51\u5e95\u5c42\u5c0f\u63a2\u7d22]:\u5b9e\u73b0\u5e76\u90e8\u7f72\u591a\u7ebf\u7a0b\u5e76\u53d1Tcp\u670d\u52a1\u5668\u6846\u67b6(\u57fa\u4e8e\u751f\u4ea7\u8005\u6d88\u8d39\u8005\u6a21\u578b\u7684\u7ebf\u7a0b\u6c60\u7ed3\u6784) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-18T11:07:07+00:00","dateModified":"2025-04-18T11:07:07+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/11967.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/11967.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/11967.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"[\u8ba1\u7f51\u5e95\u5c42\u5c0f\u63a2\u7d22]:\u5b9e\u73b0\u5e76\u90e8\u7f72\u591a\u7ebf\u7a0b\u5e76\u53d1Tcp\u670d\u52a1\u5668\u6846\u67b6(\u57fa\u4e8e\u751f\u4ea7\u8005\u6d88\u8d39\u8005\u6a21\u578b\u7684\u7ebf\u7a0b\u6c60\u7ed3\u6784)"}]},{"@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\/11967","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=11967"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/11967\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/11961"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=11967"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=11967"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=11967"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=11967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}