{"id":64214,"date":"2026-01-23T03:08:18","date_gmt":"2026-01-22T19:08:18","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/64214.html"},"modified":"2026-01-23T03:08:18","modified_gmt":"2026-01-22T19:08:18","slug":"linux%e7%ba%bf%e7%a8%8b%e5%ae%9e%e6%88%98%ef%bc%9a%e4%bb%8e%e7%ad%89%e5%be%85%e5%88%b0c%e5%a4%9a%e7%ba%bf%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/64214.html","title":{"rendered":"Linux\u7ebf\u7a0b\u5b9e\u6218\uff1a\u4ece\u7b49\u5f85\u5230C++\u591a\u7ebf\u7a0b"},"content":{"rendered":"<p>Linux \u7ebf\u7a0b\u6df1\u5ea6\u6307\u5357&#xff1a;\u4ece\u7b49\u5f85\u3001\u5206\u79bb\u5230 C&#043;&#043; \u591a\u7ebf\u7a0b\u5b9e\u6218<\/p>\n<h3>1. \u7ebf\u7a0b\u57fa\u7840\u6982\u5ff5\u56de\u987e<\/h3>\n<p>\u5728 Linux \u7cfb\u7edf\u4e2d&#xff0c;\u7ebf\u7a0b\u662f\u8f7b\u91cf\u7ea7\u7684\u6267\u884c\u5355\u5143&#xff0c;\u5171\u4eab\u8fdb\u7a0b\u7684\u5730\u5740\u7a7a\u95f4\u3001\u6587\u4ef6\u63cf\u8ff0\u7b26\u7b49\u8d44\u6e90&#xff0c;\u4f46\u62e5\u6709\u72ec\u7acb\u7684\u6808\u3001\u5bc4\u5b58\u5668\u72b6\u6001\u548c\u7a0b\u5e8f\u8ba1\u6570\u5668\u3002\u4f7f\u7528 POSIX \u7ebf\u7a0b\u5e93 (pthread) \u8fdb\u884c\u7ebf\u7a0b\u64cd\u4f5c\u3002<\/p>\n<ul>\n<li>\u521b\u5efa\u7ebf\u7a0b&#xff1a;pthread_create<\/li>\n<li>\u83b7\u53d6\u7ebf\u7a0b ID&#xff1a;pthread_self<\/li>\n<li>\u7ec8\u6b62\u7ebf\u7a0b&#xff1a;pthread_exit \u6216\u4ece\u7ebf\u7a0b\u51fd\u6570\u8fd4\u56de\u3002<\/li>\n<\/ul>\n<h3>2. \u7ebf\u7a0b\u7684\u72b6\u6001\u7ba1\u7406&#xff1a;\u7b49\u5f85 (Joining) \u4e0e \u5206\u79bb (Detaching)<\/h3>\n<h4>2.1 \u7ebf\u7a0b\u7b49\u5f85 (pthread_join)<\/h4>\n<p>\u5f53\u4e00\u4e2a\u7ebf\u7a0b\u521b\u5efa\u4e86\u53e6\u4e00\u4e2a\u7ebf\u7a0b&#xff08;\u901a\u5e38\u79f0\u4e3a\u5b50\u7ebf\u7a0b&#xff09;\u65f6&#xff0c;\u7236\u7ebf\u7a0b\u53ef\u80fd\u9700\u8981\u7b49\u5f85\u5b50\u7ebf\u7a0b\u5b8c\u6210\u6267\u884c\u5e76\u83b7\u53d6\u5176\u9000\u51fa\u72b6\u6001\u3002\u8fd9\u901a\u8fc7 pthread_join \u5b9e\u73b0\u3002<\/p>\n<p>#include &lt;pthread.h&gt;<br \/>\nint pthread_join(pthread_t thread, void **retval);<\/p>\n<ul>\n<li>thread: \u8981\u7b49\u5f85\u7684\u7ebf\u7a0b ID\u3002<\/li>\n<li>retval: \u6307\u5411\u4e00\u4e2a\u6307\u9488\u7684\u6307\u9488&#xff0c;\u7528\u4e8e\u63a5\u6536\u76ee\u6807\u7ebf\u7a0b\u7684\u9000\u51fa\u72b6\u6001&#xff08;\u5373 pthread_exit \u4f20\u5165\u7684\u503c\u6216\u7ebf\u7a0b\u51fd\u6570\u7684\u8fd4\u56de\u503c&#xff09;\u3002\u53ef\u4e3a NULL\u3002<\/li>\n<li>\u8fd4\u56de\u503c&#xff1a;\u6210\u529f\u8fd4\u56de 0&#xff1b;\u9519\u8bef\u8fd4\u56de\u9519\u8bef\u7801\u3002<\/li>\n<\/ul>\n<p>\u4f5c\u7528&#xff1a;<\/p>\n<li>\u963b\u585e\u8c03\u7528\u7ebf\u7a0b&#xff0c;\u76f4\u5230\u6307\u5b9a\u7684 thread \u7ec8\u6b62\u3002<\/li>\n<li>\u56de\u6536\u76ee\u6807\u7ebf\u7a0b\u7684\u8d44\u6e90&#xff08;\u4e3b\u8981\u662f\u6808\u7a7a\u95f4&#xff09;\u3002<\/li>\n<li>\u83b7\u53d6\u76ee\u6807\u7ebf\u7a0b\u7684\u9000\u51fa\u72b6\u6001\u3002<\/li>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>void* thread_function(void* arg) {<br \/>\n    \/\/ &#8230; \u7ebf\u7a0b\u5de5\u4f5c &#8230;<br \/>\n    return (void*)42; \/\/ \u6216 pthread_exit((void*)42);<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    pthread_t tid;<br \/>\n    void *exit_status;<\/p>\n<p>    pthread_create(&amp;tid, NULL, thread_function, NULL);<br \/>\n    \/\/ &#8230; \u4e3b\u7ebf\u7a0b\u5176\u4ed6\u5de5\u4f5c &#8230;<br \/>\n    pthread_join(tid, &amp;exit_status); \/\/ \u7b49\u5f85\u5b50\u7ebf\u7a0b\u7ed3\u675f<br \/>\n    printf(&#034;Thread exited with status: %ld\\\\n&#034;, (long)exit_status);<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkzMDExLzY5NTk0MDczOC5zaHRtbA&#061;&#061;.html https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkzMDExLzY5NTk0MDkyMy5zaHRtbA&#061;&#061;.html https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkzMDExLzY5NTk0MDc1NC5zaHRtbA&#061;&#061;.html https:\/\/tv.sohu.com\/v\/dXMvNDM4OTIwMjE2LzY5NTk0MDk3Ny5zaHRtbA&#061;&#061;.html https:\/\/tv.sohu.com\/v\/dXMvNDM4OTIwMjE2LzY5NTk0MTIxNC5zaHRtbA&#061;&#061;.html https:\/\/tv.sohu.com\/v\/dXMvNDM4OTIwMjE2LzY5NTk0MTA3NC5zaHRtbA&#061;&#061;.html https:\/\/tv.sohu.com\/v\/dXMvNDM4OTIwMjE2LzY5NTk0MTIyMi5zaHRtbA&#061;&#061;.html https:\/\/tv.sohu.com\/v\/dXMvNDM4OTIwMjE2LzY5NTk0MTI1Ny5zaHRtbA&#061;&#061;.html https:\/\/tv.sohu.com\/v\/dXMvNDM4OTIwMjE2LzY5NTk0MTI2MS5zaHRtbA&#061;&#061;.html https:\/\/tv.sohu.com\/v\/dXMvNDM4OTIwMjE2LzY5NTk0MTEzNy5zaHRtbA&#061;&#061;.html https:\/\/tv.sohu.com\/v\/dXMvNDM4OTIwMjE2LzY5NTk0MTMyNi5zaHRtbA&#061;&#061;.html https:\/\/tv.sohu.com\/v\/dXMvNDM4OTIwMjE2LzY5NTk0MTQzMi5zaHRtbA&#061;&#061;.html https:\/\/tv.sohu.com\/v\/dXMvNDM4OTIwMjE2LzY5NTk0MTE0NS5zaHRtbA&#061;&#061;.html \u00a0<\/p>\n<p>\u5173\u952e\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u4e00\u4e2a\u7ebf\u7a0b\u53ea\u80fd\u88ab join \u4e00\u6b21\u3002<\/li>\n<li>\u5982\u679c\u7ebf\u7a0b\u5df2\u88ab join \u6216\u6807\u8bb0\u4e3a detached&#xff0c;\u518d\u6b21 join \u4f1a\u51fa\u9519\u3002<\/li>\n<li>\u672a join \u7684\u975e\u5206\u79bb\u7ebf\u7a0b\u7ec8\u6b62\u540e&#xff0c;\u5176\u8d44\u6e90\u4e0d\u4f1a\u88ab\u81ea\u52a8\u56de\u6536&#xff0c;\u6210\u4e3a\u201c\u50f5\u5c38\u7ebf\u7a0b\u201d&#xff0c;\u9020\u6210\u8d44\u6e90\u6cc4\u6f0f\u3002<\/li>\n<\/ul>\n<h4>2.2 \u7ebf\u7a0b\u5206\u79bb (pthread_detach)<\/h4>\n<p>\u6709\u65f6&#xff0c;\u7236\u7ebf\u7a0b\u5e76\u4e0d\u5173\u5fc3\u5b50\u7ebf\u7a0b\u4f55\u65f6\u7ed3\u675f\u6216\u5176\u9000\u51fa\u72b6\u6001&#xff0c;\u4e5f\u4e0d\u60f3\u53bb\u663e\u5f0f join \u5b83\u3002\u8fd9\u65f6\u53ef\u4ee5\u5c06\u7ebf\u7a0b\u6807\u8bb0\u4e3a\u201c\u5206\u79bb\u72b6\u6001\u201d\u3002<\/p>\n<p>#include &lt;pthread.h&gt;<br \/>\nint pthread_detach(pthread_t thread);<\/p>\n<ul>\n<li>thread: \u8981\u5206\u79bb\u7684\u7ebf\u7a0b ID\u3002<\/li>\n<li>\u8fd4\u56de\u503c&#xff1a;\u6210\u529f\u8fd4\u56de 0&#xff1b;\u9519\u8bef\u8fd4\u56de\u9519\u8bef\u7801\u3002<\/li>\n<\/ul>\n<p>\u4f5c\u7528&#xff1a;<\/p>\n<li>\u5c06\u7ebf\u7a0b thread \u6807\u8bb0\u4e3a\u5206\u79bb\u72b6\u6001\u3002<\/li>\n<li>\u5206\u79bb\u72b6\u6001\u7684\u7ebf\u7a0b\u7ec8\u6b62\u65f6&#xff0c;\u5176\u8d44\u6e90\u4f1a\u81ea\u52a8\u88ab\u7cfb\u7edf\u56de\u6536&#xff0c;\u65e0\u9700\u5176\u4ed6\u7ebf\u7a0b\u8c03\u7528 pthread_join\u3002<\/li>\n<li>\u4e0d\u80fd\u5bf9\u5206\u79bb\u72b6\u6001\u7684\u7ebf\u7a0b\u8c03\u7528 pthread_join\u3002<\/li>\n<p>\u4f55\u65f6\u4f7f\u7528&#xff1a;<\/p>\n<ul>\n<li>\u5f53\u4e0d\u5173\u5fc3\u7ebf\u7a0b\u7684\u8fd4\u56de\u7ed3\u679c\u65f6\u3002<\/li>\n<li>\u5f53\u7ebf\u7a0b\u662f\u201c\u4e00\u6b21\u6027\u201d\u7684\u540e\u53f0\u4efb\u52a1\u65f6\u3002<\/li>\n<li>\u9632\u6b62\u5fd8\u8bb0 join \u5bfc\u81f4\u8d44\u6e90\u6cc4\u6f0f\u3002<\/li>\n<\/ul>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>void* worker_thread(void* arg) {<br \/>\n    \/\/ &#8230; \u540e\u53f0\u5de5\u4f5c&#xff0c;\u4e0d\u9700\u8981\u8fd4\u56de\u7ed3\u679c &#8230;<br \/>\n    return NULL;<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    pthread_t tid;<br \/>\n    pthread_create(&amp;tid, NULL, worker_thread, NULL);<br \/>\n    pthread_detach(tid); \/\/ \u7acb\u5373\u5206\u79bb&#xff0c;\u4e0d\u5173\u5fc3\u5176\u7ed3\u675f<br \/>\n    \/\/ &#8230; \u4e3b\u7ebf\u7a0b\u7ee7\u7eed\u8fd0\u884c&#xff0c;\u65e0\u9700\u7b49\u5f85 tid &#8230;<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkzMDExLzY5NTk0MDczOC5zaHRtbA&#061;&#061;.html https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkzMDExLzY5NTk0MDkyMy5zaHRtbA&#061;&#061;.html https:\/\/tv.sohu.com\/v\/dXMvNDQyMDkzMDExLzY5NTk0MDc1NC5zaHRtbA&#061;&#061;.html https:\/\/tv.sohu.com\/v\/dXMvNDM4OTIwMjE2LzY5NTk0MDk3Ny5zaHRtbA&#061;&#061;.html https:\/\/tv.sohu.com\/v\/dXMvNDM4OTIwMjE2LzY5NTk0MTIxNC5zaHRtbA&#061;&#061;.html https:\/\/tv.sohu.com\/v\/dXMvNDM4OTIwMjE2LzY5NTk0MTA3NC5zaHRtbA&#061;&#061;.html https:\/\/tv.sohu.com\/v\/dXMvNDM4OTIwMjE2LzY5NTk0MTIyMi5zaHRtbA&#061;&#061;.html https:\/\/tv.sohu.com\/v\/dXMvNDM4OTIwMjE2LzY5NTk0MTI1Ny5zaHRtbA&#061;&#061;.html https:\/\/tv.sohu.com\/v\/dXMvNDM4OTIwMjE2LzY5NTk0MTI2MS5zaHRtbA&#061;&#061;.html https:\/\/tv.sohu.com\/v\/dXMvNDM4OTIwMjE2LzY5NTk0MTEzNy5zaHRtbA&#061;&#061;.html https:\/\/tv.sohu.com\/v\/dXMvNDM4OTIwMjE2LzY5NTk0MTMyNi5zaHRtbA&#061;&#061;.html https:\/\/tv.sohu.com\/v\/dXMvNDM4OTIwMjE2LzY5NTk0MTQzMi5zaHRtbA&#061;&#061;.html https:\/\/tv.sohu.com\/v\/dXMvNDM4OTIwMjE2LzY5NTk0MTE0NS5zaHRtbA&#061;&#061;.html \u00a0<\/p>\n<p>\u5173\u952e\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u53ef\u4ee5\u5728\u7ebf\u7a0b\u521b\u5efa\u540e\u7acb\u5373\u5206\u79bb&#xff08;\u5982\u793a\u4f8b&#xff09;\u3002<\/li>\n<li>\u7ebf\u7a0b\u4e5f\u53ef\u4ee5\u5728\u7ebf\u7a0b\u51fd\u6570\u5185\u90e8\u8c03\u7528 pthread_detach(pthread_self()) \u6765\u81ea\u6211\u5206\u79bb\u3002<\/li>\n<li>\u5c1d\u8bd5\u5206\u79bb\u4e00\u4e2a\u5df2\u7ecf\u7ec8\u6b62\u7684\u7ebf\u7a0b\u6216\u672a\u521b\u5efa\u7684\u7ebf\u7a0b\u4f1a\u51fa\u9519\u3002<\/li>\n<li>\u5206\u79bb\u64cd\u4f5c\u53ea\u80fd\u8fdb\u884c\u4e00\u6b21\u3002<\/li>\n<\/ul>\n<p>join vs detach \u603b\u7ed3&#xff1a;<\/p>\n<table>\n<tr>\u7279\u6027pthread_joinpthread_detach<\/tr>\n<tbody>\n<tr>\n<td style=\"text-align:left\">\u76ee\u7684<\/td>\n<td style=\"text-align:left\">\u7b49\u5f85\u7ed3\u675f\u3001\u83b7\u53d6\u72b6\u6001\u3001\u56de\u6536\u8d44\u6e90<\/td>\n<td style=\"text-align:left\">\u81ea\u52a8\u56de\u6536\u8d44\u6e90&#xff0c;\u4e0d\u83b7\u53d6\u72b6\u6001<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:left\">\u963b\u585e<\/td>\n<td style=\"text-align:left\">\u963b\u585e\u8c03\u7528\u7ebf\u7a0b\u76f4\u5230\u76ee\u6807\u7ebf\u7a0b\u7ed3\u675f<\/td>\n<td style=\"text-align:left\">\u975e\u963b\u585e&#xff0c;\u7acb\u5373\u8fd4\u56de<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:left\">\u8d44\u6e90\u56de\u6536<\/td>\n<td style=\"text-align:left\">\u7531 join \u8c03\u7528\u8005\u8d1f\u8d23\u56de\u6536<\/td>\n<td style=\"text-align:left\">\u7531\u7cfb\u7edf\u5728\u7ebf\u7a0b\u7ed3\u675f\u65f6\u81ea\u52a8\u56de\u6536<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:left\">\u72b6\u6001\u83b7\u53d6<\/td>\n<td style=\"text-align:left\">\u53ef\u4ee5\u83b7\u53d6\u9000\u51fa\u72b6\u6001<\/td>\n<td style=\"text-align:left\">\u65e0\u6cd5\u83b7\u53d6\u9000\u51fa\u72b6\u6001<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:left\">\u8c03\u7528\u6b21\u6570<\/td>\n<td style=\"text-align:left\">\u53ea\u80fd join \u4e00\u6b21<\/td>\n<td style=\"text-align:left\">\u53ea\u80fd detach \u4e00\u6b21<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>3. \u7ebf\u7a0b\u540c\u6b65\u673a\u5236 (Synchronization)<\/h3>\n<p>\u591a\u4e2a\u7ebf\u7a0b\u5e76\u53d1\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\u65f6&#xff0c;\u9700\u540c\u6b65\u4ee5\u907f\u514d\u6570\u636e\u7ade\u4e89 (Data Race) \u548c\u7ade\u6001\u6761\u4ef6 (Race Condition)\u3002\u4e3b\u8981\u673a\u5236&#xff1a;<\/p>\n<h4>3.1 \u4e92\u65a5\u9501 (Mutexes, pthread_mutex_t)<\/h4>\n<p>\u63d0\u4f9b\u5bf9\u5171\u4eab\u8d44\u6e90\u7684\u72ec\u5360\u8bbf\u95ee\u3002\u7ebf\u7a0b\u8bbf\u95ee\u4e34\u754c\u533a\u524d\u52a0\u9501&#xff0c;\u8bbf\u95ee\u540e\u89e3\u9501\u3002<\/p>\n<p>#include &lt;pthread.h&gt;<br \/>\npthread_mutex_t mutex &#061; PTHREAD_MUTEX_INITIALIZER; \/\/ \u9759\u6001\u521d\u59cb\u5316<\/p>\n<p>\/\/ \u52a8\u6001\u521d\u59cb\u5316<br \/>\nint pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr);<br \/>\nint pthread_mutex_destroy(pthread_mutex_t *mutex);<\/p>\n<p>int pthread_mutex_lock(pthread_mutex_t *mutex);   \/\/ \u963b\u585e\u52a0\u9501<br \/>\nint pthread_mutex_trylock(pthread_mutex_t *mutex); \/\/ \u975e\u963b\u585e\u52a0\u9501<br \/>\nint pthread_mutex_unlock(pthread_mutex_t *mutex); \/\/ \u89e3\u9501<\/p>\n<p>\u793a\u4f8b (\u94f6\u884c\u8d26\u6237\u5b58\u53d6\u6b3e)&#xff1a;<\/p>\n<p>struct account {<br \/>\n    pthread_mutex_t lock;<br \/>\n    int balance;<br \/>\n};<\/p>\n<p>void* deposit(void* arg) {<br \/>\n    struct account *acc &#061; (struct account*)arg;<br \/>\n    for (int i &#061; 0; i &lt; 10000; &#043;&#043;i) {<br \/>\n        pthread_mutex_lock(&amp;acc-&gt;lock);<br \/>\n        acc-&gt;balance &#043;&#061; 1;<br \/>\n        pthread_mutex_unlock(&amp;acc-&gt;lock);<br \/>\n    }<br \/>\n    return NULL;<br \/>\n}<br \/>\n\/\/ &#8230; \u7c7b\u4f3c\u5730\u5b9e\u73b0 withdraw &#8230;<\/p>\n<h4>3.2 \u6761\u4ef6\u53d8\u91cf (Condition Variables, pthread_cond_t)<\/h4>\n<p>\u7528\u4e8e\u7ebf\u7a0b\u95f4\u7b49\u5f85\u67d0\u4e2a\u6761\u4ef6\u6210\u7acb\u3002\u5fc5\u987b\u4e0e\u4e92\u65a5\u9501\u914d\u5408\u4f7f\u7528\u3002<\/p>\n<p>#include &lt;pthread.h&gt;<br \/>\npthread_cond_t cond &#061; PTHREAD_COND_INITIALIZER;<\/p>\n<p>int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr);<br \/>\nint pthread_cond_destroy(pthread_cond_t *cond);<\/p>\n<p>int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex); \/\/ \u7b49\u5f85\u6761\u4ef6&#xff0c;\u91ca\u653e\u9501&#xff0c;\u88ab\u5524\u9192\u540e\u91cd\u65b0\u83b7\u5f97\u9501<br \/>\nint pthread_cond_signal(pthread_cond_t *cond); \/\/ \u5524\u9192\u81f3\u5c11\u4e00\u4e2a\u7b49\u5f85\u8be5\u6761\u4ef6\u7684\u7ebf\u7a0b<br \/>\nint pthread_cond_broadcast(pthread_cond_t *cond); \/\/ \u5524\u9192\u6240\u6709\u7b49\u5f85\u8be5\u6761\u4ef6\u7684\u7ebf\u7a0b<\/p>\n<p>\u4f7f\u7528\u8303\u5f0f (\u751f\u4ea7\u8005-\u6d88\u8d39\u8005)&#xff1a;<\/p>\n<p>pthread_mutex_t mutex &#061; PTHREAD_MUTEX_INITIALIZER;<br \/>\npthread_cond_t cond &#061; PTHREAD_COND_INITIALIZER;<br \/>\nqueue_t queue; \/\/ \u5171\u4eab\u961f\u5217<\/p>\n<p>\/\/ \u751f\u4ea7\u8005\u7ebf\u7a0b<br \/>\nvoid* producer(void* arg) {<br \/>\n    while (\/* \u6709\u6570\u636e\u751f\u4ea7 *\/) {<br \/>\n        data_t data &#061; produce_data();<br \/>\n        pthread_mutex_lock(&amp;mutex);<br \/>\n        enqueue(&amp;queue, data); \/\/ \u5c06\u6570\u636e\u653e\u5165\u961f\u5217<br \/>\n        pthread_cond_signal(&amp;cond); \/\/ \u901a\u77e5\u53ef\u80fd\u6709\u6d88\u8d39\u8005\u5728\u7b49\u5f85<br \/>\n        pthread_mutex_unlock(&amp;mutex);<br \/>\n    }<br \/>\n    return NULL;<br \/>\n}<\/p>\n<p>\/\/ \u6d88\u8d39\u8005\u7ebf\u7a0b<br \/>\nvoid* consumer(void* arg) {<br \/>\n    while (\/* \u9700\u8981\u6d88\u8d39 *\/) {<br \/>\n        pthread_mutex_lock(&amp;mutex);<br \/>\n        while (queue_empty(&amp;queue)) { \/\/ \u5fc5\u987b\u7528 while \u5faa\u73af\u68c0\u67e5&#xff0c;\u907f\u514d\u865a\u5047\u5524\u9192<br \/>\n            pthread_cond_wait(&amp;cond, &amp;mutex); \/\/ \u7b49\u5f85\u6761\u4ef6&#xff0c;\u91ca\u653e\u9501<br \/>\n        }<br \/>\n        data_t data &#061; dequeue(&amp;queue); \/\/ \u961f\u5217\u4e0d\u7a7a&#xff0c;\u53d6\u51fa\u6570\u636e<br \/>\n        pthread_mutex_unlock(&amp;mutex);<br \/>\n        consume_data(data);<br \/>\n    }<br \/>\n    return NULL;<br \/>\n}<\/p>\n<p>\u5173\u952e\u70b9&#xff1a;<\/p>\n<ul>\n<li>pthread_cond_wait \u4f1a\u91ca\u653e\u5173\u8054\u7684\u4e92\u65a5\u9501&#xff0c;\u5e76\u963b\u585e\u7ebf\u7a0b\u3002<\/li>\n<li>\u88ab\u5524\u9192\u65f6&#xff08;\u901a\u8fc7 signal \u6216 broadcast&#xff09;&#xff0c;\u5b83\u4f1a\u5728\u8fd4\u56de\u524d\u91cd\u65b0\u83b7\u5f97\u4e92\u65a5\u9501\u3002<\/li>\n<li>\u5524\u9192\u540e&#xff0c;\u6761\u4ef6\u53ef\u80fd\u4e0d\u518d\u6210\u7acb&#xff08;\u5176\u4ed6\u7ebf\u7a0b\u62a2\u5148\u6d88\u8d39\u4e86&#xff09;&#xff0c;\u56e0\u6b64\u5fc5\u987b\u5728\u5faa\u73af\u4e2d\u68c0\u67e5\u6761\u4ef6 (while (condition))\u3002<\/li>\n<\/ul>\n<h4>3.3 \u5176\u4ed6\u540c\u6b65\u673a\u5236<\/h4>\n<ul>\n<li>\u8bfb\u5199\u9501 (pthread_rwlock_t): \u5141\u8bb8\u591a\u4e2a\u8bfb\u7ebf\u7a0b\u5e76\u53d1&#xff0c;\u4f46\u5199\u7ebf\u7a0b\u72ec\u5360\u3002<\/li>\n<li>\u4fe1\u53f7\u91cf (sem_t): \u66f4\u901a\u7528\u7684\u8ba1\u6570\u5668&#xff0c;\u53ef\u7528\u4e8e\u63a7\u5236\u5e76\u53d1\u8bbf\u95ee\u6570\u91cf\u3002<\/li>\n<li>\u5c4f\u969c (pthread_barrier_t): \u8ba9\u4e00\u7ec4\u7ebf\u7a0b\u5728\u4ee3\u7801\u4e2d\u7684\u67d0\u4e00\u70b9\u7b49\u5f85&#xff0c;\u76f4\u5230\u6240\u6709\u6210\u5458\u90fd\u5230\u8fbe\u3002<\/li>\n<\/ul>\n<h3>4. C&#043;&#043; \u591a\u7ebf\u7a0b\u5b9e\u6218 (std::thread)<\/h3>\n<p>C&#043;&#043;11 \u5f15\u5165\u4e86 &lt;thread&gt; \u5934\u6587\u4ef6&#xff0c;\u63d0\u4f9b\u4e86\u66f4\u73b0\u4ee3\u3001\u9762\u5411\u5bf9\u8c61\u7684\u7ebf\u7a0b\u63a5\u53e3 std::thread&#xff0c;\u901a\u5e38\u6bd4\u76f4\u63a5\u4f7f\u7528 pthread API \u66f4\u7b80\u6d01\u5b89\u5168\u3002<\/p>\n<h4>4.1 \u521b\u5efa\u7ebf\u7a0b<\/h4>\n<p>#include &lt;thread&gt;<br \/>\n#include &lt;iostream&gt;<\/p>\n<p>void hello() {<br \/>\n    std::cout &lt;&lt; &#034;Hello Concurrent World from thread!&#034; &lt;&lt; std::endl;<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    std::thread t(hello); \/\/ \u521b\u5efa\u5e76\u542f\u52a8\u7ebf\u7a0b&#xff0c;\u6267\u884c hello \u51fd\u6570<br \/>\n    t.join(); \/\/ \u7b49\u5f85\u7ebf\u7a0b\u7ed3\u675f<br \/>\n    \/\/ t.detach(); \/\/ \u6216\u8005\u5206\u79bb\u5b83<br \/>\n    return 0;<br \/>\n}<\/p>\n<ul>\n<li>\u7ebf\u7a0b\u51fd\u6570\u53ef\u4ee5\u662f\u4efb\u4f55\u53ef\u8c03\u7528\u5bf9\u8c61&#xff08;\u51fd\u6570\u3001\u51fd\u6570\u5bf9\u8c61\u3001Lambda \u8868\u8fbe\u5f0f&#xff09;\u3002<\/li>\n<li>\u6ce8\u610f&#xff1a;\u5982\u679c\u7ebf\u7a0b\u51fd\u6570\u629b\u51fa\u5f02\u5e38\u4e14\u672a\u88ab\u6355\u83b7&#xff0c;\u7a0b\u5e8f\u5c06\u8c03\u7528 std::terminate\u3002<\/li>\n<\/ul>\n<h4>4.2 \u4f20\u9012\u53c2\u6570<\/h4>\n<p>\u53c2\u6570\u6309\u503c\u590d\u5236\u6216\u79fb\u52a8\u4f20\u9012\u5230\u7ebf\u7a0b\u5185\u90e8\u5b58\u50a8\u3002\u82e5\u9700\u4f20\u9012\u5f15\u7528&#xff0c;\u9700\u4f7f\u7528 std::ref \u6216 std::cref \u5305\u88c5\u3002<\/p>\n<p>void modify(int &amp;x) {<br \/>\n    x &#061; 42;<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    int value &#061; 10;<br \/>\n    std::thread t(modify, std::ref(value)); \/\/ \u4f20\u9012\u5f15\u7528<br \/>\n    t.join();<br \/>\n    std::cout &lt;&lt; value &lt;&lt; std::endl; \/\/ \u8f93\u51fa 42<br \/>\n    return 0;<br \/>\n}<\/p>\n<h4>4.3 \u7b49\u5f85\u4e0e\u5206\u79bb (join, detach)<\/h4>\n<p>\u4e0e pthread \u6982\u5ff5\u4e00\u81f4&#xff1a;<\/p>\n<p>std::thread t(background_task);<\/p>\n<p>if (t.joinable()) { \/\/ \u68c0\u67e5\u662f\u5426\u53ef\u4ee5 join \u6216 detach<br \/>\n    t.join();   \/\/ \u7b49\u5f85 t \u7ed3\u675f<br \/>\n    \/\/ \u6216 t.detach(); \/\/ \u5206\u79bb t<br \/>\n}<\/p>\n<ul>\n<li>\u5fc5\u987b\u5728 std::thread \u5bf9\u8c61\u9500\u6bc1\u524d\u51b3\u5b9a\u662f join \u8fd8\u662f detach&#xff08;\u6216\u5df2 join\/detach&#xff09;&#xff0c;\u5426\u5219\u7a0b\u5e8f\u7ec8\u6b62 (std::terminate)\u3002<\/li>\n<\/ul>\n<h4>4.4 C&#043;&#043; \u540c\u6b65\u673a\u5236 (&lt;mutex&gt;, &lt;condition_variable&gt;)<\/h4>\n<p>C&#043;&#043;11 \u4e5f\u63d0\u4f9b\u4e86\u6807\u51c6\u5e93\u7684\u4e92\u65a5\u9501\u548c\u6761\u4ef6\u53d8\u91cf\u3002<\/p>\n<h5>4.4.1 \u4e92\u65a5\u9501<\/h5>\n<p>#include &lt;mutex&gt;<br \/>\nstd::mutex mtx;<\/p>\n<p>void safe_increment(int &amp;counter) {<br \/>\n    std::lock_guard&lt;std::mutex&gt; lock(mtx); \/\/ RAII: \u6784\u9020\u65f6\u52a0\u9501&#xff0c;\u6790\u6784\u65f6\u89e3\u9501<br \/>\n    &#043;&#043;counter;<br \/>\n    \/\/ \u4e5f\u53ef\u4f7f\u7528 std::unique_lock (\u66f4\u7075\u6d3b&#xff0c;\u53ef\u5ef6\u8fdf\u52a0\u9501\u3001\u63d0\u524d\u89e3\u9501&#xff0c;\u7528\u4e8e\u6761\u4ef6\u53d8\u91cf)<br \/>\n}<\/p>\n<ul>\n<li>std::lock_guard: \u4f5c\u7528\u57df\u9501&#xff0c;\u6784\u9020\u65f6\u9501\u5b9a&#xff0c;\u6790\u6784\u65f6\u89e3\u9501\u3002<\/li>\n<li>std::unique_lock: \u66f4\u7075\u6d3b&#xff0c;\u652f\u6301\u5ef6\u8fdf\u9501\u5b9a\u3001\u624b\u52a8\u9501\u5b9a\u89e3\u9501\u3001\u8f6c\u79fb\u6240\u6709\u6743\u7b49\u3002<\/li>\n<\/ul>\n<h5>4.4.2 \u6761\u4ef6\u53d8\u91cf<\/h5>\n<p>#include &lt;mutex&gt;<br \/>\n#include &lt;condition_variable&gt;<br \/>\n#include &lt;queue&gt;<\/p>\n<p>std::mutex mtx;<br \/>\nstd::condition_variable cv;<br \/>\nstd::queue&lt;data_t&gt; data_queue;<\/p>\n<p>\/\/ \u751f\u4ea7\u8005<br \/>\nvoid producer() {<br \/>\n    while (\/* &#8230; *\/) {<br \/>\n        data_t data &#061; produce_data();<br \/>\n        {<br \/>\n            std::lock_guard&lt;std::mutex&gt; lk(mtx);<br \/>\n            data_queue.push(data);<br \/>\n        }<br \/>\n        cv.notify_one(); \/\/ \u901a\u77e5\u4e00\u4e2a\u6d88\u8d39\u8005<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ \u6d88\u8d39\u8005<br \/>\nvoid consumer() {<br \/>\n    while (\/* &#8230; *\/) {<br \/>\n        std::unique_lock&lt;std::mutex&gt; lk(mtx); \/\/ \u5fc5\u987b\u7528 unique_lock<br \/>\n        cv.wait(lk, []{ return !data_queue.empty(); }); \/\/ \u7b49\u5f85\u6761\u4ef6\u6210\u7acb&#xff0c;\u4f7f\u7528 Lambda \u8c13\u8bcd\u68c0\u67e5<br \/>\n        data_t data &#061; data_queue.front();<br \/>\n        data_queue.pop();<br \/>\n        lk.unlock(); \/\/ \u53ef\u4ee5\u63d0\u524d\u89e3\u9501&#xff0c;\u5904\u7406\u6570\u636e\u65f6\u4e0d\u9700\u9501<br \/>\n        consume_data(data);<br \/>\n    }<br \/>\n}<\/p>\n<ul>\n<li>\u4f7f\u7528 std::unique_lock \u914d\u5408\u6761\u4ef6\u53d8\u91cf&#xff0c;\u56e0\u4e3a wait \u65b9\u6cd5\u9700\u8981\u80fd\u89e3\u9501\u548c\u91cd\u65b0\u52a0\u9501\u3002<\/li>\n<li>cv.wait(lk, predicate) \u7b49\u4ef7\u4e8e&#xff1a; while (!predicate()) {<br \/>\n    cv.wait(lk);<br \/>\n}<\/p>\n<\/li>\n<\/ul>\n<h5>4.4.3 std::future \u548c std::async<\/h5>\n<p>\u7528\u4e8e\u5f02\u6b65\u6267\u884c\u4efb\u52a1\u5e76\u83b7\u53d6\u7ed3\u679c\u3002<\/p>\n<p>#include &lt;future&gt;<br \/>\n#include &lt;iostream&gt;<\/p>\n<p>int compute_value() {<br \/>\n    \/\/ &#8230; \u8017\u65f6\u8ba1\u7b97 &#8230;<br \/>\n    return 42;<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    std::future&lt;int&gt; fut &#061; std::async(std::launch::async, compute_value); \/\/ \u5f02\u6b65\u6267\u884c<br \/>\n    \/\/ &#8230; \u505a\u5176\u4ed6\u4e8b\u60c5 &#8230;<br \/>\n    int result &#061; fut.get(); \/\/ \u83b7\u53d6\u7ed3\u679c&#xff08;\u5982\u679c\u9700\u8981&#xff0c;\u4f1a\u7b49\u5f85\u8ba1\u7b97\u5b8c\u6210&#xff09;<br \/>\n    std::cout &lt;&lt; &#034;Result: &#034; &lt;&lt; result &lt;&lt; std::endl;<br \/>\n    return 0;<br \/>\n}<\/p>\n<ul>\n<li>std::async \u53ef\u4ee5\u6307\u5b9a\u7b56\u7565 (std::launch::async \u7acb\u5373\u5f02\u6b65\u6267\u884c, std::launch::deferred \u5ef6\u8fdf\u6267\u884c\u76f4\u5230\u8c03\u7528 get\/wait)\u3002<\/li>\n<li>fut.get() \u963b\u585e\u76f4\u5230\u7ed3\u679c\u53ef\u7528&#xff0c;\u5e76\u53ea\u80fd\u8c03\u7528\u4e00\u6b21\u3002fut.wait() \u53ea\u7b49\u5f85\u5b8c\u6210\u4e0d\u53d6\u7ed3\u679c\u3002<\/li>\n<\/ul>\n<h3>5. \u7ebf\u7a0b\u6c60 (Thread Pool)<\/h3>\n<p>\u9891\u7e41\u521b\u5efa\u9500\u6bc1\u7ebf\u7a0b\u5f00\u9500\u5927\u3002\u7ebf\u7a0b\u6c60\u9884\u5148\u521b\u5efa\u4e00\u7ec4\u7ebf\u7a0b&#xff0c;\u7b49\u5f85\u4efb\u52a1\u5230\u6765\u3002\u4efb\u52a1\u88ab\u653e\u5165\u961f\u5217&#xff0c;\u7a7a\u95f2\u7ebf\u7a0b\u4ece\u961f\u5217\u4e2d\u53d6\u51fa\u4efb\u52a1\u6267\u884c\u3002<\/p>\n<p>\u6838\u5fc3\u7ec4\u4ef6&#xff1a;<\/p>\n<li>\u7ebf\u7a0b\u961f\u5217&#xff1a;\u4e00\u7ec4\u5de5\u4f5c\u7ebf\u7a0b\u3002<\/li>\n<li>\u4efb\u52a1\u961f\u5217&#xff1a;\u5b58\u653e\u5f85\u6267\u884c\u7684\u4efb\u52a1&#xff08;\u901a\u5e38\u662f\u51fd\u6570\u5bf9\u8c61&#xff09;\u3002<\/li>\n<li>\u540c\u6b65\u673a\u5236&#xff1a;\u4e92\u65a5\u9501\u4fdd\u62a4\u4efb\u52a1\u961f\u5217&#xff0c;\u6761\u4ef6\u53d8\u91cf\u901a\u77e5\u7ebf\u7a0b\u6709\u65b0\u4efb\u52a1\u3002<\/li>\n<p>\u7b80\u5316\u7248 C&#043;&#043;11 \u7ebf\u7a0b\u6c60\u793a\u4f8b&#xff1a;<\/p>\n<p>#include &lt;thread&gt;<br \/>\n#include &lt;mutex&gt;<br \/>\n#include &lt;condition_variable&gt;<br \/>\n#include &lt;queue&gt;<br \/>\n#include &lt;functional&gt;<br \/>\n#include &lt;vector&gt;<br \/>\n#include &lt;stdexcept&gt;<\/p>\n<p>class ThreadPool {<br \/>\npublic:<br \/>\n    ThreadPool(size_t threads);<br \/>\n    ~ThreadPool();<\/p>\n<p>    template&lt;class F, class&#8230; Args&gt;<br \/>\n    auto enqueue(F&amp;&amp; f, Args&amp;&amp;&#8230; args)<br \/>\n        -&gt; std::future&lt;typename std::result_of&lt;F(Args&#8230;)&gt;::type&gt;;<\/p>\n<p>private:<br \/>\n    std::vector&lt;std::thread&gt; workers;<br \/>\n    std::queue&lt;std::function&lt;void()&gt;&gt; tasks;<br \/>\n    std::mutex queue_mutex;<br \/>\n    std::condition_variable condition;<br \/>\n    bool stop;<br \/>\n};<\/p>\n<p>\/\/ \u6784\u9020\u51fd\u6570&#xff1a;\u521b\u5efa threads \u4e2a\u5de5\u4f5c\u7ebf\u7a0b<br \/>\nThreadPool::ThreadPool(size_t threads) : stop(false) {<br \/>\n    for (size_t i &#061; 0; i &lt; threads; &#043;&#043;i) {<br \/>\n        workers.emplace_back([this] {<br \/>\n            for (;;) {<br \/>\n                std::function&lt;void()&gt; task;<br \/>\n                {<br \/>\n                    std::unique_lock&lt;std::mutex&gt; lock(this-&gt;queue_mutex);<br \/>\n                    this-&gt;condition.wait(lock,<br \/>\n                        [this]{ return this-&gt;stop || !this-&gt;tasks.empty(); });<br \/>\n                    if (this-&gt;stop &amp;&amp; this-&gt;tasks.empty())<br \/>\n                        return;<br \/>\n                    task &#061; std::move(this-&gt;tasks.front());<br \/>\n                    this-&gt;tasks.pop();<br \/>\n                }<br \/>\n                task(); \/\/ \u6267\u884c\u4efb\u52a1<br \/>\n            }<br \/>\n        });<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ \u6790\u6784\u51fd\u6570&#xff1a;\u505c\u6b62\u6240\u6709\u7ebf\u7a0b<br \/>\nThreadPool::~ThreadPool() {<br \/>\n    {<br \/>\n        std::unique_lock&lt;std::mutex&gt; lock(queue_mutex);<br \/>\n        stop &#061; true;<br \/>\n    }<br \/>\n    condition.notify_all(); \/\/ \u5524\u9192\u6240\u6709\u7ebf\u7a0b<br \/>\n    for (std::thread &amp;worker : workers) {<br \/>\n        worker.join(); \/\/ \u7b49\u5f85\u6240\u6709\u7ebf\u7a0b\u7ed3\u675f<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ \u6dfb\u52a0\u4efb\u52a1\u5230\u961f\u5217<br \/>\ntemplate&lt;class F, class&#8230; Args&gt;<br \/>\nauto ThreadPool::enqueue(F&amp;&amp; f, Args&amp;&amp;&#8230; args)<br \/>\n    -&gt; std::future&lt;typename std::result_of&lt;F(Args&#8230;)&gt;::type&gt; {<br \/>\n    using return_type &#061; typename std::result_of&lt;F(Args&#8230;)&gt;::type;<\/p>\n<p>    auto task &#061; std::make_shared&lt;std::packaged_task&lt;return_type()&gt;&gt;(<br \/>\n        std::bind(std::forward&lt;F&gt;(f), std::forward&lt;Args&gt;(args)&#8230;)<br \/>\n    );<\/p>\n<p>    std::future&lt;return_type&gt; res &#061; task-&gt;get_future();<br \/>\n    {<br \/>\n        std::unique_lock&lt;std::mutex&gt; lock(queue_mutex);<br \/>\n        if (stop)<br \/>\n            throw std::runtime_error(&#034;enqueue on stopped ThreadPool&#034;);<br \/>\n        tasks.emplace([task](){ (*task)(); }); \/\/ \u5c06\u53ef\u8c03\u7528\u5bf9\u8c61\u653e\u5165\u4efb\u52a1\u961f\u5217<br \/>\n    }<br \/>\n    condition.notify_one(); \/\/ \u901a\u77e5\u4e00\u4e2a\u5de5\u4f5c\u7ebf\u7a0b<br \/>\n    return res;<br \/>\n}<\/p>\n<p>\u4f7f\u7528&#xff1a;<\/p>\n<p>ThreadPool pool(4); \/\/ 4\u4e2a\u5de5\u4f5c\u7ebf\u7a0b<\/p>\n<p>auto result &#061; pool.enqueue([](int answer) {<br \/>\n    std::this_thread::sleep_for(std::chrono::seconds(1));<br \/>\n    return answer;<br \/>\n}, 42);<\/p>\n<p>\/\/ &#8230; \u505a\u5176\u4ed6\u4e8b &#8230;<br \/>\nstd::cout &lt;&lt; &#034;Result: &#034; &lt;&lt; result.get() &lt;&lt; std::endl; \/\/ \u7b49\u5f85\u5e76\u83b7\u53d6\u7ed3\u679c<\/p>\n<h3>6. \u7ebf\u7a0b\u5b89\u5168\u4e0e\u5e38\u89c1\u9677\u9631<\/h3>\n<ul>\n<li>\u6570\u636e\u7ade\u4e89 (Data Race)&#xff1a;\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bfb\u5199\u540c\u4e00\u5185\u5b58\u4f4d\u7f6e\u4e14\u65e0\u540c\u6b65\u3002\u4f7f\u7528\u4e92\u65a5\u9501\u6216\u539f\u5b50\u64cd\u4f5c\u89e3\u51b3\u3002<\/li>\n<li>\u6b7b\u9501 (Deadlock)&#xff1a;\u4e24\u4e2a\u6216\u591a\u4e2a\u7ebf\u7a0b\u76f8\u4e92\u7b49\u5f85\u5bf9\u65b9\u6301\u6709\u7684\u9501\u3002\u907f\u514d\u65b9\u6cd5&#xff1a;\n<ul>\n<li>\u9501\u987a\u5e8f&#xff1a;\u6240\u6709\u7ebf\u7a0b\u6309\u76f8\u540c\u987a\u5e8f\u83b7\u53d6\u9501\u3002<\/li>\n<li>\u907f\u514d\u5d4c\u5957\u9501&#xff1a;\u5c3d\u91cf\u907f\u514d\u5728\u4e00\u4e2a\u9501\u4fdd\u62a4\u533a\u5185\u83b7\u53d6\u53e6\u4e00\u4e2a\u9501\u3002<\/li>\n<li>\u4f7f\u7528 std::lock \u6216 std::scoped_lock (C&#043;&#043;17)&#xff1a;\u4e00\u6b21\u9501\u5b9a\u591a\u4e2a\u4e92\u65a5\u91cf&#xff0c;\u907f\u514d\u6b7b\u9501\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u865a\u5047\u5524\u9192 (Spurious Wakeup)&#xff1a;\u6761\u4ef6\u53d8\u91cf wait \u53ef\u80fd\u5728\u6ca1\u6709 signal\/broadcast \u7684\u60c5\u51b5\u4e0b\u8fd4\u56de\u3002\u56e0\u6b64\u5fc5\u987b\u5728\u5faa\u73af\u4e2d\u68c0\u67e5\u6761\u4ef6\u3002<\/li>\n<li>\u4f18\u5148\u7ea7\u53cd\u8f6c (Priority Inversion)&#xff1a;\u4f4e\u4f18\u5148\u7ea7\u7ebf\u7a0b\u6301\u6709\u9ad8\u4f18\u5148\u7ea7\u7ebf\u7a0b\u9700\u8981\u7684\u9501\u3002\u53ef\u901a\u8fc7\u4f18\u5148\u7ea7\u7ee7\u627f\u534f\u8bae\u89e3\u51b3 (\u5982 pthread_mutexattr_setprotocol)\u3002<\/li>\n<\/ul>\n<h3>7. \u8c03\u8bd5\u4e0e\u5206\u6790\u5de5\u5177<\/h3>\n<ul>\n<li>Valgrind (Helgrind, DRD)&#xff1a;\u68c0\u6d4b\u7ebf\u7a0b\u9519\u8bef&#xff08;\u6570\u636e\u7ade\u4e89\u3001\u6b7b\u9501&#xff09;\u3002<\/li>\n<li>GDB&#xff1a;\u8c03\u8bd5\u591a\u7ebf\u7a0b\u7a0b\u5e8f (info threads, thread &lt;id&gt;, break &lt;function&gt; thread &lt;id&gt;).<\/li>\n<li>ps -eLf \/ top -H&#xff1a;\u67e5\u770b\u8fdb\u7a0b\u7684\u7ebf\u7a0b\u4fe1\u606f\u3002<\/li>\n<li>perf&#xff1a;\u6027\u80fd\u5206\u6790&#xff0c;\u67e5\u770b\u7ebf\u7a0b\u8c03\u5ea6\u3001CPU \u4f7f\u7528\u3001Cache \u547d\u4e2d\u7387\u7b49\u3002<\/li>\n<\/ul>\n<h3>\u603b\u7ed3<\/h3>\n<p>\u7406\u89e3 Linux \u7ebf\u7a0b\u7684\u6838\u5fc3\u5728\u4e8e\u638c\u63e1\u5176\u751f\u547d\u5468\u671f\u7ba1\u7406&#xff08;\u521b\u5efa\u3001\u7b49\u5f85\u3001\u5206\u79bb&#xff09;\u548c\u540c\u6b65\u673a\u5236&#xff08;\u4e92\u65a5\u9501\u3001\u6761\u4ef6\u53d8\u91cf&#xff09;\u3002C&#043;&#043; \u7684 std::thread \u53ca\u76f8\u5173\u5e93\u63d0\u4f9b\u4e86\u66f4\u73b0\u4ee3\u4fbf\u6377\u7684\u63a5\u53e3\u3002\u7ebf\u7a0b\u6c60\u662f\u7ba1\u7406\u5e76\u53d1\u4efb\u52a1\u7684\u5e38\u7528\u6a21\u5f0f\u3002\u59cb\u7ec8\u8b66\u60d5\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898&#xff08;\u6570\u636e\u7ade\u4e89\u3001\u6b7b\u9501&#xff09;&#xff0c;\u5e76\u5584\u7528\u8c03\u8bd5\u5de5\u5177\u3002\u901a\u8fc7\u5408\u7406\u7684\u8bbe\u8ba1\u548c\u5b9e\u8df5&#xff0c;\u591a\u7ebf\u7a0b\u7f16\u7a0b\u80fd\u663e\u8457\u63d0\u5347\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u54cd\u5e94\u80fd\u529b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Linux \u7ebf\u7a0b\u6df1\u5ea6\u6307\u5357&#xff1a;\u4ece\u7b49\u5f85\u3001\u5206\u79bb\u5230 C \u591a\u7ebf\u7a0b\u5b9e\u6218<br \/>\n1. \u7ebf\u7a0b\u57fa\u7840\u6982\u5ff5\u56de\u987e<br \/>\n\u5728 Linux \u7cfb\u7edf\u4e2d&#xff0c;\u7ebf\u7a0b\u662f\u8f7b\u91cf\u7ea7\u7684\u6267\u884c\u5355\u5143&#xff0c;\u5171\u4eab\u8fdb\u7a0b\u7684\u5730\u5740\u7a7a\u95f4\u3001\u6587\u4ef6\u63cf\u8ff0\u7b26\u7b49\u8d44\u6e90&#xff0c;\u4f46\u62e5\u6709\u72ec\u7acb\u7684\u6808\u3001\u5bc4\u5b58\u5668\u72b6\u6001\u548c\u7a0b\u5e8f\u8ba1\u6570\u5668\u3002\u4f7f\u7528 POSIX \u7ebf\u7a0b\u5e93 (pthread) \u8fdb\u884c\u7ebf\u7a0b\u64cd\u4f5c\u3002<br \/>\n\u521b\u5efa\u7ebf\u7a0b&#xff1a;pthread_create\u83b7\u53d6\u7ebf\u7a0b ID&#xff1a;pthread_self\u7ec8\u6b62\u7ebf\u7a0b&#xff1a;pthread_e<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[99,190],"topic":[],"class_list":["post-64214","post","type-post","status-publish","format-standard","hentry","category-server","tag-java","tag-190"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Linux\u7ebf\u7a0b\u5b9e\u6218\uff1a\u4ece\u7b49\u5f85\u5230C++\u591a\u7ebf\u7a0b - \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\/64214.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Linux\u7ebf\u7a0b\u5b9e\u6218\uff1a\u4ece\u7b49\u5f85\u5230C++\u591a\u7ebf\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"Linux \u7ebf\u7a0b\u6df1\u5ea6\u6307\u5357&#xff1a;\u4ece\u7b49\u5f85\u3001\u5206\u79bb\u5230 C \u591a\u7ebf\u7a0b\u5b9e\u6218 1. \u7ebf\u7a0b\u57fa\u7840\u6982\u5ff5\u56de\u987e \u5728 Linux \u7cfb\u7edf\u4e2d&#xff0c;\u7ebf\u7a0b\u662f\u8f7b\u91cf\u7ea7\u7684\u6267\u884c\u5355\u5143&#xff0c;\u5171\u4eab\u8fdb\u7a0b\u7684\u5730\u5740\u7a7a\u95f4\u3001\u6587\u4ef6\u63cf\u8ff0\u7b26\u7b49\u8d44\u6e90&#xff0c;\u4f46\u62e5\u6709\u72ec\u7acb\u7684\u6808\u3001\u5bc4\u5b58\u5668\u72b6\u6001\u548c\u7a0b\u5e8f\u8ba1\u6570\u5668\u3002\u4f7f\u7528 POSIX \u7ebf\u7a0b\u5e93 (pthread) \u8fdb\u884c\u7ebf\u7a0b\u64cd\u4f5c\u3002 \u521b\u5efa\u7ebf\u7a0b&#xff1a;pthread_create\u83b7\u53d6\u7ebf\u7a0b ID&#xff1a;pthread_self\u7ec8\u6b62\u7ebf\u7a0b&#xff1a;pthread_e\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/64214.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-01-22T19:08:18+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/64214.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/64214.html\",\"name\":\"Linux\u7ebf\u7a0b\u5b9e\u6218\uff1a\u4ece\u7b49\u5f85\u5230C++\u591a\u7ebf\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-01-22T19:08:18+00:00\",\"dateModified\":\"2026-01-22T19:08:18+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/64214.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/64214.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/64214.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linux\u7ebf\u7a0b\u5b9e\u6218\uff1a\u4ece\u7b49\u5f85\u5230C++\u591a\u7ebf\u7a0b\"}]},{\"@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":"Linux\u7ebf\u7a0b\u5b9e\u6218\uff1a\u4ece\u7b49\u5f85\u5230C++\u591a\u7ebf\u7a0b - \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\/64214.html","og_locale":"zh_CN","og_type":"article","og_title":"Linux\u7ebf\u7a0b\u5b9e\u6218\uff1a\u4ece\u7b49\u5f85\u5230C++\u591a\u7ebf\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"Linux \u7ebf\u7a0b\u6df1\u5ea6\u6307\u5357&#xff1a;\u4ece\u7b49\u5f85\u3001\u5206\u79bb\u5230 C \u591a\u7ebf\u7a0b\u5b9e\u6218 1. \u7ebf\u7a0b\u57fa\u7840\u6982\u5ff5\u56de\u987e \u5728 Linux \u7cfb\u7edf\u4e2d&#xff0c;\u7ebf\u7a0b\u662f\u8f7b\u91cf\u7ea7\u7684\u6267\u884c\u5355\u5143&#xff0c;\u5171\u4eab\u8fdb\u7a0b\u7684\u5730\u5740\u7a7a\u95f4\u3001\u6587\u4ef6\u63cf\u8ff0\u7b26\u7b49\u8d44\u6e90&#xff0c;\u4f46\u62e5\u6709\u72ec\u7acb\u7684\u6808\u3001\u5bc4\u5b58\u5668\u72b6\u6001\u548c\u7a0b\u5e8f\u8ba1\u6570\u5668\u3002\u4f7f\u7528 POSIX \u7ebf\u7a0b\u5e93 (pthread) \u8fdb\u884c\u7ebf\u7a0b\u64cd\u4f5c\u3002 \u521b\u5efa\u7ebf\u7a0b&#xff1a;pthread_create\u83b7\u53d6\u7ebf\u7a0b ID&#xff1a;pthread_self\u7ec8\u6b62\u7ebf\u7a0b&#xff1a;pthread_e","og_url":"https:\/\/www.wsisp.com\/helps\/64214.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-01-22T19:08:18+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"8 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/64214.html","url":"https:\/\/www.wsisp.com\/helps\/64214.html","name":"Linux\u7ebf\u7a0b\u5b9e\u6218\uff1a\u4ece\u7b49\u5f85\u5230C++\u591a\u7ebf\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-01-22T19:08:18+00:00","dateModified":"2026-01-22T19:08:18+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/64214.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/64214.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/64214.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Linux\u7ebf\u7a0b\u5b9e\u6218\uff1a\u4ece\u7b49\u5f85\u5230C++\u591a\u7ebf\u7a0b"}]},{"@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\/64214","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=64214"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/64214\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=64214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=64214"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=64214"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=64214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}