{"id":116269,"date":"2026-10-21T02:24:55","date_gmt":"2026-10-20T18:24:55","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/116269.html"},"modified":"2026-10-21T02:24:55","modified_gmt":"2026-10-20T18:24:55","slug":"posix%e4%bf%a1%e5%8f%b7%e9%87%8f%e4%b8%8e%e7%8e%af%e5%bd%a2%e9%98%9f%e5%88%97","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/116269.html","title":{"rendered":"POSIX\u4fe1\u53f7\u91cf\u4e0e\u73af\u5f62\u961f\u5217"},"content":{"rendered":"<h3>\u4e00\u3001\u5148\u56de\u987e&#xff1a;\u4fe1\u53f7\u91cf\u5230\u5e95\u662f\u4e2a\u4ec0\u4e48\u4e1c\u897f<\/h3>\n<h4>1.1 \u4fe1\u53f7 \u2260 \u4fe1\u53f7\u91cf<\/h4>\n<p>\u4fe1\u53f7\u548c\u4fe1\u53f7\u91cf&#xff0c;\u662f\u300c\u8001\u5a46\u300d\u548c\u300c\u8001\u5a46\u997c\u300d\u7684\u5173\u7cfb \u2014\u2014 \u6ca1\u6709\u4efb\u4f55\u5173\u7cfb\u3002<\/p>\n<table>\n<tr>\u4fe1\u53f7&#xff08;signal&#xff09;\u4fe1\u53f7\u91cf&#xff08;semaphore \/ \u4fe1\u53f7\u706f&#xff09;<\/tr>\n<tbody>\n<tr>\n<td>\u672c\u8d28<\/td>\n<td>\u4e00\u79cd\u8fdb\u7a0b\u95f4\u5f02\u6b65\u901a\u77e5\u7684\u65b9\u5f0f<\/td>\n<td>\u4e00\u4e2a\u8ba1\u6570\u5668<\/td>\n<\/tr>\n<tr>\n<td>\u4f5c\u7528<\/td>\n<td>\u901a\u77e5\u4f60&#034;\u51fa\u4e8b\u4e86&#034;<\/td>\n<td>\u8868\u660e\u4e34\u754c\u8d44\u6e90\u4e2d\u8d44\u6e90\u6570\u91cf\u8fd8\u5269\u591a\u5c11<\/td>\n<\/tr>\n<tr>\n<td>\u6bd4\u55bb<\/td>\n<td>\u95e8\u94c3<\/td>\n<td>\u505c\u8f66\u573a\u7684\u4f59\u4f4d\u663e\u793a\u5c4f<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>1.2 POSIX \u4e0e System V<\/h4>\n<ul>\n<li>sem_init \/ sem_wait \/ sem_post \u8fd9\u4e00\u5957\u5c5e\u4e8e POSIX \u6807\u51c6&#xff08;\u53ef\u79fb\u690d\u64cd\u4f5c\u7cfb\u7edf\u63a5\u53e3&#xff09;<\/li>\n<li>\u4e4b\u524d\u8fdb\u7a0b\u95f4\u901a\u4fe1\u5b66\u7684 semget \/ semop \/ semctl \u5c5e\u4e8e System V \u6807\u51c6<\/li>\n<li>POSIX \u7cfb\u5217\u662f\u6700\u5e38\u7528\u7684&#xff0c;System V \u57fa\u672c\u5df2\u7ecf\u7b97\u88ab\u6dd8\u6c70\u4e86<\/li>\n<li>\u77e5\u9053\u5b83\u4fe9\u662f\u4e24\u5957\u6807\u51c6\u5c31\u53ef\u4ee5&#xff0c;\u7406\u8bba\u5b8c\u5168\u4e00\u6837&#xff0c;\u6ca1\u6709\u533a\u522b<\/li>\n<\/ul>\n<p>\u2714 \u5173\u952e\u70b9&#xff1a;\u4ee5\u524d\u8bb2 System V \u4fe1\u53f7\u91cf\u65f6\u662f\u5728&#034;\u8fdb\u7a0b\u95f4\u901a\u4fe1&#034;\u91cc\u8bb2\u7684&#xff0c;\u4eca\u5929\u5728&#034;\u7ebf\u7a0b&#034;\u91cc\u8bb2 POSIX \u4fe1\u53f7\u91cf \u2014\u2014 \u7406\u8bba\u4e00\u6a21\u4e00\u6837&#xff0c;\u53ea\u662f\u4f5c\u7528\u7684\u8303\u56f4\u4ece\u8fdb\u7a0b\u53d8\u6210\u4e86\u7ebf\u7a0b&#xff08;sem_init \u7684 pshared \u53c2\u6570\u63a7\u5236&#xff09;\u3002<\/p>\n<hr \/>\n<h3>\u4e8c\u3001\u2605 \u672c\u8d28\u7ed3\u8bba\u4e00&#xff1a;\u8ba1\u6570\u5668 &#061; \u5bf9\u7279\u5b9a\u8d44\u6e90\u7684\u300c\u9884\u8ba2\u673a\u5236\u300d<\/h3>\n<h4>2.1 \u7535\u5f71\u9662&#xff1a;\u4e70\u7968\u90a3\u4e00\u523b&#xff0c;\u5ea7\u4f4d\u5c31\u662f\u4f60\u7684\u4e86<\/h4>\n<p>\u7528\u7535\u5f71\u9662\u4e3e\u4f8b&#xff1a;<\/p>\n<p>\u4f60\u662f\u5c41\u80a1\u5750\u5728\u5ea7\u4f4d\u4e0a&#xff0c;\u8fd9\u4e2a\u5ea7\u4f4d\u8d44\u6e90\u624d\u5c5e\u4e8e\u4f60&#xff1f;\u8fd8\u662f\u7968\u4e70\u5230\u4e86&#xff0c;\u5ea7\u4f4d\u5c31\u662f\u4f60\u7684\u4e86&#xff1f;<br \/>\n\u7968\u4e70\u6210\u529f\u7684\u90a3\u4e00\u523b&#xff0c;\u5ea7\u4f4d\u5c31\u662f\u6211\u7684\u4e86\u3002\u54ea\u6015\u4eca\u5929\u8fd9\u573a\u7535\u5f71\u6211\u4e0d\u53bb\u770b&#xff0c;\u90a3\u4e2a\u5ea7\u4f4d\u4e5f\u5fc5\u987b\u7ed9\u6211\u7559\u7740\u3002<\/p>\n<p>\u7531\u6b64\u5f97\u51fa\u672c\u7ae0\u6700\u91cd\u8981\u7684\u4e00\u6761\u7ed3\u8bba&#xff1a;<\/p>\n<p>\u4fe1\u53f7\u91cf\u7684\u672c\u8d28\u662f\u4e00\u4e2a\u8ba1\u6570\u5668&#xff0c;\u662f\u5bf9\u7279\u5b9a\u8d44\u6e90\u7684\u300c\u9884\u8ba2\u673a\u5236\u300d\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261020182441-6ad7b1e9da2b6.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h4>2.2 \u843d\u5230\u4ee3\u7801\u4e0a\u5c31\u662f\u4e09\u884c<\/h4>\n<p><span class=\"token keyword\">int<\/span> sem <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">50<\/span><span class=\"token punctuation\">;<\/span>                 <span class=\"token comment\">\/\/ \u653e\u6620\u5385 50 \u4e2a\u5ea7\u4f4d \u2192 \u8ba1\u6570\u5668\u521d\u503c &#061; \u8d44\u6e90\u603b\u6570<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u4e70\u7968 &#061; \u7533\u8bf7\u8d44\u6e90 &#061; P \u64cd\u4f5c<\/span><br \/>\nsem<span class=\"token operator\">&#8212;<\/span><span class=\"token punctuation\">;<\/span>                        <span class=\"token comment\">\/\/ \u7533\u8bf7\u4e00\u4e2a<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>sem <span class=\"token operator\">&lt;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span>                  <span class=\"token comment\">\/\/ \u51cf\u5230\u8d1f\u6570 &#061; \u6ca1\u7968\u4e86<\/span><br \/>\n    \u963b\u585e\u6302\u8d77<span class=\"token punctuation\">;<\/span>                  <span class=\"token comment\">\/\/ \u6392\u961f\u7b49\u522b\u4eba\u9000\u7968&#xff08;\u4e0d\u662f\u5fd9\u7b49&#xff01;&#xff09;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u9000\u7968 \/ \u7528\u5b8c\u5f52\u8fd8 &#061; V \u64cd\u4f5c<\/span><br \/>\nsem<span class=\"token operator\">&#043;&#043;<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>&#x1f4a1; \u8fd9\u6837\u5c31\u80fd\u4fdd\u8bc1&#xff1a;\u8fdb\u653e\u6620\u5385\u7684\u4eba\u6570\u6c38\u8fdc\u4e0d\u4f1a\u8d85\u8fc7\u5ea7\u4f4d\u6570&#xff0c;\u6700\u591a\u76f8\u7b49\u3002\u518d\u901a\u8fc7\u5408\u7406\u7684\u8c03\u5ea6\u8ba9\u4e0d\u540c\u7ebf\u7a0b\u5750\u4e0d\u540c\u5ea7\u4f4d \u2014\u2014 \u5927\u5bb6\u5c31\u80fd\u540c\u65f6\u8bbf\u95ee\u653e\u6620\u5385\u91cc\u4e0d\u540c\u7684\u5ea7\u4f4d\u4e86\u3002<\/p>\n<h4>2.3 \u653e\u6620\u5385\u6700\u6015\u7684\u4e24\u4ef6\u4e8b<\/h4>\n<table>\n<tr>\u4e8b\u6545\u6280\u672f\u542b\u4e49<\/tr>\n<tbody>\n<tr>\n<td>\u7968\u5356\u591a\u4e86<\/td>\n<td>\u8ba1\u6570\u5668\u51cf\u5230\u8d1f\u6570\u8fd8\u653e\u4eba\u8fdb\u6765 \u2192 \u8d85\u5356 \u2192 \u6570\u636e\u88ab\u8986\u76d6<\/td>\n<\/tr>\n<tr>\n<td>\u7968\u53f7\u91cd\u590d<\/td>\n<td>\u4e24\u4e2a\u7ebf\u7a0b\u62ff\u5230\u540c\u4e00\u4e2a\u4f4d\u7f6e \u2192 \u540c\u65f6\u8bfb\u5199\u540c\u4e00\u5757\u5185\u5b58 \u2192 \u6570\u636e\u4e0d\u4e00\u81f4<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u2714 \u8fd9\u4e24\u6761\u6b63\u597d\u5bf9\u5e94\u540e\u9762\u73af\u5f62\u961f\u5217\u7684\u4e24\u6761\u94c1\u5f8b&#xff1a;<br \/>\n\u2460 \u4e0d\u8981\u8bbf\u95ee\u540c\u4e00\u4e2a\u4f4d\u7f6e&#xff08;\u4e92\u65a5&#xff09;&#xff1b;\u2461 \u4e0d\u8981\u653e\u5165\u8fc7\u591a\u7684\u8fdb\u7a0b\u8fdb\u6765&#xff08;\u9650\u6d41&#xff09;\u3002<\/p>\n<h4>2.4 \u4e3a\u4ec0\u4e48&#034;\u4e70\u7968&#034;\u4e0d\u7b97\u8bbf\u95ee\u8d44\u6e90&#xff1f;<\/h4>\n<p>\u8fd9\u662f\u8fd9\u5957\u6bd4\u55bb\u6700\u5999\u7684\u5730\u65b9&#xff1a;<\/p>\n<ul>\n<li>\u4e70\u7968 &#061; \u9884\u8ba2\u6210\u529f&#xff0c;\u7968\u8fd8\u6ca1\u7528 \u2192 \u53ea\u662f\u628a&#034;\u8fd8\u5269\u591a\u5c11\u4efd&#034;\u8fd9\u4e2a\u5224\u5b9a\u7ed9\u505a\u6389\u4e86<\/li>\n<li>\u771f\u6b63\u8fdb\u573a\u843d\u5ea7 &#061; \u8bbf\u95ee\u4e34\u754c\u8d44\u6e90<\/li>\n<\/ul>\n<p>&#x1f4a1; \u6240\u4ee5\u4fe1\u53f7\u91cf\u628a\u300c\u8d44\u6e90\u591f\u4e0d\u591f\u300d\u7684\u5224\u5b9a&#xff0c;\u63d0\u524d\u5230\u4e86\u8bbf\u95ee\u4e34\u754c\u8d44\u6e90\u4e4b\u524d&#xff0c;\u5e76\u4e14\u662f\u539f\u5b50\u5b8c\u6210\u7684\u3002\u8fd9\u4e00\u70b9\u5728\u7b2c\u5341\u8282\u4f1a\u518d\u62d4\u9ad8\u4e00\u6b21\u3002<\/p>\n<hr \/>\n<h3>\u4e09\u3001\u4e8c\u5143\u4fe1\u53f7\u91cf\u5c31\u662f\u4e92\u65a5\u9501<\/h3>\n<p>\u7ee7\u7eed\u5f80\u4e0b\u63a8&#xff1a;<\/p>\n<ul>\n<li>\u666e\u901a\u653e\u6620\u5385\u6709 50 \u4e2a\u5ea7\u4f4d \u2192 sem &#061; 50 \u2192 \u591a\u5143\u4fe1\u53f7\u91cf<\/li>\n<li>\u8d85\u7ea7 VIP \u653e\u6620\u5385\u53ea\u6709 1 \u4e2a\u5ea7\u4f4d \u2192 sem &#061; 1 \u2192 \u4e8c\u5143\u4fe1\u53f7\u91cf<\/li>\n<\/ul>\n<p>sem &#061; 1 \u65f6&#xff1a;\u4f60\u7533\u8bf7\u4e00\u6b21\u6210\u529f\u4e86&#xff0c;\u6574\u4e2a\u653e\u6620\u5385\u5c31\u662f\u4f60\u7684&#xff1b;\u7533\u8bf7\u5931\u8d25\u5c31\u7b49\u7740\u3002<\/p>\n<p>\u4e8c\u5143\u4fe1\u53f7\u91cf\u7684\u672c\u8d28&#xff0c;\u4e0d\u5c31\u662f\u4e92\u65a5\u5417&#xff1f;<\/p>\n<p>\u800c\u4e92\u65a5\u6211\u4eec\u5df2\u7ecf\u5b66\u8fc7\u4e86 \u2014\u2014 \u6240\u4ee5 mutex \u53ef\u4ee5\u770b\u4f5c\u4e8c\u5143\u4fe1\u53f7\u91cf\u7684\u4e00\u4e2a\u7279\u4f8b\u5b9e\u73b0\u3002<\/p>\n<p>\u2714 \u8bb0\u6cd5&#xff1a;\u4fe1\u53f7\u91cf\u53ea\u6709 1 \u6216 0 \u4e24\u79cd\u72b6\u6001\u7684&#xff0c;\u53eb\u4e8c\u5143\u4fe1\u53f7\u91cf&#xff0c;\u5b83\u5c31\u662f\u4e92\u65a5\u3002<\/p>\n<hr \/>\n<h3>\u56db\u3001\u2605 \u672c\u8d28\u7ed3\u8bba\u4e8c&#xff1a;\u4e24\u79cd\u4f7f\u7528\u573a\u666f&#xff0c;\u51b3\u5b9a\u4f60\u7528 mutex \u8fd8\u662f sem<\/h3>\n<p>\u8fd9\u662f\u672c\u7ae0\u7b2c\u4e8c\u4e2a\u91cd\u8981\u7ed3\u8bba&#xff0c;\u4e5f\u662f\u6574\u4e2a\u9009\u578b\u7684\u5206\u6c34\u5cad\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261020182444-6ad7b1ecde9b2.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h4>\u573a\u666f\u4e00&#xff1a;\u628a\u76ee\u6807\u8d44\u6e90\u6574\u4f53\u4f7f\u7528<\/h4>\n<p>\u5178\u578b\u4f8b\u5b50\u5c31\u662f\u4e0a\u4e00\u7ae0\u6211\u4eec\u5199\u7684 \u57fa\u4e8e\u6761\u4ef6\u53d8\u91cf\u7684\u963b\u585e\u961f\u5217\u3002<\/p>\n<p>\u201c\u6211\u4eec\u7684\u963b\u585e\u961f\u5217\u91cc&#xff0c;std::queue \u662f\u5bb9\u5668\u9002\u914d\u5668&#xff0c;\u5b83\u5185\u90e8\u5c01\u88c5\u7684\u7c7b\u91cc\u9762\u6709\u5934\u6307\u9488\u3001\u6709\u5c3e\u6307\u9488&#xff0c;\u8fd8\u8981\u505a\u7a7a\u95f4\u7684\u4e8c\u6b21\u6269\u5bb9\u3002\u4f60\u5728 pop \u7684\u65f6\u5019\u5b83\u53ef\u80fd\u6b63\u5728 push&#xff0c;\u6b63\u5728\u4e8c\u6b21\u6269\u5bb9 \u2014\u2014 pop \u4e0d\u5c31\u5931\u8d25\u4e86\u5417&#xff1f;\u6240\u4ee5\u90a3\u79cd\u961f\u5217\u5b83\u4e0d\u80fd\u591f\u5e76\u53d1\u8d77\u6765&#xff0c;\u53ea\u80fd\u88ab\u67d0\u4e00\u4e2a\u751f\u4ea7\u8005\u6216\u67d0\u4e00\u4e2a\u6d88\u8d39\u8005\u5355\u72ec\u4f7f\u7528\u3002\u201d<\/p>\n<p>\u6240\u4ee5\u963b\u585e\u961f\u5217\u5fc5\u987b\u6574\u4f53\u52a0\u9501 \u2192 \u7528 mutex \u6216\u4e8c\u5143\u4fe1\u53f7\u91cf\u3002<\/p>\n<h4>\u573a\u666f\u4e8c&#xff1a;\u628a\u76ee\u6807\u8d44\u6e90\u6309\u5757\u5206\u6279\u4f7f\u7528<\/h4>\n<p>\u201c\u6211\u4eec\u8bbf\u95ee\u5185\u5b58\u7a7a\u95f4\u8bfb\u6570\u636e\u5199\u6570\u636e&#xff0c;\u996d\u4e5f\u5f97\u4e00\u53e3\u4e00\u53e3\u5403&#xff0c;\u7a7a\u95f4\u6211\u7ed9\u4f60\u7533\u8bf7\u4e00\u5927\u5806&#xff0c;\u4f60\u4e5f\u662f\u4e00\u4e2a\u4e00\u4e2a\u8fc7\u7684\u3002\u201d<\/p>\n<p>\u5982\u679c\u4e00\u5757\u5927\u8d44\u6e90\u53ef\u4ee5\u88ab\u5212\u5206\u6210\u4e00\u5c0f\u5757\u4e00\u5c0f\u5757&#xff0c;\u800c\u4e14\u6bcf\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u4e0d\u540c\u7684\u5757&#xff0c;\u90a3\u5c31\u5b8c\u5168\u6ca1\u6709\u51b2\u7a81 \u2014\u2014 \u53ef\u4ee5\u8ba9\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8fdb\u6765\u3002<\/p>\n<p>\u8981\u505a\u7684\u4e8b\u60c5\u65e0\u975e\u4e24\u70b9&#xff1a;\u2460 \u4e0d\u8981\u653e\u8fc7\u591a\u7684\u8fdb\u7a0b\/\u7ebf\u7a0b\u8fdb\u6765&#xff1b;\u2461 \u4e0d\u8981\u8ba9\u591a\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u540c\u4e00\u4e2a\u4f4d\u7f6e\u3002<\/p>\n<p>\u2192 \u8fd9\u65f6\u5019\u7528\u591a\u5143\u4fe1\u53f7\u91cf&#xff08;sem &#061; N&#xff09;\u3002<\/p>\n<h4>\u9009\u578b\u7ed3\u8bba<\/h4>\n<table>\n<tr>\u8d44\u6e90\u4f7f\u7528\u65b9\u5f0f\u7528\u4ec0\u4e48\u7406\u7531<\/tr>\n<tbody>\n<tr>\n<td>\u6574\u4f53\u4f7f\u7528&#xff0c;\u4e00\u6b21\u53ea\u8bb8\u4e00\u4e2a\u4eba\u5b8c\u6574\u5360\u7528<\/td>\n<td>mutex \/ \u4e8c\u5143\u4fe1\u53f7\u91cf<\/td>\n<td>\u5fc5\u987b\u4e32\u884c<\/td>\n<\/tr>\n<tr>\n<td>\u6309\u5757\u5206\u6279\u4f7f\u7528&#xff0c;\u5404\u7ebf\u7a0b\u8bbf\u95ee\u4e0d\u540c\u5757<\/td>\n<td>\u591a\u5143\u4fe1\u53f7\u91cf<\/td>\n<td>\u53ef\u4ee5\u771f\u6b63\u5e76\u53d1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h3>\u4e94\u3001\u4e3a\u4ec0\u4e48 P \/ V \u5fc5\u987b\u539f\u5b50&#xff1a;\u4fe1\u53f7\u91cf\u672c\u8eab\u4e5f\u662f\u4e34\u754c\u8d44\u6e90<\/h3>\n<p>\u8fd9\u4e00\u6b65\u63a8\u5bfc\u7279\u522b\u5173\u952e&#xff0c;\u662f\u6574\u4e2a\u4fe1\u53f7\u91cf\u80fd\u6210\u7acb\u7684\u524d\u63d0\u3002<\/p>\n<h4>5.1 \u63a8\u5bfc\u94fe<\/h4>\n<li>\u6240\u6709\u7ebf\u7a0b\u8bbf\u95ee\u8d44\u6e90\u4e4b\u524d&#xff0c;\u90fd\u5f97\u5148\u770b\u4fe1\u53f7\u91cf<\/li>\n<li>\u4fe1\u53f7\u91cf\u53c8\u5fc5\u987b\u88ab\u6240\u6709\u4eba\u770b\u5230 \u2192 \u5b83\u662f\u4e2a\u5171\u4eab\u53d8\u91cf<\/li>\n<li>\u5171\u4eab\u53d8\u91cf &#043;&#043; \/ &#8212; \u4e0d\u662f\u539f\u5b50\u7684&#xff08;\u7ebf\u7a0b\u4e92\u65a5\u90a3\u4e00\u7ae0\u5df2\u7ecf\u7528\u4e09\u6761\u6c47\u7f16\u8bc1\u660e\u8fc7&#xff09;<\/li>\n<li>\u6240\u4ee5 \u2014\u2014 \u4fe1\u53f7\u91cf\u672c\u8eab\u4e5f\u662f\u4e34\u754c\u8d44\u6e90&#xff01;<\/li>\n<p>\u8001\u5e08\u539f\u8bdd&#xff1a;\u201c\u6211\u4eec\u4fe1\u53f7\u91cf\u4e0d\u80fd\u662f\u4e00\u4e2a\u7b80\u5355\u7684 int count \u52a0\u52a0\u3001\u51cf\u51cf&#xff0c;\u90a3\u4e0d\u884c&#xff0c;\u90a3\u5e76\u4e0d\u662f\u539f\u5b50\u7684\u3002\u201d<\/p>\n<h4>5.2 \u4e8e\u662f\u6709\u4e86 P \u548c V<\/h4>\n<table>\n<tr>\u64cd\u4f5c\u8bed\u4e49\u672c\u8d28<\/tr>\n<tbody>\n<tr>\n<td>P \u64cd\u4f5c<\/td>\n<td>\u7533\u8bf7\u8d44\u6e90&#xff08;sem_wait&#xff09;<\/td>\n<td>\u5bf9\u4fe1\u53f7\u91cf\u7684\u503c\u505a &#8211;&#xff0c;\u5fc5\u987b\u539f\u5b50<\/td>\n<\/tr>\n<tr>\n<td>V \u64cd\u4f5c<\/td>\n<td>\u91ca\u653e\u8d44\u6e90&#xff08;sem_post&#xff09;<\/td>\n<td>\u5bf9\u4fe1\u53f7\u91cf\u7684\u503c\u505a &#043;&#043;&#xff0c;\u5fc5\u987b\u539f\u5b50<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&#x1f4a1; P \/ V \u6765\u81ea\u8377\u5170\u8bed&#xff08;Proberen \u5c1d\u8bd5 \/ Verhogen \u589e\u52a0&#xff09;&#xff0c;Dijkstra \u63d0\u51fa\u7684\u3002\u56fd\u5185\u6559\u6750\u901a\u7528 P\/V \u8bb0\u6cd5&#xff0c;\u4ee3\u7801\u91cc\u53eb sem_wait \/ sem_post\u3002<\/p>\n<h4>5.3 \u80fd\u4e0d\u80fd\u81ea\u5df1\u7528\u300c\u9501 &#043; \u8ba1\u6570\u5668\u300d\u5b9e\u73b0\u4e00\u4e2a\u4fe1\u53f7\u91cf&#xff1f;<\/h4>\n<p>\u53ef\u4ee5\u3002 \u4e00\u628a\u9501\u4fdd\u62a4\u4e00\u4e2a int count&#xff0c;\u5c31\u80fd\u6a21\u62df\u4fe1\u53f7\u91cf\u7684\u884c\u4e3a\u3002<\/p>\n<p>\u4f46&#xff1a;\u4e0d\u91cd\u8981\u3002\u5173\u952e\u662f&#034;\u60f3\u8bbf\u95ee\u8d44\u6e90\u5fc5\u987b\u5148\u8fc7\u4fe1\u53f7\u91cf\u8fd9\u4e00\u5173&#034; \u2014\u2014<br \/>\nP \u6210\u529f &#061; \u9884\u8ba2\u6210\u529f &#061; \u91cc\u9762\u4e00\u5b9a\u6709\u4e00\u4efd\u8d44\u6e90\u662f\u7ed9\u4f60\u7559\u7740\u7684\u3002<\/p>\n<hr \/>\n<h3>\u516d\u3001\u94fa\u57ab&#xff1a;\u73af\u5f62\u961f\u5217&#xff0c;\u4ee5\u53ca&#034;\u7a7a\u548c\u6ee1&#034;\u7684\u8001\u95ee\u9898<\/h3>\n<h4>6.1 \u590d\u4e60&#xff1a;\u73af\u5f62\u961f\u5217\u957f\u4ec0\u4e48\u6837<\/h4>\n<ul>\n<li>\u5b83\u4e5f\u662f\u4e2a\u961f\u5217&#xff0c;\u6240\u4ee5\u4e00\u5b9a\u6709\u5934\u6709\u5c3e&#xff08;head \/ tail&#xff09;<\/li>\n<li>\u521d\u59cb\u65f6 head &#061;&#061; tail<\/li>\n<li>\u5f80\u5c3e\u90e8\u63d2\u5165\u5143\u7d20&#xff0c;tail \u5f80\u540e\u79fb&#xff1b;\u79fb\u5230\u672b\u5c3e\u5c31\u7ed5\u56de\u5f00\u5934 \u2014\u2014 \u8fd9\u5c31\u662f&#034;\u73af&#034;<\/li>\n<\/ul>\n<h4>6.2 \u81f4\u547d\u95ee\u9898&#xff1a;\u7a7a\u548c\u6ee1\u6307\u5411\u540c\u4e00\u4e2a\u4f4d\u7f6e<\/h4>\n<p>\u7a7a&#xff1a;head &#061;&#061; tail<br \/>\n\u6ee1&#xff1a;head &#061;&#061; tail     \u2190 \u4e00\u6a21\u4e00\u6837&#xff0c;\u5206\u4e0d\u51fa\u6765<\/p>\n<p>\u7ecf\u5178\u7684\u4e24\u79cd\u89e3\u51b3\u65b9\u6848&#xff1a;<\/p>\n<table>\n<tr>\u65b9\u6848\u505a\u6cd5\u4ee3\u4ef7<\/tr>\n<tbody>\n<tr>\n<td>\u65b9\u6848 1<\/td>\n<td>\u52a0\u4e00\u4e2a\u8ba1\u6570\u5668 int count&#xff1a;\u5165\u961f count&#043;&#043;&#xff0c;\u51fa\u961f count&#8211;\u7a7a\u65f6 count &#061;&#061; 0&#xff0c;\u6ee1\u65f6 count &#061;&#061; N<\/td>\n<td>\u591a\u4e00\u4e2a\u53d8\u91cf\u8981\u7ef4\u62a4<\/td>\n<\/tr>\n<tr>\n<td>\u65b9\u6848 2<\/td>\n<td>\u7a7a\u4e00\u4e2a\u4f4d\u7f6e&#xff1a;tail &#043; 1 &#061;&#061; head \u89c6\u4e3a\u6ee1&#xff0c;\u6c38\u8fdc\u7559\u4e00\u4e2a\u683c\u5b50\u4e0d\u7528<\/td>\n<td>\u6d6a\u8d39\u4e00\u4e2a\u69fd\u4f4d<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>6.3 \u7269\u7406\u5b9e\u73b0&#xff1a;\u56fa\u5b9a\u957f\u5ea6\u6570\u7ec4 &#043; \u6a21\u8fd0\u7b97<\/h4>\n<p>\u8bfe\u4ef6\u677f\u4e66\u7ed9\u7684\u662f\u6700\u6734\u7d20\u4e5f\u6700\u5b9e\u7528\u7684\u505a\u6cd5&#xff1a;<\/p>\n<p><span class=\"token keyword\">int<\/span> head <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> tail <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>tail<span class=\"token operator\">&#043;&#043;<\/span><span class=\"token punctuation\">;<\/span>  tail <span class=\"token operator\">%&#061;<\/span> N<span class=\"token punctuation\">;<\/span>      <span class=\"token comment\">\/\/ \u5165\u961f\u540e\u7ed5\u56de<\/span><br \/>\nhead<span class=\"token operator\">&#043;&#043;<\/span><span class=\"token punctuation\">;<\/span>  head <span class=\"token operator\">%&#061;<\/span> N<span class=\"token punctuation\">;<\/span>      <span class=\"token comment\">\/\/ \u51fa\u961f\u540e\u7ed5\u56de<\/span><\/p>\n<p>\u4f46\u662f&#xff1a;\u201c\u4f60\u5f80\u540e\u79fb\u4e0d\u5c31\u8d85\u8303\u56f4\u4e86\u5417&#xff1f;\u968f\u65f6\u90fd\u53ef\u80fd\u5d29\u6389\u3002\u6240\u4ee5 tail&#043;&#043; \u4e4b\u540e\u4e0d\u7ba1\u662f\u4ec0\u4e48&#xff0c;\u76f4\u63a5\u6a21\u4e0a N\u3002\u201d<\/p>\n<p>&#x1f4a1; \u903b\u8f91\u4e0a\u662f\u73af&#xff0c;\u7269\u7406\u4e0a\u5c31\u662f\u4e00\u4e2a\u957f\u5ea6\u4e3a N \u7684\u6570\u7ec4 &#043; \u6a21\u8fd0\u7b97\u3002\u5f53\u7136\u4f60\u4e5f\u53ef\u4ee5\u7528\u5355\u94fe\u8868\u9996\u5c3e\u76f8\u8fde\u505a&#xff0c;\u6ca1\u5fc5\u8981\u3002<\/p>\n<h4>6.4 \u4f46\u4eca\u5929&#xff0c;\u6211\u4eec\u4e0d\u5224\u65ad\u7a7a\u6ee1<\/h4>\n<p>\u201c\u6211\u4eca\u5929\u8981\u8bb2\u7684\u548c\u73af\u5f62\u961f\u5217\u6709\u5173&#xff0c;\u4f46\u6211\u4eec\u4e0d\u9700\u8981\u518d\u53bb\u5224\u65ad\u4ec0\u4e48\u4e3a\u7a7a\u4e3a\u6ee1\u4e86&#xff0c;\u538b\u6839\u5c31\u4e0d\u7528\u5173\u5fc3 \u2014\u2014 \u56e0\u4e3a\u6211\u4eca\u5929\u8981\u5b8c\u6210\u7684\u662f\u57fa\u4e8e\u73af\u5f62\u961f\u5217\u7684\u751f\u4ea7\u8005\u6d88\u8d39\u8005\u6a21\u578b&#xff0c;\u4f60\u5230\u5e95\u6709\u6ca1\u6709\u6ee1\u3001\u6709\u6ca1\u6709\u7a7a&#xff0c;\u6211\u4eec\u662f\u6709\u5176\u4ed6\u65b9\u6848\u6765\u8861\u91cf\u7684\u3002\u8fd9\u4e2a\u65b9\u6848\u5c31\u53eb\u505a\u4fe1\u53f7\u91cf\u3002\u201d<\/p>\n<hr \/>\n<h3>\u4e03\u3001\u2605 \u8ffd\u9010\u6e38\u620f&#xff1a;\u56db\u6761\u7ea6\u5b9a<\/h3>\n<p>\u628a\u73af\u5f62\u961f\u5217\u6bd4\u55bb\u6210\u4e00\u5f20\u5927\u5706\u684c&#xff0c;\u6bcf\u4e2a\u683c\u5b50\u5c31\u662f\u4e00\u4e2a\u76d8\u5b50&#xff1a;<\/p>\n<ul>\n<li>\u6211\u662f\u751f\u4ea7\u8005&#xff0c;\u5f80\u76d8\u5b50\u91cc\u653e\u82f9\u679c<\/li>\n<li>\u4f60\u662f\u6d88\u8d39\u8005&#xff0c;\u4ece\u76d8\u5b50\u91cc\u62ff\u82f9\u679c<\/li>\n<li>\u6211\u4ece\u5c3e\u90e8\u5f00\u59cb\u653e&#xff0c;\u4f60\u4ece\u5934\u90e8\u5f00\u59cb\u62ff<\/li>\n<\/ul>\n<h4>7.1 \u6e38\u620f\u662f\u8fd9\u6837\u8fdb\u884c\u7684<\/h4>\n<li>\u76d8\u5b50\u5168\u7a7a\u65f6&#xff0c;\u4e24\u4e2a\u4eba\u6307\u5411\u540c\u4e00\u4e2a\u4f4d\u7f6e \u2192 \u4f60\u5f97\u8ba9\u6211\u5148\u751f\u4ea7&#xff08;\u4f60\u62ff\u4e5f\u62ff\u4e0d\u5230&#xff0c;\u91cc\u9762\u538b\u6839\u6ca1\u6709&#xff09;<\/li>\n<li>\u6211\u4e00\u76f4\u653e\u3001\u4e00\u76f4\u653e&#xff0c;\u653e\u6ee1\u4e86 \u2192 \u4e24\u4e2a\u4eba\u53c8\u6307\u5411\u540c\u4e00\u4e2a\u4f4d\u7f6e<\/li>\n<li>\u8fd9\u65f6\u5019\u6211\u4e0d\u80fd\u518d\u653e\u4e86&#xff08;\u518d\u653e\u5c31\u628a\u5386\u53f2\u4e0a\u7684\u82f9\u679c\u8986\u76d6\u4e86&#xff0c;\u76d8\u5b50\u653e\u4e0d\u4e0b\u4f1a\u6389&#xff09;<\/li>\n<li>\u6362\u4f60\u62ff&#xff0c;\u4f60\u62ff\u62ff\u62ff&#xff0c;\u62ff\u7a7a\u4e86 \u2192 \u4e24\u4e2a\u4eba\u53c8\u56de\u5230\u540c\u4e00\u4e2a\u4f4d\u7f6e<\/li>\n<li>\u53c8\u8f6e\u5230\u6211\u751f\u4ea7 \u2014\u2014 \u8fd9\u662f\u4e00\u573a\u8ffd\u9010\u6e38\u620f<\/li>\n<p>\u64cd\u573a\u8dd1\u5708\u7684\u6bd4\u55bb&#xff1a;\u4f60\u7ad9\u7740\u4e0d\u52a8&#xff0c;\u6211\u4e00\u76f4\u8dd1&#xff0c;\u5f53\u6211\u5957\u4f60\u6574\u6574\u4e00\u5708\u65f6&#xff0c;\u6211\u4eec\u5c31\u5728\u540c\u4e00\u4e2a\u4f4d\u7f6e\u4e86\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261020182447-6ad7b1ef3a307.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h4>7.2 \u56db\u6761\u7ea6\u5b9a&#xff08;\u7f3a\u4e00\u4e0d\u53ef&#xff09;<\/h4>\n<table>\n<tr>\u7ea6\u5b9a\u5185\u5bb9\u4e3a\u4ec0\u4e48<\/tr>\n<tbody>\n<tr>\n<td>\u7ea6\u5b9a 1<\/td>\n<td>\u4e3a\u7a7a&#xff0c;\u751f\u4ea7\u8005\u5148\u8fd0\u884c<\/td>\n<td>\u7a7a\u7684\u65f6\u5019\u6d88\u8d39\u8005\u62ff\u4e0d\u5230\u4efb\u4f55\u4e1c\u897f<\/td>\n<\/tr>\n<tr>\n<td>\u7ea6\u5b9a 2<\/td>\n<td>\u4e3a\u6ee1&#xff0c;\u6d88\u8d39\u8005\u5148\u8fd0\u884c<\/td>\n<td>\u6ee1\u7684\u65f6\u5019\u751f\u4ea7\u8005\u518d\u653e\u5c31\u4f1a\u8986\u76d6\u6570\u636e<\/td>\n<\/tr>\n<tr>\n<td>\u7ea6\u5b9a 3<\/td>\n<td>\u751f\u4ea7\u8005\u4e0d\u80fd\u628a\u6d88\u8d39\u8005\u5957\u4e00\u4e2a\u5708\u4ee5\u4e0a<\/td>\n<td>\u6700\u591a\u5e76\u9a7e\u9f50\u9a71&#xff1b;\u5957\u4e00\u5708\u5c31\u7b49\u4e8e\u8986\u76d6\u4e86\u6240\u6709\u5386\u53f2\u6570\u636e<\/td>\n<\/tr>\n<tr>\n<td>\u7ea6\u5b9a 4<\/td>\n<td>\u6d88\u8d39\u8005\u4e0d\u80fd\u8d85\u8fc7\u751f\u4ea7\u8005<\/td>\n<td>\u8d85\u8fc7\u751f\u4ea7\u8005 &#061; \u8dd1\u5230\u524d\u9762\u53bb\u62ff&#xff0c;\u62ff\u5230\u7684\u5168\u662f\u65e0\u6548\u6570\u636e&#xff08;\u201c\u8b66\u5bdf\u8dd1\u5230\u5c0f\u5077\u524d\u9762\u53bb\u4e86&#xff0c;\u8fd9\u5c31\u5f88\u6ed1\u7a3d\u201d&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u7ea6\u5b9a 3\u30014 \u5408\u8d77\u6765\u8bf4\u7684\u5176\u5b9e\u662f\u4e00\u4ef6\u4e8b&#xff1a;\u4e24\u4e2a\u4eba\u7684\u76f8\u5bf9\u8ddd\u79bb\u6c38\u8fdc\u4e0d\u80fd\u8d85\u8fc7\u4e00\u5708\u3002<\/p>\n<p>\u2714 \u8bb0\u4f4f\u4e00\u53e5\u8bdd&#xff1a;\u7ea6\u5b9a 1\u30012 \u7ba1&#034;\u8c01\u5148\u8dd1&#034;&#xff0c;\u7ea6\u5b9a 3\u30014 \u7ba1&#034;\u8c01\u4e0d\u80fd\u8d85\u8fc7\u8c01&#034;\u3002<\/p>\n<hr \/>\n<h3>\u516b\u3001\u4e24\u4e2a\u7ed3\u8bba&#xff1a;\u4ec0\u4e48\u65f6\u5019\u53ef\u4ee5\u300c\u4e00\u8d77\u8dd1\u300d<\/h3>\n<p>\u4e09\u4e2a\u95ee\u9898&#xff0c;\u7b54\u6848\u76f4\u63a5\u7ed9\u51fa\u7ed3\u8bba\u3002<\/p>\n<h4>\u601d\u8003 1&#xff1a;\u53ea\u8981\u4e0d\u8bbf\u95ee\u540c\u4e00\u4e2a\u4f4d\u7f6e&#xff0c;\u80fd\u4e0d\u80fd\u540c\u65f6\u8fdb\u884c&#xff1f;<\/h4>\n<p>\u80fd\u3002 \u6211\u5728\u7b2c 6 \u4e2a\u4f4d\u7f6e\u653e\u82f9\u679c&#xff0c;\u4f60\u5728\u7b2c 1 \u4e2a\u4f4d\u7f6e\u62ff\u82f9\u679c \u2014\u2014 \u5b8c\u5168\u4e0d\u51b2\u7a81&#xff0c;\u751f\u4ea7\u6d88\u8d39\u53ef\u4ee5\u771f\u6b63\u540c\u65f6\u53d1\u751f\u3002<\/p>\n<h4>\u601d\u8003 2&#xff1a;\u4ec0\u4e48\u65f6\u5019\u4e24\u4e2a\u4eba\u4f1a\u6307\u5411\u540c\u4e00\u4e2a\u4f4d\u7f6e&#xff1f;<\/h4>\n<p>\u201c\u8fd9\u4e00\u4e2a\u95ee\u9898\u53ea\u8981\u60f3\u660e\u767d\u4e86&#xff0c;\u73af\u5f62\u961f\u5217\u7684\u95ee\u9898\u5c31\u7406\u89e3\u4e00\u534a\u4e86\u3002\u201d<\/p>\n<p>\u7b54\u6848&#xff1a;\u53ea\u6709\u4e24\u79cd\u60c5\u51b5 \u2014\u2014 \u4e3a\u7a7a&#xff0c;\u6216\u8005\u4e3a\u6ee1\u3002<br \/>\n\u56e0\u4e3a head \u548c tail \u53ea\u6709\u5728\u4e3a\u7a7a\u548c\u4e3a\u6ee1\u65f6\u624d\u4f1a\u91cd\u5408&#xff0c;\u5176\u4ed6\u4efb\u4f55\u65f6\u5019\u90fd\u4e0d\u4f1a\u3002<\/p>\n<h4>\u601d\u8003 3&#xff1a;\u4e3a\u7a7a \/ \u4e3a\u6ee1\u7684\u65f6\u5019\u8c01\u5148\u8dd1&#xff1f;<\/h4>\n<ul>\n<li>\u4e3a\u7a7a \u2192 \u6d88\u8d39\u8005\u6ca1\u6cd5\u6d88\u8d39 \u2192 \u53ea\u80fd\u8ba9\u751f\u4ea7\u8005\u5148\u8fd0\u884c<\/li>\n<li>\u4e3a\u6ee1 \u2192 \u751f\u4ea7\u8005\u6ca1\u6cd5\u751f\u4ea7 \u2192 \u53ea\u80fd\u8ba9\u6d88\u8d39\u8005\u5148\u8fd0\u884c<\/li>\n<\/ul>\n<p>\u6ce8\u610f\u8fd9\u4e0d\u53ea\u662f&#034;\u5148\u540e&#034;\u95ee\u9898&#xff0c;\u8fd8\u5fc5\u987b\u662f\u4e92\u65a5\u7684&#xff1a;\u4e0d\u80fd\u6211\u751f\u4ea7\u8005\u6b63\u5728\u751f\u4ea7\u7684\u65f6\u5019\u4f60\u5c31\u9760\u8fdb\u6765\u4e86\u3002<\/p>\n<h4>\u4e8e\u662f\u5f97\u5230\u4e24\u6761\u7ed3\u8bba<\/h4>\n<p>\u7ed3\u8bba 1&#xff1a;\u73af\u5f62\u961f\u5217\u3010\u4e0d\u4e3a\u7a7a &amp;&amp; \u4e0d\u4e3a\u6ee1\u3011 \u2192 \u751f\u4ea7\u548c\u6d88\u8d39\u53ef\u4ee5\u3010\u540c\u65f6\u8fdb\u884c\u3011&#xff01;<br \/>\n\u7ed3\u8bba 2&#xff1a;\u73af\u5f62\u961f\u5217\u3010\u4e3a\u7a7a || \u4e3a\u6ee1\u3011     \u2192 \u751f\u4ea7\u548c\u6d88\u8d39\u9700\u8981\u3010\u540c\u6b65 &#043; \u4e92\u65a5\u3011&#xff01;<\/p>\n<p>&#x1f4a1; \u8fd9\u4e24\u6761\u7ed3\u8bba\u7684&#034;\u5386\u53f2\u610f\u4e49&#034;\u5728\u4e8e&#xff1a;\u5b83\u628a\u73af\u5f62\u961f\u5217\u7684\u95ee\u9898&#xff0c;\u4ece&#034;\u600e\u4e48\u5224\u65ad\u7a7a\u6ee1&#034;\u8f6c\u5316\u6210\u4e86&#034;\u600e\u4e48\u4fdd\u8bc1\u8fd9 4 \u4e2a\u7ea6\u5b9a&#034;\u3002<br \/>\n\u7b54&#xff1a;\u7528\u4fe1\u53f7\u91cf\u6765\u4fdd\u8bc1\u3002<\/p>\n<hr \/>\n<h3>\u4e5d\u3001\u4fe1\u53f7\u91cf\u7684\u4e24\u5f20\u7968&#xff1a;sem_blank \u4e0e sem_data<\/h3>\n<p>\u5728\u52a8\u624b\u5199\u4ee3\u7801\u4e4b\u524d&#xff0c;\u5148\u628a&#034;\u751f\u4ea7\u8005\u5173\u5fc3\u4ec0\u4e48\u3001\u6d88\u8d39\u8005\u5173\u5fc3\u4ec0\u4e48&#034;\u5206\u6e05\u695a\u3002<\/p>\n<h4>9.1 \u751f\u4ea7\u8005\u5173\u5fc3\u300c\u7a7a\u4f4d\u300d&#xff0c;\u6d88\u8d39\u8005\u5173\u5fc3\u300c\u6570\u636e\u300d<\/h4>\n<p>\u201c\u6211\u653e\u82f9\u679c&#xff0c;\u4f60\u62ff\u82f9\u679c\u3002\u4f60\u5f53\u7136\u6700\u5173\u6ce8\u7684\u662f\u82f9\u679c&#xff0c;\u800c\u6211\u4f5c\u4e3a\u653e\u82f9\u679c\u7684\u4eba&#xff0c;\u6700\u5173\u6ce8\u7684\u662f\u7a7a\u76d8\u5b50\u3002\u201d<\/p>\n<table>\n<tr>\u89d2\u8272\u5173\u5fc3\u7684\u8d44\u6e90\u4fe1\u53f7\u91cf\u521d\u503c<\/tr>\n<tbody>\n<tr>\n<td>\u751f\u4ea7\u8005<\/td>\n<td>\u73af\u5f62\u961f\u5217\u91cc\u7a7a\u7684\u4f4d\u7f6e<\/td>\n<td>sem_blank<\/td>\n<td>N&#xff08;\u521a\u5f00\u59cb\u5168\u7a7a&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u6d88\u8d39\u8005<\/td>\n<td>\u73af\u5f62\u961f\u5217\u91cc\u6709\u6548\u6570\u636e<\/td>\n<td>sem_data<\/td>\n<td>0&#xff08;\u521a\u5f00\u59cb\u6ca1\u6570\u636e&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261020182449-6ad7b1f1a8701.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h4>9.2 \u4ee3\u7801\u9aa8\u67b6<\/h4>\n<p><span class=\"token comment\">\/\/ \u751f\u4ea7\u8005<\/span><br \/>\n<span class=\"token function\">P<\/span><span class=\"token punctuation\">(<\/span>sem_blank<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>              <span class=\"token comment\">\/\/ \u7a7a\u4f4d\u51cf\u4e00&#xff0c;P \u4e0d\u5230\u5c31\u963b\u585e<\/span><br \/>\n<span class=\"token comment\">\/\/ \u8d70\u5230\u8fd9\u91cc&#xff0c;\u4e00\u5b9a\u6709\u7a7a\u95f4&#xff01;<\/span><br \/>\nring<span class=\"token punctuation\">[<\/span>p_step<span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> data<span class=\"token punctuation\">;<\/span><br \/>\np_step<span class=\"token operator\">&#043;&#043;<\/span><span class=\"token punctuation\">;<\/span>  p_step <span class=\"token operator\">%&#061;<\/span> N<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token function\">V<\/span><span class=\"token punctuation\">(<\/span>sem_data<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>               <span class=\"token comment\">\/\/ \u6570\u636e\u52a0\u4e00&#xff0c;\u901a\u77e5\u6d88\u8d39\u8005<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u6d88\u8d39\u8005<\/span><br \/>\n<span class=\"token function\">P<\/span><span class=\"token punctuation\">(<\/span>sem_data<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>               <span class=\"token comment\">\/\/ \u6570\u636e\u51cf\u4e00&#xff0c;P \u4e0d\u5230\u5c31\u963b\u585e<\/span><br \/>\n<span class=\"token comment\">\/\/ \u8d70\u5230\u8fd9\u91cc&#xff0c;\u4e00\u5b9a\u6709\u6570\u636e&#xff01;<\/span><br \/>\ndata <span class=\"token operator\">&#061;<\/span> ring<span class=\"token punctuation\">[<\/span>c_step<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span><br \/>\nc_step<span class=\"token operator\">&#043;&#043;<\/span><span class=\"token punctuation\">;<\/span>  c_step <span class=\"token operator\">%&#061;<\/span> N<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token function\">V<\/span><span class=\"token punctuation\">(<\/span>sem_blank<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>              <span class=\"token comment\">\/\/ \u7a7a\u4f4d\u52a0\u4e00&#xff0c;\u901a\u77e5\u751f\u4ea7\u8005<\/span><\/p>\n<h4>9.3 \u5b8c\u7f8e\u5bf9\u79f0<\/h4>\n<p>\u751f\u4ea7\u8005&#xff1a;P(\u7a7a\u4f4d) \u2192 \u751f\u4ea7 \u2192 V(\u6570\u636e)<br \/>\n\u6d88\u8d39\u8005&#xff1a;P(\u6570\u636e) \u2192 \u6d88\u8d39 \u2192 V(\u7a7a\u4f4d)<\/p>\n<p>\u2714 \u53cc\u65b9\u5404\u81ea P \u81ea\u5df1\u5173\u5fc3\u7684\u8d44\u6e90\u3001V \u5bf9\u65b9\u5173\u5fc3\u7684\u8d44\u6e90 \u2014\u2014 \u8fd9\u5c31\u662f\u4e24\u4e2a\u4fe1\u53f7\u91cf\u5168\u90e8\u7684\u6d41\u8f6c\u903b\u8f91\u3002<br \/>\n\u2714 \u800c\u4e14\u6ce8\u610f\u987a\u5e8f&#xff1a;P \u5728\u52a0\u9501\u4e4b\u524d&#xff08;\u591a\u751f\u4ea7\u591a\u6d88\u8d39\u65f6&#xff09;\u3002\u5148\u9884\u5b9a\u3001\u518d\u6392\u961f \u2014\u2014 \u5224\u5b9a\u8d44\u6e90\u662f\u5426\u5c31\u7eea&#xff0c;\u6839\u672c\u4e0d\u9700\u8981\u52a0\u9501\u3002\u7b2c\u5341\u4e09\u8282\u4e13\u95e8\u8bba\u8bc1\u8fd9\u4e00\u70b9\u3002<\/p>\n<hr \/>\n<h3>\u5341\u3001\u63a5\u53e3&#xff1a;POSIX \u4fe1\u53f7\u91cf\u7684\u56db\u4ef6\u5957<\/h3>\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;semaphore.h&gt;<\/span><\/span><\/p>\n<p><span class=\"token class-name\">sem_t<\/span> sem<span class=\"token punctuation\">;<\/span>                                    <span class=\"token comment\">\/\/ \u2460 \u7c7b\u578b&#xff1a;sem_t<\/span><\/p>\n<p><span class=\"token keyword\">int<\/span> <span class=\"token function\">sem_init<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">sem_t<\/span> <span class=\"token operator\">*<\/span>sem<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">int<\/span> pshared<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">unsigned<\/span> <span class=\"token keyword\">int<\/span> value<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>   <span class=\"token comment\">\/\/ \u2461 \u521d\u59cb\u5316<\/span><br \/>\n<span class=\"token comment\">\/\/   pshared &#061; 0  \u2192 \u7ebf\u7a0b\u95f4\u5171\u4eab&#xff08;\u6211\u4eec\u4eca\u5929\u7528\u7684&#xff09;<\/span><br \/>\n<span class=\"token comment\">\/\/   pshared \u2260 0  \u2192 \u8fdb\u7a0b\u95f4\u5171\u4eab<\/span><br \/>\n<span class=\"token comment\">\/\/   value        \u2192 \u4fe1\u53f7\u91cf\u521d\u59cb\u503c&#xff08;\u8ba1\u6570\u5668\u521d\u503c&#xff09;<\/span><\/p>\n<p><span class=\"token keyword\">int<\/span> <span class=\"token function\">sem_destroy<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">sem_t<\/span> <span class=\"token operator\">*<\/span>sem<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>                  <span class=\"token comment\">\/\/ \u2462 \u9500\u6bc1<\/span><\/p>\n<p><span class=\"token keyword\">int<\/span> <span class=\"token function\">sem_wait<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">sem_t<\/span> <span class=\"token operator\">*<\/span>sem<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>                     <span class=\"token comment\">\/\/ \u2463 P \u64cd\u4f5c&#xff1a;\u503c\u51cf 1<\/span><br \/>\n<span class=\"token keyword\">int<\/span> <span class=\"token function\">sem_post<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">sem_t<\/span> <span class=\"token operator\">*<\/span>sem<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>                     <span class=\"token comment\">\/\/ \u2464 V \u64cd\u4f5c&#xff1a;\u503c\u52a0 1<\/span><\/p>\n<h4>10.1 \u4e09\u4e2a\u8981\u70b9<\/h4>\n<ul>\n<li>sem_t \u5c31\u662f\u4e00\u4e2a\u7c7b\u578b&#xff0c;\u8ddf pthread_mutex_t \u4e00\u6837&#xff0c;\u5b9a\u4e49\u4e00\u4e2a\u5bf9\u8c61\u5c31\u662f\u4e00\u4e2a\u4fe1\u53f7\u91cf<\/li>\n<li>sem_wait &#061; P&#xff0c;sem_post &#061; V \u2014\u2014 \u540d\u5b57\u4e0d\u540c&#xff0c;\u8bed\u4e49\u5b8c\u5168\u4e00\u6837<\/li>\n<li>\u8fd4\u56de\u503c&#xff1a;0 \u8868\u793a\u6210\u529f&#xff0c;\u975e 0 \u8868\u793a\u5931\u8d25<\/li>\n<\/ul>\n<h4>10.2 \u4e3a\u4ec0\u4e48\u53ea\u9700\u8981\u91cd\u70b9\u5b66 POSIX \u8fd9\u4e00\u5957<\/h4>\n<p>\u201cPOSIX \u4fe1\u53f7\u91cf\u4e3b\u8981\u7528\u6765\u505a\u7ebf\u7a0b\u95f4\u7684\u540c\u6b65\u3002\u5f53\u7136\u5982\u679c\u4f60\u60f3\u505a\u5230\u8fdb\u7a0b\u95f4\u540c\u6b65\u4e5f\u53ef\u4ee5 \u2014\u2014 \u7528\u5171\u4eab\u5185\u5b58&#xff0c;\u628a\u90a3\u4e2a\u5730\u5740\u5f3a\u8f6c\u6210 sem_t* \u7c7b\u578b&#xff0c;\u7528 sem_init \u53bb\u521d\u59cb\u5316\u5c31\u884c\u3002\u201d<\/p>\n<p>\u6240\u4ee5 System V \u90a3\u4e00\u5957\u53ef\u4ee5\u4e0d\u770b\u4e86&#xff0c;POSIX \u8fd9\u5957\u65e2\u80fd\u505a\u7ebf\u7a0b\u4e5f\u80fd\u505a\u8fdb\u7a0b&#xff08;\u914d\u5408\u5171\u4eab\u5185\u5b58&#xff09;\u3002<\/p>\n<h4>10.3 sem_wait \u5185\u90e8\u5e72\u4e86\u4ec0\u4e48<\/h4>\n<p><span class=\"token function\">sem_wait<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>sem<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/ \u5e95\u5c42&#xff1a;sem \u7684\u503c &#8212;<\/span><br \/>\n<span class=\"token comment\">\/\/   \u251c\u2500 \u51cf\u5b8c &gt;&#061; 0 \u2192 \u7533\u8bf7\u6210\u529f&#xff0c;\u7ee7\u7eed\u5f80\u540e\u8d70<\/span><br \/>\n<span class=\"token comment\">\/\/   \u2514\u2500 \u51cf\u5b8c &lt;  0 \u2192 \u5f53\u524d\u7ebf\u7a0b\u88ab\u3010\u963b\u585e\u3011&#xff0c;\u76f4\u5230\u4fe1\u53f7\u91cf\u503c\u91cd\u65b0 &gt; 0 \u624d\u88ab\u5524\u9192<\/span><\/p>\n<p>&#x1f4a1; \u8fd9\u5c31\u662f&#034;\u9884\u8ba2&#034;&#xff1a;sem_wait \u8fd4\u56de\u4e86&#xff0c;\u8bf4\u660e\u8d44\u6e90\u4e00\u5b9a\u6709\u4f60\u7684&#xff1b;sem_wait \u963b\u585e\u4e86&#xff0c;\u8bf4\u660e\u8d44\u6e90\u8fd8\u6ca1\u5230\u4f4d&#xff0c;\u4f60\u8fde\u95e8\u90fd\u4e0d\u7528\u8fdb\u3002<\/p>\n<hr \/>\n<h3>\u5341\u4e00\u3001\u628a\u4fe1\u53f7\u91cf\u5c01\u88c5\u6210 Sem<\/h3>\n<p><span class=\"token comment\">\/\/ Sem.hpp<\/span><br \/>\n<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;semaphore.h&gt;<\/span><\/span><\/p>\n<p><span class=\"token keyword\">namespace<\/span> SemModule<br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">const<\/span> <span class=\"token keyword\">int<\/span> default_value <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token keyword\">class<\/span> <span class=\"token class-name\">Sem<\/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 function\">Sem<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> value <span class=\"token operator\">&#061;<\/span> default_value<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span> <span class=\"token function\">sem_init<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>_sem<span class=\"token punctuation\">,<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">,<\/span> value<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token punctuation\">}<\/span>   <span class=\"token comment\">\/\/ pshared &#061; 0<\/span><br \/>\n        <span class=\"token keyword\">void<\/span> <span class=\"token function\">P<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span> <span class=\"token function\">sem_wait<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>_sem<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token punctuation\">}<\/span><br \/>\n        <span class=\"token keyword\">void<\/span> <span class=\"token function\">V<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span> <span class=\"token function\">sem_post<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>_sem<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\">Sem<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span>   <span class=\"token punctuation\">{<\/span> <span class=\"token function\">sem_destroy<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>_sem<span class=\"token punctuation\">)<\/span><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        sem_t _sem<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u2714 \u5c01\u88c5\u5b8c\u5c31\u4e09\u4ef6\u4e8b&#xff1a;\u6784\u9020\u7ed9\u521d\u503c\u3001P()\u3001V()\u3002<br \/>\n\u2714 \u4e4b\u540e\u5728\u73af\u5f62\u961f\u5217\u91cc\u76f4\u63a5\u7528 Sem \u5bf9\u8c61&#xff0c;\u4e0d\u7528\u518d\u78b0\u88f8\u63a5\u53e3\u3002<\/p>\n<hr \/>\n<h3>\u5341\u4e8c\u3001\u4ee3\u7801 v1&#xff1a;\u5355\u751f\u4ea7\u5355\u6d88\u8d39\u7684\u73af\u5f62\u961f\u5217<\/h3>\n<h4>12.1 \u5148\u770b\u4e0a\u5c42\u600e\u4e48\u7528&#xff08;\u81ea\u4e0a\u800c\u4e0b\u8bbe\u8ba1&#xff09;<\/h4>\n<p>SemModule<span class=\"token double-colon punctuation\">::<\/span>Sem <span class=\"token function\">blank_sem<\/span><span class=\"token punctuation\">(<\/span>cap<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>    <span class=\"token comment\">\/\/ \u6216\u8005\u76f4\u63a5\u5728 RingQueue \u5185\u90e8\u6784\u9020<\/span><br \/>\nRingQueue<span class=\"token operator\">&lt;<\/span><span class=\"token keyword\">int<\/span><span class=\"token operator\">&gt;<\/span> <span class=\"token operator\">*<\/span>rq <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token generic-function\"><span class=\"token function\">RingQueue<\/span><span class=\"token generic class-name\"><span class=\"token operator\">&lt;<\/span><span class=\"token keyword\">int<\/span><span class=\"token operator\">&gt;<\/span><\/span><\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">5<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token function\">pthread_create<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>p<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">nullptr<\/span><span class=\"token punctuation\">,<\/span> producer<span class=\"token punctuation\">,<\/span> rq<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>      <span class=\"token comment\">\/\/ \u4e00\u4e2a\u751f\u4ea7\u8005<\/span><br \/>\n<span class=\"token function\">pthread_create<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>c<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">nullptr<\/span><span class=\"token punctuation\">,<\/span> consumer<span class=\"token punctuation\">,<\/span> rq<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>      <span class=\"token comment\">\/\/ \u4e00\u4e2a\u6d88\u8d39\u8005<\/span><\/p>\n<p>\u751f\u4ea7\u8005\u5f80\u73af\u5f62\u961f\u5217\u91cc Enqueue&#xff0c;\u6d88\u8d39\u8005\u4ece\u91cc\u9762 Pop \u2014\u2014 \u4e24\u79cd\u89d2\u8272\u3001\u4e00\u4e2a\u4ea4\u6613\u573a\u6240&#xff0c;\u5168\u9f50\u4e86\u3002<\/p>\n<h4>12.2 RingQueue \u7684\u6210\u5458<\/h4>\n<p><span class=\"token keyword\">template<\/span> <span class=\"token operator\">&lt;<\/span><span class=\"token keyword\">typename<\/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><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token keyword\">private<\/span><span class=\"token operator\">:<\/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> _ring_queue<span class=\"token punctuation\">;<\/span>   <span class=\"token comment\">\/\/ \u5e95\u5c42\u5bb9\u5668&#xff1a;\u56fa\u5b9a\u957f\u5ea6\u6570\u7ec4&#xff08;\u4e0d\u662f queue&#xff01;&#xff09;<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> _cap<span class=\"token punctuation\">;<\/span>                     <span class=\"token comment\">\/\/ \u5bb9\u91cf\u4e0a\u9650 N<\/span><\/p>\n<p>    Sem _blank_sem<span class=\"token punctuation\">;<\/span>               <span class=\"token comment\">\/\/ \u751f\u4ea7\u8005\u5173\u5fc3&#xff1a;\u7a7a\u4f4d\u7f6e\u6570\u91cf&#xff0c;\u521d\u503c &#061; N<\/span><br \/>\n    Sem _data_sem<span class=\"token punctuation\">;<\/span>                <span class=\"token comment\">\/\/ \u6d88\u8d39\u8005\u5173\u5fc3&#xff1a;\u6570\u636e\u6570\u91cf&#xff0c;  \u521d\u503c &#061; 0<\/span><\/p>\n<p>    <span class=\"token keyword\">int<\/span> _p_step<span class=\"token punctuation\">;<\/span>                  <span class=\"token comment\">\/\/ \u751f\u4ea7\u8005\u4e0b\u6807&#xff0c;\u521d\u503c 0<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> _c_step<span class=\"token punctuation\">;<\/span>                  <span class=\"token comment\">\/\/ \u6d88\u8d39\u8005\u4e0b\u6807&#xff0c;\u521d\u503c 0<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>&#x1f4a1; \u4e3a\u4ec0\u4e48\u7528 vector \u800c\u4e0d\u662f queue&#xff1f; \u56e0\u4e3a\u8981\u80fd\u6309\u4e0b\u6807\u968f\u673a\u8bbf\u95ee&#xff08;_ring_queue[i] &#061; in&#xff09;\u3002<br \/>\nqueue \u53ea\u80fd push \/ front \/ pop&#xff0c;\u62ff\u4e0d\u5230\u4e0b\u6807&#xff0c;\u4e5f\u505a\u4e0d\u4e86&#034;\u4e24\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u4e0d\u540c\u4f4d\u7f6e&#034;\u8fd9\u4ef6\u4e8b \u2014\u2014 \u8fd9\u6b63\u662f\u5b83\u53ea\u80fd\u6574\u4f53\u4f7f\u7528\u7684\u539f\u56e0\u3002<\/p>\n<h4>12.3 \u4e24\u4e2a\u63a5\u53e3&#xff08;v1&#xff1a;\u65e0\u9501&#xff09;<\/h4>\n<p><span class=\"token keyword\">void<\/span> <span class=\"token function\">Enqueue<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">const<\/span> T <span class=\"token operator\">&amp;<\/span>in<span class=\"token punctuation\">)<\/span>              <span class=\"token comment\">\/\/ \u751f\u4ea7\u8005<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n    _blank_sem<span class=\"token punctuation\">.<\/span><span class=\"token function\">P<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>                    <span class=\"token comment\">\/\/ \u2460 \u7533\u8bf7\u7a7a\u4f4d&#xff08;\u6ca1\u6709\u5c31\u963b\u585e&#xff09;<\/span><br \/>\n    _ring_queue<span class=\"token punctuation\">[<\/span>_p_step<span class=\"token operator\">&#043;&#043;<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> in<span class=\"token punctuation\">;<\/span>       <span class=\"token comment\">\/\/ \u2461 \u4e00\u5b9a\u6709\u4f4d\u7f6e&#xff0c;\u76f4\u63a5\u751f\u4ea7<\/span><br \/>\n    _p_step <span class=\"token operator\">%&#061;<\/span> _cap<span class=\"token punctuation\">;<\/span>                   <span class=\"token comment\">\/\/ \u2462 \u7ef4\u6301\u73af\u5f62\u7279\u6027<\/span><br \/>\n    _data_sem<span class=\"token punctuation\">.<\/span><span class=\"token function\">V<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>                     <span class=\"token comment\">\/\/ \u2463 \u6570\u636e &#043;1&#xff0c;\u901a\u77e5\u6d88\u8d39\u8005<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token keyword\">void<\/span> <span class=\"token function\">Pop<\/span><span class=\"token punctuation\">(<\/span>T <span class=\"token operator\">*<\/span>out<span class=\"token punctuation\">)<\/span>                       <span class=\"token comment\">\/\/ \u6d88\u8d39\u8005<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n    _data_sem<span class=\"token punctuation\">.<\/span><span class=\"token function\">P<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>                     <span class=\"token comment\">\/\/ \u2460 \u7533\u8bf7\u6570\u636e&#xff08;\u6ca1\u6709\u5c31\u963b\u585e&#xff09;<\/span><br \/>\n    <span class=\"token operator\">*<\/span>out <span class=\"token operator\">&#061;<\/span> _ring_queue<span class=\"token punctuation\">[<\/span>_c_step<span class=\"token operator\">&#043;&#043;<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span>     <span class=\"token comment\">\/\/ \u2461 \u4e00\u5b9a\u6709\u6570\u636e&#xff0c;\u76f4\u63a5\u53d6\u8d70<\/span><br \/>\n    _c_step <span class=\"token operator\">%&#061;<\/span> _cap<span class=\"token punctuation\">;<\/span>                   <span class=\"token comment\">\/\/ \u2462 \u7ef4\u6301\u73af\u5f62\u7279\u6027<\/span><br \/>\n    _blank_sem<span class=\"token punctuation\">.<\/span><span class=\"token function\">V<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>                    <span class=\"token comment\">\/\/ \u2463 \u7a7a\u4f4d &#043;1&#xff0c;\u901a\u77e5\u751f\u4ea7\u8005<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h4>12.4 \u5355\u751f\u4ea7\u5355\u6d88\u8d39\u4e3a\u4ec0\u4e48\u4e0d\u9700\u8981\u52a0\u9501<\/h4>\n<p>\u201c\u4ed6\u4eec\u4e24\u4e2a\u4ece\u751f\u4ea7\u5230\u6d88\u8d39\u7684\u8fc7\u7a0b\u90fd\u662f\u539f\u5b50\u7684&#xff0c;\u6ca1\u6709\u4eba\u5e72\u6270\u6211 \u2014\u2014 \u5c24\u5176\u662f\u5355\u751f\u4ea7\u5355\u6d88\u8d39\u3002\u53ea\u6709\u5f53\u5b83\u4fe9\u8bbf\u95ee\u540c\u4e00\u4e2a\u4f4d\u7f6e\u65f6\u624d\u4f1a\u51fa\u73b0\u7ade\u4e89&#xff0c;\u90a3\u4e00\u523b\u7528\u4fe1\u53f7\u91cf\u6765\u4fdd\u8bc1\u4e92\u65a5\u4e0e\u540c\u6b65&#xff1b;\u4e0d\u8bbf\u95ee\u540c\u4e00\u4f4d\u7f6e\u65f6&#xff0c;\u6211\u4eec\u4e92\u4e0d\u5e72\u6270\u3002\u201d<\/p>\n<p>\u800c\u4ec0\u4e48\u65f6\u5019\u4f1a\u8bbf\u95ee\u540c\u4e00\u4e2a\u4f4d\u7f6e&#xff1f;\u53ea\u6709\u4e3a\u7a7a\u6216\u4e3a\u6ee1\u7684\u65f6\u5019 \u2014\u2014 \u800c\u90a3\u4e24\u4e2a\u65f6\u523b&#xff0c;\u4fe1\u53f7\u91cf\u5df2\u7ecf\u628a\u5176\u4e2d\u4e00\u4e2a\u7ebf\u7a0b\u6321\u4f4f\u4e86&#xff08;sem \u503c\u4e3a 0&#xff0c;P \u4e0d\u8fc7\u53bb&#xff09;\u3002\u6240\u4ee5\u51b2\u7a81\u6839\u672c\u4e0d\u4f1a\u53d1\u751f\u3002<\/p>\n<h4>12.5 \u8dd1\u8d77\u6765\u770b\u73b0\u8c61<\/h4>\n<p>\u5b9e\u9a8c\u4e00&#xff1a;\u751f\u4ea7\u8005\u5feb\u3001\u6d88\u8d39\u8005\u6162&#xff08;\u6bcf 2 \u79d2\u6d88\u8d39\u4e00\u4e2a&#xff09;<\/p>\n<p>\u751f\u4ea7 1  2  3  4  5        \u2190 \u4e00\u77ac\u95f4\u628a\u961f\u5217\u6253\u6ee1&#xff08;cap &#061; 5&#xff09;<br \/>\n&#xff08;\u751f\u4ea7\u8005\u963b\u585e&#xff1a;blank_sem \u5df2\u7ecf 0&#xff09;<br \/>\n\u6d88\u8d39 1                    \u2190 2 \u79d2\u540e<br \/>\n\u751f\u4ea7 6                    \u2190 \u7a7a\u51fa\u4e00\u4e2a\u4f4d\u7f6e&#xff0c;\u7acb\u523b\u8865\u4e0a<br \/>\n\u6d88\u8d39 2<br \/>\n\u751f\u4ea7 7<br \/>\n\u6d88\u8d39 3<br \/>\n&#8230;<\/p>\n<p>\u2714 \u73b0\u8c61&#xff1a;\u4e00\u77ac\u95f4\u62c9\u6ee1&#xff0c;\u4e4b\u540e&#034;\u6d88\u8d39\u4e00\u4e2a\u3001\u751f\u4ea7\u4e00\u4e2a&#034;&#xff0c;\u4e25\u683c\u4ea4\u66ff \u2014\u2014 \u8fd9\u5c31\u662f\u540c\u6b65\u88ab\u53ef\u89c6\u5316\u4e86\u3002<\/p>\n<p>\u5b9e\u9a8c\u4e8c&#xff1a;\u751f\u4ea7\u8005\u6162\u3001\u6d88\u8d39\u8005\u5feb<\/p>\n<p>\u6d88\u8d39\u8005\u4e00\u4e0a\u6765\u5c31 P(data_sem) \u2192 \u503c\u4e3a 0 \u2192 \u76f4\u63a5\u963b\u585e<br \/>\n\u751f\u4ea7 1 \u2192 \u6d88\u8d39 1 \u2192 \u751f\u4ea7 2 \u2192 \u6d88\u8d39 2 &#8230;<\/p>\n<p>\u2714 \u73b0\u8c61&#xff1a;\u751f\u4ea7\u8005&#034;\u751f\u4ea7\u4e00\u4e2a&#034;&#xff0c;\u6d88\u8d39\u8005\u624d&#034;\u80fd\u6d88\u8d39\u4e00\u4e2a&#034; \u2014\u2014 \u6d88\u8d39\u8005\u88ab\u7a7a\u961f\u5217\u5361\u4f4f\u4e86\u3002<\/p>\n<p>&#x1f4a1; \u8fd9\u4e24\u7ec4\u5b9e\u9a8c\u7684\u552f\u4e00\u76ee\u7684&#xff1a;\u8ba9\u4f60\u8089\u773c\u770b\u5230\u540c\u6b65\u3002\u771f\u5b9e\u9ad8\u5e76\u53d1\u8dd1\u8d77\u6765&#xff0c;\u4e24\u4e2a\u7ebf\u7a0b\u5c31\u662f\u5404\u79cd\u4ea4\u53c9&#xff0c;\u770b\u4e0d\u51fa\u6765\u4e86\u3002<\/p>\n<hr \/>\n<h3>\u5341\u4e09\u3001\u2605 \u5148\u52a0\u9501\u8fd8\u662f\u5148 P&#xff1f;\u2014\u2014\u7535\u5f71\u9662\u4e70\u7968\u7684\u7b2c\u4e8c\u8bfe<\/h3>\n<p>\u5355\u751f\u4ea7\u5355\u6d88\u8d39\u4e0d\u9700\u8981\u9501\u3002\u4f46\u4e00\u65e6\u6269\u5230\u591a\u751f\u4ea7\u591a\u6d88\u8d39&#xff0c;\u5c31\u5fc5\u987b\u52a0\u9501\u3002\u4e8e\u662f\u95ee\u9898\u6765\u4e86&#xff1a;<\/p>\n<p><span class=\"token comment\">\/\/ \u5199\u6cd5 1&#xff1a;\u5148\u52a0\u9501&#xff0c;\u518d P<\/span><br \/>\nmutex<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 \/>\nsem<span class=\"token punctuation\">.<\/span><span class=\"token function\">P<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><br \/>\nsem<span class=\"token punctuation\">.<\/span><span class=\"token function\">V<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\nmutex<span class=\"token punctuation\">.<\/span><span class=\"token function\">Unlock<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u5199\u6cd5 2&#xff1a;\u5148 P&#xff0c;\u518d\u52a0\u9501<\/span><br \/>\nsem<span class=\"token punctuation\">.<\/span><span class=\"token function\">P<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\nmutex<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 punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><br \/>\nmutex<span class=\"token punctuation\">.<\/span><span class=\"token function\">Unlock<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\nsem<span class=\"token punctuation\">.<\/span><span class=\"token function\">V<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<h4>13.1 \u5148\u52a0\u9501\u9519\u5417&#xff1f;\u2014\u2014 \u4e0d\u9519&#xff0c;\u4f46\u4f4e\u6548<\/h4>\n<p>\u56e0\u4e3a\u52a0\u9501\u5c31\u662f\u5728\u7ef4\u62a4\u4e92\u65a5\u5173\u7cfb&#xff1a;\u591a\u4e2a\u751f\u4ea7\u8005\u5148\u81ea\u5df1\u4e89\u51fa\u4e00\u4e2a\u80dc\u5229\u8005&#xff0c;\u518d\u7531\u8fd9\u4e2a\u80dc\u5229\u8005\u53bb\u548c\u6d88\u8d39\u8005\u7ade\u4e89 \u2014\u2014 \u903b\u8f91\u4e0a\u6ca1\u6bdb\u75c5\u3002<\/p>\n<h4>13.2 \u4f46\u6548\u7387\u5dee\u5728\u54ea&#xff1a;\u770b\u7535\u5f71\u7684\u4e24\u79cd\u6392\u961f\u65b9\u5f0f<\/h4>\n<p>\u65b9\u5f0f A&#xff08;\u5148\u52a0\u9501&#xff09;&#xff1a;\u6240\u6709\u4eba\u5728\u7535\u5f71\u9662\u95e8\u53e3\u6392\u961f&#xff0c;\u4e00\u4e2a\u4e2a\u8fdb\u53bb\u68c0\u7968&#xff0c;\u8f6e\u5230\u4f60\u624d\u638f\u624b\u673a\u4e70\u7968\u3002\u4e70\u5230\u4e86\u8fdb\u53bb&#xff0c;\u4e70\u5931\u8d25\u4e86\u5927\u5bb6\u4e00\u8d77\u7b49\u3002<\/p>\n<p>\u65b9\u5f0f B&#xff08;\u5148 P&#xff09;&#xff1a;\u6240\u6709\u4eba\u5148\u628a\u7968\u62a2\u4e86&#xff0c;\u6ca1\u62a2\u5230\u7968\u7684\u4eba\u8fde\u6392\u961f\u7684\u8d44\u683c\u90fd\u6ca1\u6709&#xff1b;\u62a2\u5230\u7968\u7684\u4eba\u518d\u4f9d\u6b21\u5728\u95e8\u53e3\u6392\u961f\u5165\u573a\u3002<\/p>\n<p>\u54ea\u79cd\u6548\u7387\u9ad8&#xff1f;\u660e\u663e\u662f B\u3002<\/p>\n<h4>13.3 \u4e3a\u4ec0\u4e48 B \u66f4\u5feb<\/h4>\n<table>\n<tr>\u5148\u52a0\u9501&#xff08;A&#xff09;\u5148 P&#xff08;B&#xff09;<\/tr>\n<tbody>\n<tr>\n<td>\u9501\u7684\u4f5c\u7528\u8303\u56f4<\/td>\n<td>\u5305\u542b\u4e86&#034;\u7533\u8bf7\u8d44\u6e90&#034;\u8fd9\u4e00\u6b65<\/td>\n<td>\u53ea\u5305\u542b&#034;\u8bbf\u95ee\u8d44\u6e90&#034;\u8fd9\u4e00\u6b65<\/td>\n<\/tr>\n<tr>\n<td>\u6392\u961f\u7b49\u5f85\u7684\u7ebf\u7a0b\u5728\u5e72\u4ec0\u4e48<\/td>\n<td>\u5e72\u7b49&#xff0c;\u4ec0\u4e48\u90fd\u6ca1\u505a<\/td>\n<td>\u5df2\u7ecf\u5728\u539f\u5b50\u5730\u7533\u8bf7\u4fe1\u53f7\u91cf&#xff0c;\u628a\u8d44\u6e90\u51c6\u5907\u597d\u4e86<\/td>\n<\/tr>\n<tr>\n<td>\u5173\u952e\u5dee\u5f02<\/td>\n<td>\u62a2\u5230\u9501\u7684\u4eba\u6162\u6162\u7533\u8bf7\u8d44\u6e90&#xff0c;\u5176\u4ed6\u4eba\u5e72\u7b49<\/td>\n<td>\u8d44\u6e90\u88ab\u4e00\u6b21\u6027\u74dc\u5206&#xff0c;\u5927\u5bb6\u624b\u6301\u95e8\u7968\u6392\u961f<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u2714 \u800c\u4e14\u4fe1\u53f7\u91cf\u4e0d\u4f1a\u8d85\u7533\u8bf7&#xff1a;sem \u521d\u503c\u662f\u51e0&#xff0c;\u6700\u591a\u5c31\u653e\u51e0\u4e2a\u7ebf\u7a0b\u8fdb\u6765\u3002\u8fd9\u4e2a&#034;\u9650\u6d41&#034;\u662f\u539f\u5b50\u5b8c\u6210\u7684&#xff0c;\u4e0d\u9700\u8981\u9501\u4fdd\u62a4\u3002<\/p>\n<p>\u8001\u5e08\u7ed3\u8bba&#xff1a;\u5148\u7528\u4fe1\u53f7\u91cf\u628a\u8d44\u6e90\u74dc\u5206\u6389&#xff0c;\u518d\u8ba9\u62ff\u5230\u8d44\u6e90\u7684\u4eba\u4e00\u4e2a\u4e2a\u8fdb\u6765\u3002\u8c01\u62ff\u7740\u4fe1\u53f7\u91cf&#xff0c;\u8c01\u5c31\u4e00\u5b9a\u80fd\u8bbf\u95ee&#xff1b;\u8bbf\u95ee\u8fc7\u7a0b\u4e00\u5b9a\u662f\u8fde\u7eed\u7684&#xff0c;\u56e0\u4e3a\u52a0\u9501\u4e86&#xff0c;\u6ca1\u4eba\u6253\u6270\u4f60\u3002<\/p>\n<h4>13.4 \u7ed3\u8bba<\/h4>\n<p>\u6b63\u786e\u987a\u5e8f&#xff1a;\u5148 P&#xff08;\u9884\u5b9a\u8d44\u6e90&#xff09;&#xff0c;\u540e Lock&#xff08;\u6392\u961f\u8fdb\u5165&#xff09;\u3002<\/p>\n<p>&#x1f4a1; \u6362\u4e2a\u8bf4\u6cd5\u5c31\u662f\u672c\u6587\u7b2c\u56db\u8282\u7684\u90a3\u53e5\u8bdd&#xff1a;\u628a&#034;\u8d44\u6e90\u591f\u4e0d\u591f&#034;\u7684\u5224\u5b9a&#xff0c;\u63d0\u524d\u5230\u8bbf\u95ee\u4e34\u754c\u8d44\u6e90\u4e4b\u524d\u3002<\/p>\n<hr \/>\n<h3>\u5341\u56db\u3001\u4ee3\u7801 v2&#xff1a;\u591a\u751f\u4ea7\u591a\u6d88\u8d39&#xff0c;\u6539\u4e86\u54ea\u91cc<\/h3>\n<h4>14.1 \u6307\u5bfc\u601d\u60f3&#xff1a;\u7f3a\u4ec0\u4e48\u8865\u4ec0\u4e48<\/h4>\n<p>\u6309 321 \u539f\u5219\u76d8\u70b9\u4e00\u4e0b v1 \u5df2\u7ecf\u7ef4\u62a4\u4e86\u54ea\u4e9b\u5173\u7cfb&#xff1a;<\/p>\n<table>\n<tr>\u5173\u7cfbv1&#xff08;\u5355\u751f\u4ea7\u5355\u6d88\u8d39&#xff09;v2&#xff08;\u591a\u751f\u4ea7\u591a\u6d88\u8d39&#xff09;<\/tr>\n<tbody>\n<tr>\n<td>\u751f\u4ea7\u8005 \u2014 \u751f\u4ea7\u8005<\/td>\n<td>\u4e0d\u5b58\u5728&#xff08;\u53ea\u6709\u4e00\u4e2a&#xff09;<\/td>\n<td>\u2716 \u7f3a&#xff0c;\u8981\u52a0\u9501<\/td>\n<\/tr>\n<tr>\n<td>\u6d88\u8d39\u8005 \u2014 \u6d88\u8d39\u8005<\/td>\n<td>\u4e0d\u5b58\u5728&#xff08;\u53ea\u6709\u4e00\u4e2a&#xff09;<\/td>\n<td>\u2716 \u7f3a&#xff0c;\u8981\u52a0\u9501<\/td>\n<\/tr>\n<tr>\n<td>\u751f\u4ea7\u8005 \u2014 \u6d88\u8d39\u8005<\/td>\n<td>\u2714 \u4fe1\u53f7\u91cf\u5df2\u4fdd\u8bc1<\/td>\n<td>\u2714 \u4e0d\u53d8<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u201c\u5f53\u4f60\u660e\u767d\u4e86\u751f\u4ea7\u8005\u6d88\u8d39\u8005\u6a21\u578b&#xff0c;\u4f60\u73b0\u5728\u5c31\u77e5\u9053\u4ee3\u7801\u91cc\u7f3a\u4ec0\u4e48 \u2014\u2014 \u7f3a\u751f\u4ea7\u8005\u548c\u751f\u4ea7\u8005\u4e4b\u95f4\u3001\u6d88\u8d39\u8005\u548c\u6d88\u8d39\u8005\u4e4b\u95f4\u7684\u4e92\u65a5\u5173\u7cfb\u3002\u201d<\/p>\n<h4>14.2 \u52a0\u4e24\u628a\u9501<\/h4>\n<p>Mutex _p_mutex<span class=\"token punctuation\">;<\/span>      <span class=\"token comment\">\/\/ \u7ef4\u62a4\u3010\u751f\u4ea7\u8005\u4e4b\u95f4\u3011\u7684\u4e92\u65a5<\/span><br \/>\nMutex _c_mutex<span class=\"token punctuation\">;<\/span>      <span class=\"token comment\">\/\/ \u7ef4\u62a4\u3010\u6d88\u8d39\u8005\u4e4b\u95f4\u3011\u7684\u4e92\u65a5<\/span><\/p>\n<p>\u2714 \u4e3a\u4ec0\u4e48\u662f\u4e24\u628a\u800c\u4e0d\u662f\u4e00\u628a&#xff1f;<br \/>\n\u4e00\u628a\u4e5f\u80fd\u4fdd\u8bc1\u4e92\u65a5&#xff0c;\u4f46\u4f1a\u628a\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\u4e92\u76f8\u4e32\u8d77\u6765&#xff08;\u751f\u4ea7\u8005\u6301\u9501\u65f6\u6d88\u8d39\u8005\u4e5f\u8fdb\u4e0d\u6765&#xff09;&#xff0c;\u5e76\u53d1\u5ea6\u767d\u767d\u964d\u4f4e\u3002\u5206\u6210\u4e24\u628a&#xff0c;\u5404\u7ba1\u5404\u7684\u3002<\/p>\n<h4>14.3 \u6700\u7ec8\u7248\u4ee3\u7801<\/h4>\n<p><span class=\"token keyword\">void<\/span> <span class=\"token function\">Enqueue<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">const<\/span> T <span class=\"token operator\">&amp;<\/span>in<span class=\"token punctuation\">)<\/span>                  <span class=\"token comment\">\/\/ \u751f\u4ea7\u8005<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n    _blank_sem<span class=\"token punctuation\">.<\/span><span class=\"token function\">P<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>                        <span class=\"token comment\">\/\/ \u2460 \u5148\u74dc\u5206\u8d44\u6e90&#xff08;\u539f\u5b50\u5224\u5b9a&#xff09;<\/span><br \/>\n    _p_mutex<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\">\/\/ \u2461 \u518d\u6392\u961f&#xff08;\u751f\u4ea7\u8005\u4e4b\u95f4\u4e92\u65a5&#xff09;<\/span><br \/>\n    _ring_queue<span class=\"token punctuation\">[<\/span>_p_step<span class=\"token operator\">&#043;&#043;<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> in<span class=\"token punctuation\">;<\/span>           <span class=\"token comment\">\/\/ \u2462 \u751f\u4ea7&#xff08;\u4e00\u5b9a\u6709\u4f4d\u7f6e&#xff09;<\/span><br \/>\n    _p_step <span class=\"token operator\">%&#061;<\/span> _cap<span class=\"token punctuation\">;<\/span><br \/>\n    _p_mutex<span class=\"token punctuation\">.<\/span><span class=\"token function\">Unlock<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    _data_sem<span class=\"token punctuation\">.<\/span><span class=\"token function\">V<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>                         <span class=\"token comment\">\/\/ \u2463 \u901a\u77e5\u6d88\u8d39\u8005<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token keyword\">void<\/span> <span class=\"token function\">Pop<\/span><span class=\"token punctuation\">(<\/span>T <span class=\"token operator\">*<\/span>out<span class=\"token punctuation\">)<\/span>                           <span class=\"token comment\">\/\/ \u6d88\u8d39\u8005<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n    _data_sem<span class=\"token punctuation\">.<\/span><span class=\"token function\">P<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>                         <span class=\"token comment\">\/\/ \u2460 \u5148\u74dc\u5206\u8d44\u6e90<\/span><br \/>\n    _c_mutex<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\">\/\/ \u2461 \u518d\u6392\u961f&#xff08;\u6d88\u8d39\u8005\u4e4b\u95f4\u4e92\u65a5&#xff09;<\/span><br \/>\n    <span class=\"token operator\">*<\/span>out <span class=\"token operator\">&#061;<\/span> _ring_queue<span class=\"token punctuation\">[<\/span>_c_step<span class=\"token operator\">&#043;&#043;<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span>         <span class=\"token comment\">\/\/ \u2462 \u6d88\u8d39&#xff08;\u4e00\u5b9a\u6709\u6570\u636e&#xff09;<\/span><br \/>\n    _c_step <span class=\"token operator\">%&#061;<\/span> _cap<span class=\"token punctuation\">;<\/span><br \/>\n    _c_mutex<span class=\"token punctuation\">.<\/span><span class=\"token function\">Unlock<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    _blank_sem<span class=\"token punctuation\">.<\/span><span class=\"token function\">V<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>                        <span class=\"token comment\">\/\/ \u2463 \u901a\u77e5\u751f\u4ea7\u8005<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>&#x1f4a1; V \u653e\u5728\u89e3\u9501\u4e4b\u524d\u8fd8\u662f\u4e4b\u540e\u90fd\u53ef\u4ee5\u3002\u8bfe\u4ef6\u91cc\u653e\u5728\u89e3\u9501\u4e4b\u540e&#xff08;\u5148 Unlock \u518d V&#xff09;&#xff0c;\u201c\u751f\u4ea7\u5b8c\u4e4b\u540e V&#xff0c;\u7136\u540e\u518d\u91ca\u653e\u9501\u201d&#xff08;\u653e\u9501\u5185&#xff09;\u2014\u2014 \u4e24\u79cd\u90fd\u5bf9&#xff0c;\u56e0\u4e3a V \u53ea\u662f\u628a\u8ba1\u6570\u5668\u52a0\u4e00&#xff0c;\u4e0d\u6d89\u53ca\u4e34\u754c\u533a\u3002<\/p>\n<hr \/>\n<h3>\u5341\u4e94\u3001\u2605 \u53cd\u5411\u7406\u89e3&#xff1a;\u4e3a\u4ec0\u4e48\u4fe1\u53f7\u91cf\u7684\u4ee3\u7801\u91cc\u5b8c\u5168\u6ca1\u6709\u5224\u65ad&#xff1f;<\/h3>\n<p>\u8fd9\u662f\u6574\u8282\u8bfe\u6700\u60f3\u8ba9\u4f60\u60f3\u901a\u7684\u4e00\u95ee\u3002<\/p>\n<p>\u628a\u4e24\u4efd\u4ee3\u7801\u6446\u5728\u4e00\u8d77\u770b&#xff1a;<\/p>\n<p><span class=\"token comment\">\/\/ \u25c0 \u4e0a\u4e00\u7ae0&#xff1a;\u6761\u4ef6\u53d8\u91cf\u7248&#xff08;\u6709\u5224\u65ad&#xff09;<\/span><br \/>\n<span class=\"token function\">pthread_mutex_lock<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>_mutex<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">while<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token function\">IsFull<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span>                              <span class=\"token comment\">\/\/ \u2190 \u5728\u3010\u4e34\u754c\u533a\u5185\u90e8\u3011\u5224\u65ad<\/span><br \/>\n    <span class=\"token function\">pthread_cond_wait<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>_product_cond<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">&amp;<\/span>_mutex<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n_block_queue<span class=\"token punctuation\">.<\/span><span class=\"token function\">push<\/span><span class=\"token punctuation\">(<\/span>in<span class=\"token punctuation\">)<\/span><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>_mutex<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u25b6 \u4eca\u5929&#xff1a;\u4fe1\u53f7\u91cf\u7248&#xff08;\u6ca1\u6709\u5224\u65ad&#xff09;<\/span><br \/>\n_blank_sem<span class=\"token punctuation\">.<\/span><span class=\"token function\">P<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>                                 <span class=\"token comment\">\/\/ \u2190 \u5224\u5b9a\u88ab\u585e\u8fdb\u4e86\u8fd9\u4e00\u53e5\u91cc<\/span><br \/>\n_ring_queue<span class=\"token punctuation\">[<\/span>_p_step<span class=\"token operator\">&#043;&#043;<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> in<span class=\"token punctuation\">;<\/span><\/p>\n<h4>15.1 \u5224\u5b9a\u53bb\u54ea\u4e86&#xff1f;<\/h4>\n<p>\u201c\u56e0\u4e3a\u4fe1\u53f7\u91cf\u672c\u6765\u5c31\u662f\u7528\u6765\u63cf\u8ff0\u4e34\u754c\u8d44\u6e90\u6570\u91cf\u7684\u3002\u4fe1\u53f7\u91cf\u7533\u8bf7\u5931\u8d25&#xff0c;\u5c31\u662f\u6761\u4ef6\u4e0d\u6ee1\u8db3\u3002\u201d<\/p>\n<table>\n<tr>\u6761\u4ef6\u53d8\u91cf\u7248\u4fe1\u53f7\u91cf\u7248<\/tr>\n<tbody>\n<tr>\n<td>\u5224\u5b9a\u8bed\u53e5<\/td>\n<td>while (IsFull())<\/td>\n<td>\u6ca1\u6709<\/td>\n<\/tr>\n<tr>\n<td>\u5224\u5b9a\u5728\u54ea<\/td>\n<td>\u9501\u5185&#xff08;\u4e34\u754c\u533a\u91cc&#xff09;<\/td>\n<td>\u9501\u5916&#xff08;P \u64cd\u4f5c\u5185\u90e8&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u5224\u5b9a\u5f62\u5f0f<\/td>\n<td>\u8bfb\u5171\u4eab\u53d8\u91cf &#043; \u6bd4\u8f83<\/td>\n<td>\u539f\u5b50\u5730\u5bf9\u8ba1\u6570\u5668\u51cf\u4e00<\/td>\n<\/tr>\n<tr>\n<td>\u963b\u585e\u65b9\u5f0f<\/td>\n<td>cond_wait&#xff08;\u6302\u5230\u6761\u4ef6\u53d8\u91cf\u961f\u5217&#xff09;<\/td>\n<td>P \u51cf\u5230\u8d1f\u6570&#xff08;\u6302\u5230\u4fe1\u53f7\u91cf\u7b49\u5f85\u961f\u5217&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u2714 \u4e24\u8005\u672c\u8d28\u5b8c\u5168\u4e00\u6837&#xff0c;\u53ea\u662f&#034;\u5224\u5b9a\u8d44\u6e90\u662f\u5426\u5c31\u7eea&#034;\u8fd9\u4ef6\u4e8b\u4ece\u9501\u5185\u642c\u5230\u4e86\u9501\u5916\u3002<\/p>\n<h4>15.2 \u8fd9\u5c31\u662f&#034;\u4fe1\u53f7\u91cf\u7684\u53e6\u4e00\u4e2a\u672c\u8d28&#034;<\/h4>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261020182452-6ad7b1f407780.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>\u4fe1\u53f7\u91cf\u628a\u5bf9\u300c\u4e34\u754c\u8d44\u6e90\u662f\u5426\u5b58\u5728\u300d\u7684\u5224\u5b9a&#xff0c;\u4ee5\u539f\u5b50\u6027\u7684\u5f62\u5f0f&#xff0c;\u5448\u73b0\u5728\u8bbf\u95ee\u4e34\u754c\u8d44\u6e90\u4e4b\u524d\u5c31\u5b8c\u6210\u4e86\u3002<\/p>\n<p>\u8fd9\u53e5\u8bdd\u662f\u672c\u7ae0\u7684\u6700\u9ad8\u603b\u7ed3\u3002\u5b83\u7684\u4e09\u4e2a\u542b\u4e49&#xff1a;<\/p>\n<li>\u5224\u5b9a\u63d0\u524d\u4e86 \u2014\u2014 \u4e0d\u7528\u5148\u8fdb\u4e34\u754c\u533a\u518d\u770b\u6709\u6ca1\u6709\u8d44\u6e90&#xff0c;\u95e8\u53e3\u5c31\u62e6\u4f4f\u4e86<\/li>\n<li>\u5224\u5b9a\u539f\u5b50\u4e86 \u2014\u2014 \u51cf\u4e00\u8fd9\u4e2a\u52a8\u4f5c\u4e0d\u53ef\u5206\u5272&#xff0c;\u4e0d\u4f1a\u51fa\u73b0\u4e24\u4e2a\u7ebf\u7a0b\u90fd\u5224\u5b9a\u6210\u529f<\/li>\n<li>\u5224\u5b9a\u548c\u8bbf\u95ee\u5206\u79bb\u4e86 \u2014\u2014 \u6240\u4ee5\u53ef\u4ee5\u5148 P \u540e\u52a0\u9501&#xff0c;\u9501\u53ea\u4fdd\u62a4&#034;\u8bbf\u95ee&#034;\u8fd9\u4e00\u6bb5<\/li>\n<h4>15.3 \u90a3\u80fd\u4e0d\u80fd\u7528\u300c\u9501 &#043; \u6761\u4ef6\u53d8\u91cf\u300d\u5b9e\u73b0\u73af\u5f62\u961f\u5217&#xff1f;<\/h4>\n<p>\u80fd&#xff0c;\u5b8c\u5168\u53ef\u4ee5\u3002 \u4f46\u6ca1\u5fc5\u8981 \u2014\u2014 \u6709\u66f4\u8d34\u5408\u573a\u666f\u7684\u5de5\u5177\u3002<\/p>\n<h4>15.4 \u4e3a\u4ec0\u4e48\u8fd9\u4e2a\u573a\u666f\u80fd\u7528\u4fe1\u53f7\u91cf&#xff0c;\u4e0a\u4e00\u4e2a\u573a\u666f\u4e0d\u80fd&#xff1f;<\/h4>\n<p>\u56de\u7b54\u662f\u5173\u952e&#xff1a;<\/p>\n<p>\u201c\u56e0\u4e3a\u4eca\u5929\u7684\u73af\u5f62\u961f\u5217&#xff0c;\u662f\u6211\u4eec\u81ea\u5df1\u5b9a\u4e49\u6210\u56fa\u5b9a\u5927\u5c0f\u7684&#xff0c;\u6211\u4eec\u81ea\u5df1\u7528\u81ea\u5df1\u7684\u4e0b\u6807\u53bb\u8bbf\u95ee&#xff0c;\u628a\u5b83\u7684\u8d44\u6e90\u770b\u6210\u4e86\u4e00\u5c0f\u5757\u4e00\u5c0f\u5757\u3002<br \/>\n\u800c\u4e0a\u4e00\u7ae0\u7684\u963b\u585e\u961f\u5217&#xff0c;\u6211\u4eec\u628a\u5b83\u770b\u6210\u4e00\u4e2a\u6574\u4f53 \u2014\u2014 \u5012\u4e0d\u662f\u8bf4\u6211\u4eec\u60f3\u628a\u5b83\u770b\u6210\u6574\u4f53&#xff0c;\u800c\u662f\u5b83\u53ea\u80fd\u88ab\u770b\u6210\u4e00\u4e2a\u6574\u4f53&#xff0c;\u56e0\u4e3a\u90a3\u4e2a\u961f\u5217\u4f1a\u81ea\u52a8\u6269\u5bb9&#xff0c;\u91cc\u9762\u8fd8\u6709\u5404\u79cd\u7c7b\u5185\u6210\u5458\u3002\u201d<\/p>\n<table>\n<tr>\u963b\u585e\u961f\u5217&#xff08;std::queue&#xff09;\u73af\u5f62\u961f\u5217&#xff08;vector &#043; \u4e0b\u6807&#xff09;<\/tr>\n<tbody>\n<tr>\n<td>\u80fd\u5426\u6309\u4e0b\u6807\u5207\u5206<\/td>\n<td>\u2716 \u4e0d\u80fd&#xff08;\u53ea\u80fd push\/pop&#xff09;<\/td>\n<td>\u2714 \u80fd&#xff08;ring[i]&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u4f1a\u4e0d\u4f1a\u81ea\u52a8\u6269\u5bb9<\/td>\n<td>\u2714 \u4f1a&#xff08;\u6269\u5bb9\u65f6 pop \u4f1a\u5931\u8d25&#xff09;<\/td>\n<td>\u2716 \u56fa\u5b9a\u957f\u5ea6<\/td>\n<\/tr>\n<tr>\n<td>\u53ea\u80fd\u6574\u4f53\u4f7f\u7528&#xff1f;<\/td>\n<td>\u2714 \u662f<\/td>\n<td>\u2716 \u4e0d\u662f<\/td>\n<\/tr>\n<tr>\n<td>\u9002\u914d\u7684\u6280\u672f<\/td>\n<td>mutex&#xff08;\u6216\u4e8c\u5143\u4fe1\u53f7\u91cf&#xff09;<\/td>\n<td>\u591a\u5143\u4fe1\u53f7\u91cf<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h3>\u5341\u516d\u3001\u9000\u5316&#xff1a;sem \u521d\u503c\u4e3a 1 \u65f6&#xff0c;\u5b83\u5c31\u662f\u4e92\u65a5\u9501<\/h3>\n<p>\u6700\u540e\u4e00\u4e2a\u601d\u8003\u9898&#xff1a;<\/p>\n<p>\u5982\u679c\u73af\u5f62\u961f\u5217\u7684\u5bb9\u91cf\u4e0d\u662f N \u800c\u662f 1&#xff0c;\u4f1a\u53d1\u751f\u4ec0\u4e48&#xff1f;<\/p>\n<p>\u7b54\u6848\u662f\u9000\u5316&#xff1a;<\/p>\n<ul>\n<li>\u6570\u7ec4\u53ea\u6709 1 \u4e2a\u5143\u7d20 \u2192 \u751f\u4ea7\u8005\u4e0b\u6807\u548c\u6d88\u8d39\u8005\u4e0b\u6807\u6c38\u8fdc\u662f\u540c\u4e00\u4e2a<\/li>\n<li>sem_blank &#061; 1\u3001sem_data &#061; 0<\/li>\n<li>\u7167\u6837 P \/ V \/ P \/ V \u8dd1&#xff0c;\u4f46\u5b83\u5df2\u7ecf\u4e0d\u662f\u5e76\u53d1\u4e86&#xff0c;\u800c\u662f\u4e32\u884c\u7684<\/li>\n<\/ul>\n<p>\u201c\u8fd9\u4e0d\u5c31\u662f\u4e92\u65a5\u9501\u4e86\u5417&#xff1f;\u8fd9\u4e0d\u5c31\u662f\u4f60\u7684\u73af\u5f62\u961f\u5217\u5f53\u4e2d\u53ea\u6709\u4e00\u5757&#xff0c;\u9000\u5316\u6210\u6211\u4eec\u4e0a\u4e00\u7ae0\u8bb2\u7684\u963b\u585e\u961f\u5217\u4e86\u5417&#xff1f;\u201d<\/p>\n<h4>16.1 \u7531\u6b64\u5f97\u5230\u7684\u7b2c\u4e8c\u4e2a\u6536\u83b7<\/h4>\n<p>\u2714 \u4e8c\u5143\u4fe1\u53f7\u91cf\u4e5f\u80fd\u5b9e\u73b0&#034;\u6574\u4f53\u4f7f\u7528&#034;\u7684\u540c\u6b65\u4e92\u65a5 \u2014\u2014 \u800c\u4e14\u5929\u7136\u5e26\u4e92\u76f8\u901a\u77e5&#xff1a;<\/p>\n<p>\u751f\u4ea7\u8005&#xff1a;P(blank&#061;1) \u2192 \u751f\u4ea7 \u2192 V(data&#061;1)<br \/>\n\u6d88\u8d39\u8005&#xff1a;P(data&#061;1)  \u2192 \u6d88\u8d39 \u2192 V(blank&#061;1)<\/p>\n<p>\u201c\u6211\u751f\u4ea7\u5b8c\u901a\u77e5\u4f60&#xff0c;\u4f60\u6d88\u8d39\u5b8c\u901a\u77e5\u6211 \u2014\u2014 \u7528\u4e8c\u5143\u4fe1\u53f7\u91cf\u5c31\u53ef\u4ee5\u5bf9\u4e00\u4e2a\u5927\u8d44\u6e90\u6574\u4f53\u4f7f\u7528&#xff0c;\u800c\u4e14\u8fd8\u80fd\u4e92\u76f8\u901a\u77e5\u3002\u201d<\/p>\n<p>\u8fd9\u6b63\u662f\u7b2c\u56db\u8282\u90a3\u5f20\u9009\u578b\u8868\u7684\u95ed\u73af&#xff1a;\u6574\u4f53\u4f7f\u7528 \u2192 mutex \/ \u4e8c\u5143\u4fe1\u53f7\u91cf&#xff1b;\u5206\u5757\u4f7f\u7528 \u2192 \u591a\u5143\u4fe1\u53f7\u91cf\u3002<\/p>\n<hr \/>\n<h3>\u5341\u4e03\u3001\u603b\u7ed3\u901f\u67e5\u8868<\/h3>\n<table>\n<tr>\u95ee\u9898\u7b54\u6848<\/tr>\n<tbody>\n<tr>\n<td>\u4fe1\u53f7\u548c\u4fe1\u53f7\u91cf\u6709\u5173\u7cfb\u5417<\/td>\n<td>\u2716 \u6ca1\u6709&#xff08;\u8001\u5a46 vs \u8001\u5a46\u997c&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u4fe1\u53f7\u91cf\u7684\u672c\u8d28<\/td>\n<td>\u4e00\u4e2a\u8ba1\u6570\u5668&#xff0c;\u662f\u5bf9\u7279\u5b9a\u8d44\u6e90\u7684\u9884\u8ba2\u673a\u5236<\/td>\n<\/tr>\n<tr>\n<td>\u7535\u5f71\u9662\u6bd4\u55bb\u4e70\u7684\u662f\u4ec0\u4e48<\/td>\n<td>\u4e70\u7968 &#061; \u9884\u8ba2&#xff1b;\u7968\u5230\u624b\u5ea7\u4f4d\u5c31\u662f\u4f60\u7684&#xff0c;\u4eba\u4e0d\u53bb\u4e5f\u7559\u7740<\/td>\n<\/tr>\n<tr>\n<td>sem &#061; 1 \u53eb\u4ec0\u4e48<\/td>\n<td>\u4e8c\u5143\u4fe1\u53f7\u91cf&#xff0c;\u672c\u8d28\u5c31\u662f\u4e92\u65a5<\/td>\n<\/tr>\n<tr>\n<td>\u8d44\u6e90\u6574\u4f53\u4f7f\u7528\u7528\u4ec0\u4e48<\/td>\n<td>mutex \u6216\u4e8c\u5143\u4fe1\u53f7\u91cf<\/td>\n<\/tr>\n<tr>\n<td>\u8d44\u6e90\u6309\u5757\u5206\u6279\u4f7f\u7528\u7528\u4ec0\u4e48<\/td>\n<td>\u591a\u5143\u4fe1\u53f7\u91cf<\/td>\n<\/tr>\n<tr>\n<td>\u4e3a\u4ec0\u4e48\u4fe1\u53f7\u91cf\u672c\u8eab\u4e5f\u662f\u4e34\u754c\u8d44\u6e90<\/td>\n<td>\u5b83\u8981\u88ab\u6240\u6709\u7ebf\u7a0b\u770b\u5230\u3001\u8981\u88ab &#043;&#043;\/&#8211;&#xff0c;\u800c &#043;&#043;\/&#8211; \u4e0d\u662f\u539f\u5b50\u7684<\/td>\n<\/tr>\n<tr>\n<td>P \/ V \u662f\u4ec0\u4e48<\/td>\n<td>P &#061; sem_wait&#xff08;&#8211;&#xff0c;\u7533\u8bf7&#xff09;\/ V &#061; sem_post&#xff08;&#043;&#043;&#xff0c;\u5f52\u8fd8&#xff09;&#xff0c;\u90fd\u5fc5\u987b\u539f\u5b50<\/td>\n<\/tr>\n<tr>\n<td>\u73af\u5f62\u961f\u5217\u7a7a\u548c\u6ee1\u7684\u9ebb\u70e6<\/td>\n<td>head &#061;&#061; tail \u5206\u4e0d\u51fa\u6765 \u2192 \u52a0 count \u6216\u7a7a\u4e00\u4e2a\u4f4d\u7f6e<\/td>\n<\/tr>\n<tr>\n<td>\u4f46\u4eca\u5929\u8fd8\u9700\u8981\u5224\u65ad\u7a7a\u6ee1\u5417<\/td>\n<td>\u2716 \u4e0d\u9700\u8981&#xff0c;\u4fe1\u53f7\u91cf\u66ff\u4ee3\u4e86\u5224\u65ad<\/td>\n<\/tr>\n<tr>\n<td>\u73af\u5f62\u961f\u5217\u7684\u56db\u6761\u7ea6\u5b9a<\/td>\n<td>\u2460 \u7a7a\u751f\u4ea7\u8005\u5148\u8dd1 \u2461 \u6ee1\u6d88\u8d39\u8005\u5148\u8dd1 \u2462 \u751f\u4ea7\u8005\u4e0d\u80fd\u5957\u6d88\u8d39\u8005\u4e00\u5708\u4ee5\u4e0a \u2463 \u6d88\u8d39\u8005\u4e0d\u80fd\u8d85\u8fc7\u751f\u4ea7\u8005<\/td>\n<\/tr>\n<tr>\n<td>\u4ec0\u4e48\u65f6\u5019\u80fd\u540c\u65f6\u8dd1<\/td>\n<td>\u4e0d\u4e3a\u7a7a &amp;&amp; \u4e0d\u4e3a\u6ee1<\/td>\n<\/tr>\n<tr>\n<td>\u4ec0\u4e48\u65f6\u5019\u8981\u540c\u6b65\u4e92\u65a5<\/td>\n<td>\u4e3a\u7a7a || \u4e3a\u6ee1<\/td>\n<\/tr>\n<tr>\n<td>\u751f\u4ea7\u8005\u5173\u5fc3\u4ec0\u4e48\u8d44\u6e90<\/td>\n<td>\u7a7a\u4f4d\u7f6e&#xff0c;sem_blank \u521d\u503c N<\/td>\n<\/tr>\n<tr>\n<td>\u6d88\u8d39\u8005\u5173\u5fc3\u4ec0\u4e48\u8d44\u6e90<\/td>\n<td>\u6570\u636e&#xff0c;sem_data \u521d\u503c 0<\/td>\n<\/tr>\n<tr>\n<td>\u5355\u751f\u4ea7\u5355\u6d88\u8d39\u8981\u52a0\u9501\u5417<\/td>\n<td>\u2716 \u4e0d\u8981&#xff0c;\u4fe1\u53f7\u91cf\u5df2\u7ecf\u591f\u4e86<\/td>\n<\/tr>\n<tr>\n<td>\u591a\u751f\u4ea7\u591a\u6d88\u8d39\u8981\u52a0\u51e0\u628a\u9501<\/td>\n<td>\u4e24\u628a&#xff08;\u751f\u4ea7\u8005\u4e00\u628a\u3001\u6d88\u8d39\u8005\u4e00\u628a&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u5148\u52a0\u9501\u8fd8\u662f\u5148 P<\/td>\n<td>\u2605 \u5148 P&#xff0c;\u540e\u52a0\u9501&#xff08;\u5148\u74dc\u5206\u8d44\u6e90\u518d\u6392\u961f&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u4e3a\u4ec0\u4e48\u4fe1\u53f7\u91cf\u4ee3\u7801\u91cc\u6ca1\u6709\u5224\u65ad<\/td>\n<td>\u7533\u8bf7\u5931\u8d25 &#061; \u6761\u4ef6\u4e0d\u6ee1\u8db3&#xff0c;\u5224\u5b9a\u88ab\u539f\u5b50\u5730\u585e\u8fdb\u4e86 P \u91cc<\/td>\n<\/tr>\n<tr>\n<td>\u4fe1\u53f7\u91cf\u628a\u4ec0\u4e48\u63d0\u524d\u4e86<\/td>\n<td>\u628a&#034;\u8d44\u6e90\u662f\u5426\u5b58\u5728&#034;\u7684\u5224\u5b9a&#xff0c;\u4ee5\u539f\u5b50\u5f62\u5f0f\u5448\u73b0\u5728\u8bbf\u95ee\u4e4b\u524d<\/td>\n<\/tr>\n<tr>\n<td>sem_blank \u521d\u503c\u6539\u6210 1 \u4f1a\u600e\u6837<\/td>\n<td>\u9000\u5316\u6210\u4e92\u65a5\u9501&#xff08;\u73af\u5f62\u961f\u5217\u53ea\u5269\u4e00\u4e2a\u683c\u5b50&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>\u5199\u4ee3\u7801\u65f6\u7684\u4e00\u6761\u4e3b\u7ebf<\/h4>\n<p>\u2460 \u8d44\u6e90\u80fd\u6309\u5757\u62c6\u5417&#xff1f;<br \/>\n   \u251c\u2500 \u80fd&#xff08;\u81ea\u5df1\u63a7\u4e0b\u6807\u3001\u56fa\u5b9a\u5927\u5c0f&#xff09;\u2192 \u7528\u4fe1\u53f7\u91cf<br \/>\n   \u2502     \u751f\u4ea7\u8005&#xff1a;P(\u7a7a\u4f4d) \u2192 \u751f\u4ea7 \u2192 V(\u6570\u636e)<br \/>\n   \u2502     \u6d88\u8d39\u8005&#xff1a;P(\u6570\u636e) \u2192 \u6d88\u8d39 \u2192 V(\u7a7a\u4f4d)<br \/>\n   \u2514\u2500 \u4e0d\u80fd&#xff08;\u5bb9\u5668\u6574\u4f53\u3001\u4f1a\u6269\u5bb9&#xff09;  \u2192 \u7528 mutex &#043; \u6761\u4ef6\u53d8\u91cf<\/p>\n<p>\u2461 \u662f\u5355\u751f\u4ea7\u5355\u6d88\u8d39\u5417&#xff1f;<br \/>\n   \u251c\u2500 \u662f \u2192 \u4fe1\u53f7\u91cf\u8db3\u591f&#xff0c;\u4e0d\u7528\u9501<br \/>\n   \u2514\u2500 \u5426 \u2192 \u518d\u8865\u4e24\u628a\u9501&#xff0c;\u7ef4\u62a4&#034;\u751f\u4ea7\u8005\u4e4b\u95f4&#034;&#034;\u6d88\u8d39\u8005\u4e4b\u95f4&#034;\u7684\u4e92\u65a5<\/p>\n<p>\u2462 \u52a0\u9501\u548c P \u8c01\u5728\u524d&#xff1f;<br \/>\n   \u2192 \u6c38\u8fdc\u3010\u5148 P \u540e\u52a0\u9501\u3011&#xff1a;\u5148\u9884\u5b9a&#xff0c;\u518d\u6392\u961f<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u5148\u56de\u987e&#xff1a;\u4fe1\u53f7\u91cf\u5230\u5e95\u662f\u4e2a\u4ec0\u4e48\u4e1c\u897f<br \/>\n1.1 \u4fe1\u53f7 \u2260 \u4fe1\u53f7\u91cf\u4fe1\u53f7\u548c\u4fe1\u53f7\u91cf&#xff0c;\u662f\u300c\u8001\u5a46\u300d\u548c\u300c\u8001\u5a46\u997c\u300d\u7684\u5173\u7cfb \u2014\u2014 \u6ca1\u6709\u4efb\u4f55\u5173\u7cfb\u3002\u4fe1\u53f7&#xff08;signal&#xff09;\u4fe1\u53f7\u91cf&#xff08;semaphore \/ \u4fe1\u53f7\u706f&#xff09;\u672c\u8d28\u4e00\u79cd\u8fdb\u7a0b\u95f4\u5f02\u6b65\u901a\u77e5\u7684\u65b9\u5f0f\u4e00\u4e2a\u8ba1\u6570\u5668\u4f5c\u7528\u901a\u77e5\u4f60\\&#8221;\u51fa\u4e8b\u4e86\\&#8221;\u8868\u660e\u4e34\u754c\u8d44\u6e90\u4e2d\u8d44\u6e90\u6570\u91cf\u8fd8\u5269\u591a\u5c11\u6bd4\u55bb\u95e8\u94c3\u505c\u8f66\u573a\u7684\u4f59\u4f4d\u663e\u793a\u5c4f1.2 POSIX \u4e0e System V<br \/>\nsem_init \/ sem_wait \/ sem_po<\/p>\n","protected":false},"author":2,"featured_media":116264,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[84],"topic":[],"class_list":["post-116269","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-android"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>POSIX\u4fe1\u53f7\u91cf\u4e0e\u73af\u5f62\u961f\u5217 - \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\/116269.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"POSIX\u4fe1\u53f7\u91cf\u4e0e\u73af\u5f62\u961f\u5217 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u4e00\u3001\u5148\u56de\u987e&#xff1a;\u4fe1\u53f7\u91cf\u5230\u5e95\u662f\u4e2a\u4ec0\u4e48\u4e1c\u897f 1.1 \u4fe1\u53f7 \u2260 \u4fe1\u53f7\u91cf\u4fe1\u53f7\u548c\u4fe1\u53f7\u91cf&#xff0c;\u662f\u300c\u8001\u5a46\u300d\u548c\u300c\u8001\u5a46\u997c\u300d\u7684\u5173\u7cfb \u2014\u2014 \u6ca1\u6709\u4efb\u4f55\u5173\u7cfb\u3002\u4fe1\u53f7&#xff08;signal&#xff09;\u4fe1\u53f7\u91cf&#xff08;semaphore \/ \u4fe1\u53f7\u706f&#xff09;\u672c\u8d28\u4e00\u79cd\u8fdb\u7a0b\u95f4\u5f02\u6b65\u901a\u77e5\u7684\u65b9\u5f0f\u4e00\u4e2a\u8ba1\u6570\u5668\u4f5c\u7528\u901a\u77e5\u4f60&quot;\u51fa\u4e8b\u4e86&quot;\u8868\u660e\u4e34\u754c\u8d44\u6e90\u4e2d\u8d44\u6e90\u6570\u91cf\u8fd8\u5269\u591a\u5c11\u6bd4\u55bb\u95e8\u94c3\u505c\u8f66\u573a\u7684\u4f59\u4f4d\u663e\u793a\u5c4f1.2 POSIX \u4e0e System V sem_init \/ sem_wait \/ sem_po\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/116269.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-20T18:24:55+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261020182441-6ad7b1e9da2b6.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/116269.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/116269.html\",\"name\":\"POSIX\u4fe1\u53f7\u91cf\u4e0e\u73af\u5f62\u961f\u5217 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-10-20T18:24:55+00:00\",\"dateModified\":\"2026-10-20T18:24:55+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/116269.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/116269.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/116269.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"POSIX\u4fe1\u53f7\u91cf\u4e0e\u73af\u5f62\u961f\u5217\"}]},{\"@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":"POSIX\u4fe1\u53f7\u91cf\u4e0e\u73af\u5f62\u961f\u5217 - \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\/116269.html","og_locale":"zh_CN","og_type":"article","og_title":"POSIX\u4fe1\u53f7\u91cf\u4e0e\u73af\u5f62\u961f\u5217 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u4e00\u3001\u5148\u56de\u987e&#xff1a;\u4fe1\u53f7\u91cf\u5230\u5e95\u662f\u4e2a\u4ec0\u4e48\u4e1c\u897f 1.1 \u4fe1\u53f7 \u2260 \u4fe1\u53f7\u91cf\u4fe1\u53f7\u548c\u4fe1\u53f7\u91cf&#xff0c;\u662f\u300c\u8001\u5a46\u300d\u548c\u300c\u8001\u5a46\u997c\u300d\u7684\u5173\u7cfb \u2014\u2014 \u6ca1\u6709\u4efb\u4f55\u5173\u7cfb\u3002\u4fe1\u53f7&#xff08;signal&#xff09;\u4fe1\u53f7\u91cf&#xff08;semaphore \/ \u4fe1\u53f7\u706f&#xff09;\u672c\u8d28\u4e00\u79cd\u8fdb\u7a0b\u95f4\u5f02\u6b65\u901a\u77e5\u7684\u65b9\u5f0f\u4e00\u4e2a\u8ba1\u6570\u5668\u4f5c\u7528\u901a\u77e5\u4f60\"\u51fa\u4e8b\u4e86\"\u8868\u660e\u4e34\u754c\u8d44\u6e90\u4e2d\u8d44\u6e90\u6570\u91cf\u8fd8\u5269\u591a\u5c11\u6bd4\u55bb\u95e8\u94c3\u505c\u8f66\u573a\u7684\u4f59\u4f4d\u663e\u793a\u5c4f1.2 POSIX \u4e0e System V sem_init \/ sem_wait \/ sem_po","og_url":"https:\/\/www.wsisp.com\/helps\/116269.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-10-20T18:24:55+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261020182441-6ad7b1e9da2b6.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"7 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/116269.html","url":"https:\/\/www.wsisp.com\/helps\/116269.html","name":"POSIX\u4fe1\u53f7\u91cf\u4e0e\u73af\u5f62\u961f\u5217 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-10-20T18:24:55+00:00","dateModified":"2026-10-20T18:24:55+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/116269.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/116269.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/116269.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"POSIX\u4fe1\u53f7\u91cf\u4e0e\u73af\u5f62\u961f\u5217"}]},{"@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\/116269","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=116269"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/116269\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/116264"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=116269"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=116269"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=116269"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=116269"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}