{"id":206354,"date":"2025-06-12T13:58:39","date_gmt":"2025-06-12T05:58:39","guid":{"rendered":"https:\/\/server.hk\/cnblog\/206354\/"},"modified":"2025-06-12T13:58:39","modified_gmt":"2025-06-12T05:58:39","slug":"gosynccond%ef%bc%8c%e6%9c%80%e8%a2%ab%e5%bf%bd%e8%a7%86%e7%9a%84%e5%90%8c%e6%ad%a5%e6%9c%ba%e5%88%b6","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/206354\/","title":{"rendered":"GosyncCond\uff0c\u6700\u88ab\u5ffd\u89c6\u7684\u540c\u6b65\u673a\u5236"},"content":{"rendered":"<p>\u5077\u5077\u52aa\u529b\uff0c\u6084\u65e0\u58f0\u606f\u5730\u53d8\u5f3a\uff0c\u7136\u540e\u60ca\u8273\u6240\u6709\u4eba\uff01\u54c8\u54c8\uff0c\u5c0f\u4f19\u4f34\u4eec\u53c8\u6765\u5b66\u4e60\u5566~\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGosyncCond\uff0c\u6700\u88ab\u5ffd\u89c6\u7684\u540c\u6b65\u673a\u5236\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u4e0d\u77e5\u9053\u5927\u5bb6\u5bf9\u5176\u90fd\u6709\u591a\u5c11\u4e86\u89e3\uff0c\u4e0b\u9762\u6211\u4eec\u5c31\u4e00\u8d77\u6765\u770b\u4e00\u5427\uff01\u5f53\u7136\uff0c\u975e\u5e38\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u8bc4\u8bba\uff0c\u7ed9\u51fa\u5408\u7406\u7684\u5efa\u8bae\uff0c\u6211\u4eec\u4e00\u8d77\u5b66\u4e60\uff0c\u4e00\u8d77\u8fdb\u6b65\uff01,\u8fd9\u7bc7\u6587\u7ae0\u662f\u6709\u5173 go \u4e2d\u5904\u7406\u5e76\u53d1\u7684\u7cfb\u5217\u6587\u7ae0\u7684\u4e00\u90e8\u5206\uff1a,\u5728go\u4e2d\uff0csync.cond\u662f\u4e00\u4e2a\u540c\u6b65\u539f\u8bed\uff0c\u5c3d\u7ba1\u5b83\u4e0d\u50cfsync.mutex\u6216sync.waitgroup\u90a3\u6837\u5e38\u7528\u3002\u60a8\u5f88\u5c11\u4f1a\u5728\u5927\u591a\u6570\u9879\u76ee\u4e2d\u751a\u81f3\u5728\u6807\u51c6\u5e93\u4e2d\u770b\u5230\u5b83\uff0c\u800c\u5176\u4ed6\u540c\u6b65\u673a\u5236\u5f80\u5f80\u4f1a\u53d6\u4ee3\u5b83\u3002,\u4e5f\u5c31\u662f\u8bf4\uff0c\u4f5c\u4e3a\u4e00\u540d go \u5de5\u7a0b\u5e08\uff0c\u4f60\u4e0d\u4f1a\u771f\u7684\u5e0c\u671b\u81ea\u5df1\u5728\u9605\u8bfb\u4f7f\u7528sync.cond \u7684\u4ee3\u7801\u65f6\u5374\u4e0d\u77e5\u9053\u53d1\u751f\u4e86\u4ec0\u4e48\uff0c\u56e0\u4e3a\u6bd5\u7adf\u5b83\u662f\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\u3002,\u56e0\u6b64\uff0c\u672c\u6b21\u8ba8\u8bba\u5c06\u5e2e\u52a9\u60a8\u7f29\u5c0f\u8fd9\u4e00\u5dee\u8ddd\uff0c\u66f4\u597d\u7684\u662f\uff0c\u5b83\u4f1a\u8ba9\u60a8\u66f4\u6e05\u695a\u5730\u4e86\u89e3\u5b83\u5728\u5b9e\u8df5\u4e2d\u7684\u5b9e\u9645\u8fd0\u4f5c\u65b9\u5f0f\u3002,\u90a3\u4e48\uff0c\u8ba9\u6211\u4eec\u6765\u5206\u6790\u4e00\u4e0bsync.cond \u7684\u610f\u4e49\u3002,\u5f53 goroutine \u9700\u8981\u7b49\u5f85\u7279\u5b9a\u4e8b\u60c5\u53d1\u751f\u65f6\uff0c\u4f8b\u5982\u67d0\u4e9b\u5171\u4eab\u6570\u636e\u66f4\u6539\uff0c\u5b83\u53ef\u4ee5\u201c\u963b\u585e\u201d\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u53ea\u662f\u6682\u505c\u5176\u5de5\u4f5c\uff0c\u76f4\u5230\u83b7\u5f97\u7ee7\u7eed\u7684\u8bb8\u53ef\u3002\u6700\u57fa\u672c\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5faa\u73af\uff0c\u751a\u81f3\u53ef\u80fd\u6dfb\u52a0\u4e00\u4e2a time.sleep \u6765\u9632\u6b62 cpu \u56e0\u5fd9\u7b49\u5f85\u800c\u75af\u72c2\u3002,\u8fd9\u53ef\u80fd\u662f\u8fd9\u6837\u7684\uff1a<br \/>\n<br \/>,\u73b0\u5728\uff0c\u8fd9\u5e76\u4e0d\u662f\u771f\u6b63\u6709\u6548\uff0c\u56e0\u4e3a\u8be5\u5faa\u73af\u4ecd\u5728\u540e\u53f0\u8fd0\u884c\uff0c\u6d88\u8017 cpu \u5468\u671f\uff0c\u5373\u4f7f\u6ca1\u6709\u4efb\u4f55\u66f4\u6539\u3002,\u8fd9\u5c31\u662fsync.cond \u53d1\u6325\u4f5c\u7528\u7684\u5730\u65b9\uff0c\u5b83\u662f\u8ba9 goroutine \u534f\u8c03\u5de5\u4f5c\u7684\u66f4\u597d\u65b9\u6cd5\u3002\u4ece\u6280\u672f\u4e0a\u8bb2\uff0c\u5982\u679c\u60a8\u6765\u81ea\u66f4\u5b66\u672f\u7684\u80cc\u666f\uff0c\u90a3\u4e48\u5b83\u662f\u4e00\u4e2a\u201c\u6761\u4ef6\u53d8\u91cf\u201d\u3002,\u8fd9\u662fsync.cond\u7684\u57fa\u672c\u63a5\u53e3\uff1a<br \/>\n<br \/>,\u597d\u5427\uff0c\u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u5feb\u901f\u7684\u4f2a\u793a\u4f8b\u3002\u8fd9\u6b21\uff0c\u6211\u4eec\u6709\u4e00\u4e2a pok\u00e9mon \u4e3b\u9898\uff0c\u5047\u8bbe\u6211\u4eec\u6b63\u5728\u7b49\u5f85\u4e00\u4e2a\u7279\u5b9a\u7684 pok\u00e9mon\uff0c\u5e76\u4e14\u6211\u4eec\u5e0c\u671b\u5728\u5b83\u51fa\u73b0\u65f6\u901a\u77e5\u5176\u4ed6 goroutines\u3002<br \/>\n<br \/>,\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u4e00\u4e2a goroutine \u6b63\u5728\u7b49\u5f85\u76ae\u5361\u4e18\u51fa\u73b0\uff0c\u800c\u53e6\u4e00\u4e2a goroutine\uff08\u751f\u4ea7\u8005\uff09\u4ece\u5217\u8868\u4e2d\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u795e\u5947\u5b9d\u8d1d\uff0c\u5e76\u5728\u65b0\u795e\u5947\u5b9d\u8d1d\u51fa\u73b0\u65f6\u5411\u6d88\u8d39\u8005\u53d1\u51fa\u4fe1\u53f7\u3002,\u5f53\u751f\u4ea7\u8005\u53d1\u9001\u4fe1\u53f7\u65f6\uff0c\u6d88\u8d39\u8005\u9192\u6765\u5e76\u68c0\u67e5\u662f\u5426\u51fa\u73b0\u4e86\u6b63\u786e\u7684\u795e\u5947\u5b9d\u8d1d\u3002\u5982\u679c\u6709\uff0c\u6211\u4eec\u5c31\u6355\u83b7\u795e\u5947\u5b9d\u8d1d\uff0c\u5982\u679c\u6ca1\u6709\uff0c\u6d88\u8d39\u8005\u5c31\u56de\u53bb\u7761\u89c9\u5e76\u7b49\u5f85\u4e0b\u4e00\u4e2a\u3002,\u95ee\u9898\u662f\uff0c\u751f\u4ea7\u8005\u53d1\u9001\u4fe1\u53f7\u548c\u6d88\u8d39\u8005\u5b9e\u9645\u9192\u6765\u4e4b\u95f4\u5b58\u5728\u5dee\u8ddd\u3002\u4e0e\u6b64\u540c\u65f6\uff0cpok\u00e9mon \u53ef\u80fd\u4f1a\u53d1\u751f\u53d8\u5316\uff0c\u56e0\u4e3a\u6d88\u8d39\u8005 goroutine \u53ef\u80fd\u4f1a\u665a\u4e8e 1 \u6beb\u79d2\uff08\u5f88\u5c11\uff09\u9192\u6765\uff0c\u6216\u8005\u5176\u4ed6 goroutine \u4f1a\u4fee\u6539\u5171\u4eab\u7684 pokemon\u3002\u6240\u4ee5sync.cond \u57fa\u672c\u4e0a\u662f\u5728\u8bf4\uff1a<br \/>\n<em>&#8216;\u563f\uff0c\u6709\u4e9b\u4e1c\u897f\u6539\u53d8\u4e86\uff01\u9192\u8fc7\u6765\u770b\u770b\uff0c\u4f46\u5982\u679c\u592a\u665a\u4e86\uff0c\u53ef\u80fd\u53c8\u4f1a\u53d8\u4e86\u3002&#8217;<\/em>,\u5982\u679c\u6d88\u8d39\u8005\u8d77\u665a\u4e86\uff0cpok\u00e9mon \u53ef\u80fd\u4f1a\u9003\u8dd1\uff0c\u800c goroutine \u4f1a\u91cd\u65b0\u8fdb\u5165\u7761\u7720\u72b6\u6001\u3002,\u5f53\u7136\u3002\u4e8b\u5b9e\u4e0a\uff0c\u5728 go \u4e2d\uff0c\u901a\u9053\u901a\u5e38\u6bd4sync.cond\u66f4\u53d7\u6b22\u8fce\uff0c\u56e0\u4e3a\u5b83\u4eec\u66f4\u7b80\u5355\uff0c\u66f4\u60ef\u7528\uff0c\u5e76\u4e14\u4e3a\u5927\u591a\u6570\u5f00\u53d1\u4eba\u5458\u6240\u719f\u6089\u3002,\u5728\u4e0a\u9762\u7684\u60c5\u51b5\u4e0b\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u901a\u8fc7\u901a\u9053\u53d1\u9001 pok\u00e9mon \u540d\u79f0\uff0c\u6216\u8005\u4ec5\u4f7f\u7528\u7a7a struct{} \u6765\u53d1\u51fa\u4fe1\u53f7\u800c\u4e0d\u53d1\u9001\u4efb\u4f55\u6570\u636e\u3002\u4f46\u6211\u4eec\u7684\u95ee\u9898\u4e0d\u4ec5\u4ec5\u662f\u901a\u8fc7\u901a\u9053\u4f20\u9012\u6d88\u606f\uff0c\u800c\u662f\u5904\u7406\u5171\u4eab\u72b6\u6001\u3002,\u6211\u4eec\u7684\u4f8b\u5b50\u975e\u5e38\u7b80\u5355\uff0c\u4f46\u662f\u5982\u679c\u591a\u4e2a goroutine \u8bbf\u95ee\u5171\u4eab\u7684 pokemon \u53d8\u91cf\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u5982\u679c\u6211\u4eec\u4f7f\u7528\u901a\u9053\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1a,\u4e5f\u5c31\u662f\u8bf4\uff0c\u5f53\u591a\u4e2a goroutine \u4fee\u6539\u5171\u4eab\u6570\u636e\u65f6\uff0c\u4ecd\u7136\u9700\u8981\u4e92\u65a5\u4f53\u6765\u4fdd\u62a4\u5b83\u3002\u5728\u8fd9\u4e9b\u60c5\u51b5\u4e0b\uff0c\u60a8\u7ecf\u5e38\u4f1a\u770b\u5230\u901a\u9053\u548c\u4e92\u65a5\u4f53\u7684\u7ec4\u5408\uff0c\u4ee5\u786e\u4fdd\u6b63\u786e\u7684\u540c\u6b65\u548c\u6570\u636e\u5b89\u5168\u3002,\u597d\u95ee\u9898\uff01\u60a8\u786e\u5b9e\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u5730\u5173\u95ed\u901a\u9053\uff08close(ch)\uff09\u6765\u4f7f\u7528\u901a\u9053\u5411\u6240\u6709\u7b49\u5f85\u7684 goroutine \u6a21\u4eff\u5e7f\u64ad\u4fe1\u53f7\u3002\u5f53\u60a8\u5173\u95ed\u901a\u9053\u65f6\uff0c\u4ece\u8be5\u901a\u9053\u63a5\u6536\u7684\u6240\u6709 goroutine \u90fd\u4f1a\u6536\u5230\u901a\u77e5\u3002\u4f46\u8bf7\u8bb0\u4f4f\uff0c\u5173\u95ed\u7684\u901a\u9053\u65e0\u6cd5\u91cd\u590d\u4f7f\u7528\uff0c\u4e00\u65e6\u5173\u95ed\uff0c\u5b83\u5c31\u4f1a\u4fdd\u6301\u5173\u95ed\u72b6\u6001\u3002,\u987a\u4fbf\u8bf4\u4e00\u53e5\uff0c\u5b9e\u9645\u4e0a\u6709\u4eba\u5728\u8ba8\u8bba\u5728 go 2 \u4e2d\u5220\u9664sync.cond\uff1a\u63d0\u6848\uff1async\uff1a\u5220\u9664 cond \u7c7b\u578b\u3002,\u55ef\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0csync.cond \u53ef\u80fd\u6bd4\u901a\u9053\u66f4\u5408\u9002\u3002,\u7406\u8bba\u4e0a\uff0c\u50cfsync.cond \u8fd9\u6837\u7684\u6761\u4ef6\u53d8\u91cf\u4e0d\u5fc5\u7ed1\u5b9a\u5230\u9501\u5373\u53ef\u4f7f\u5176\u4fe1\u53f7\u6b63\u5e38\u5de5\u4f5c\u3002,\u60a8\u53ef\u4ee5\u8ba9\u7528\u6237\u5728\u6761\u4ef6\u53d8\u91cf\u4e4b\u5916\u7ba1\u7406\u81ea\u5df1\u7684\u9501\uff0c\u8fd9\u542c\u8d77\u6765\u50cf\u662f\u63d0\u4f9b\u4e86\u66f4\u5927\u7684\u7075\u6d3b\u6027\u3002\u8fd9\u5e76\u4e0d\u662f\u771f\u6b63\u7684\u6280\u672f\u9650\u5236\uff0c\u66f4\u591a\u7684\u662f\u4eba\u4e3a\u9519\u8bef\u3002,\u624b\u52a8\u7ba1\u7406\u5f88\u5bb9\u6613\u5bfc\u81f4\u9519\u8bef\uff0c\u56e0\u4e3a\u8be5\u6a21\u5f0f\u4e0d\u592a\u76f4\u89c2\uff0c\u60a8\u5fc5\u987b\u5728\u8c03\u7528 wait() \u4e4b\u524d\u89e3\u9501\u4e92\u65a5\u4f53\uff0c\u7136\u540e\u5728 goroutine \u5524\u9192\u65f6\u518d\u6b21\u9501\u5b9a\u5b83\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u53ef\u80fd\u4f1a\u8ba9\u4eba\u611f\u89c9\u5c34\u5c2c\uff0c\u800c\u4e14\u5f88\u5bb9\u6613\u51fa\u9519\uff0c\u6bd4\u5982\u5fd8\u8bb0\u5728\u6b63\u786e\u7684\u65f6\u95f4\u9501\u5b9a\u6216\u89e3\u9501\u3002,\u4f46\u662f\u4e3a\u4ec0\u4e48\u56fe\u6848\u770b\u8d77\u6765\u6709\u70b9\u4e0d\u5bf9\u52b2\uff1f,\u901a\u5e38\uff0c\u8c03\u7528 cond.wait() \u7684 goroutine \u9700\u8981\u5728\u5faa\u73af\u4e2d\u68c0\u67e5\u67d0\u4e9b\u5171\u4eab\u72b6\u6001\uff0c\u5982\u4e0b\u6240\u793a\uff1a<br \/>\n<br \/>,sync.cond \u4e2d\u5d4c\u5165\u7684\u9501\u5e2e\u52a9\u6211\u4eec\u5904\u7406\u9501\u5b9a\/\u89e3\u9501\u8fc7\u7a0b\uff0c\u4f7f\u4ee3\u7801\u66f4\u7b80\u6d01\u4e14\u4e0d\u6613\u51fa\u9519\uff0c\u6211\u4eec\u5c06\u5f88\u5feb\u8be6\u7ec6\u8ba8\u8bba\u8be5\u6a21\u5f0f\u3002,\u5982\u679c\u4ed4\u7ec6\u89c2\u5bdf\u524d\u9762\u7684\u793a\u4f8b\uff0c\u60a8\u4f1a\u6ce8\u610f\u5230\u6d88\u8d39\u8005\u4e2d\u7684\u4e00\u81f4\u6a21\u5f0f\uff1a\u6211\u4eec\u603b\u662f\u5728\u7b49\u5f85 (.wait()) \u6761\u4ef6\u4e4b\u524d\u9501\u5b9a\u4e92\u65a5\u4f53\uff0c\u5e76\u5728\u6ee1\u8db3\u6761\u4ef6\u540e\u89e3\u9501\u5b83\u3002,\u53e6\u5916\uff0c\u6211\u4eec\u5c06\u7b49\u5f85\u6761\u4ef6\u5305\u88c5\u5728\u4e00\u4e2a\u5faa\u73af\u4e2d\uff0c\u8fd9\u91cc\u590d\u4e60\u4e00\u4e0b\uff1a<br \/>\n<br \/>,\u5f53\u6211\u4eec\u5728sync.cond \u4e0a\u8c03\u7528wait() \u65f6\uff0c\u6211\u4eec\u662f\u5728\u544a\u8bc9\u5f53\u524d\u7684goroutine \u7b49\u5f85\uff0c\u76f4\u5230\u6ee1\u8db3\u67d0\u4e9b\u6761\u4ef6\u3002,\u8fd9\u662f\u5e55\u540e\u53d1\u751f\u7684\u4e8b\u60c5\uff1a,\u4ee5\u4e0b\u662f wait() \u5728\u5e95\u5c42\u7684\u5de5\u4f5c\u539f\u7406\uff1a<br \/>\n<br \/>,\u867d\u7136\u5f88\u7b80\u5355\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u603b\u7ed3\u51fa4\u4e2a\u8981\u70b9\uff1a,\u7531\u4e8e\u8fd9\u79cd\u9501\u5b9a\/\u89e3\u9501\u884c\u4e3a\uff0c\u5728\u4f7f\u7528sync.cond.wait() \u65f6\u60a8\u5c06\u9075\u5faa\u4e00\u4e2a\u5178\u578b\u6a21\u5f0f\u4ee5\u907f\u514d\u5e38\u89c1\u9519\u8bef\uff1a<br \/>\n<br \/>,\u4ee5\u4e0a\u5c31\u662f\u300aGosyncCond\uff0c\u6700\u88ab\u5ffd\u89c6\u7684\u540c\u6b65\u673a\u5236\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\uff01,\u53e6\u4e00\u4e2a goroutine\uff0c\u4e00\u65e6\u77e5\u9053\u6761\u4ef6\u53ef\u80fd\u6ee1\u8db3\uff0c\u5c31\u53ef\u4ee5\u8c03\u7528 signal() \u6216 broadcast() \u6765\u5524\u9192\u7b49\u5f85\u7684 goroutine\uff0c\u5e76\u8ba9\u5b83\u4eec\u77e5\u9053\u662f\u65f6\u5019\u7ee7\u7eed\u524d\u8fdb\u4e86\u3002,\u5f53\u4e00\u4e2agoroutine\u6b63\u5728\u7b49\u5f85\u67d0\u4ef6\u4e8b\u53d1\u751f\u65f6\uff08\u7b49\u5f85\u67d0\u4e2a\u6761\u4ef6\u6210\u7acb\uff09\uff0c\u5b83\u53ef\u4ee5\u8c03\u7528wait()\u3002,gosync.waitgroup \u548c\u5bf9\u9f50\u95ee\u9898,gosync.mutex\uff1a\u6b63\u5e38\u548c\u9965\u997f\u6a21\u5f0f,gosync.pool \u53ca\u5176\u80cc\u540e\u7684\u673a\u5236,\u5077\u5077\u52aa\u529b\uff0c\u6084\u65e0\u58f0\u606f\u5730\u53d8\u5f3a\uff0c\u7136\u540e\u60ca\u8273\u6240\u6709\u4eba\uff01\u54c8\u54c8\uff0c\u5c0f\u4f19\u4f34\u4eec\u53c8\u6765\u5b66\u4e60\u5566~\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGosyncCond\uff0c\u6700\u88ab\u5ffd\u89c6\u7684\u540c\u6b65\u673a\u5236\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u4e0d\u77e5\u9053\u5927\u5bb6\u5bf9\u5176\u90fd\u6709\u591a\u5c11\u4e86\u89e3\uff0c\u4e0b\u9762\u6211\u4eec\u5c31\u4e00\u8d77\u6765\u770b\u4e00\u5427\uff01\u5f53\u7136\uff0c\u975e\u5e38\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u8bc4\u8bba\uff0c\u7ed9\u51fa\u5408\u7406\u7684\u5efa\u8bae\uff0c\u6211\u4eec\u4e00\u8d77\u5b66\u4e60\uff0c\u4e00\u8d77\u8fdb\u6b65\uff01,<br \/>\n<b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>GosyncCond\uff0c\u6700\u88ab\u5ffd\u89c6\u7684\u540c\u6b65\u673a\u5236<\/span><\/p>\n<h1>GosyncCond\uff0c\u6700\u88ab\u5ffd\u89c6\u7684\u540c\u6b65\u673a\u5236<\/h1>\n<p><span>\u6765\u6e90\uff1adev.to<\/span><br \/>\n<span>2024-11-10 14:40:08<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5077\u5077\u52aa\u529b\uff0c\u6084\u65e0\u58f0\u606f\u5730\u53d8\u5f3a\uff0c\u7136\u540e\u60ca\u8273\u6240\u6709\u4eba\uff01\u54c8\u54c8\uff0c\u5c0f\u4f19\u4f34\u4eec\u53c8\u6765\u5b66\u4e60\u5566~\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGosyncCond\uff0c\u6700\u88ab\u5ffd\u89c6\u7684\u540c\u6b65\u673a\u5236\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u4e0d\u77e5\u9053\u5927\u5bb6\u5bf9\u5176\u90fd\u6709\u591a\u5c11\u4e86\u89e3\uff0c\u4e0b\u9762\u6211\u4eec\u5c31\u4e00\u8d77\u6765\u770b\u4e00\u5427\uff01\u5f53\u7136\uff0c\u975e\u5e38\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u8bc4\u8bba\uff0c\u7ed9\u51fa\u5408\u7406\u7684\u5efa\u8bae\uff0c\u6211\u4eec\u4e00\u8d77\u5b66\u4e60\uff0c\u4e00\u8d77\u8fdb\u6b65\uff01<\/p>\n<blockquote>\n<p>\u8fd9\u662f\u5e16\u5b50\u7684\u6458\u5f55\uff1b\u5b8c\u6574\u7684\u5e16\u5b50\u53ef\u4ee5\u5728\u8fd9\u91cc\u627e\u5230\uff1ahttps:\/\/victoriametrics.com\/blog\/go-sync-cond\/<\/p>\n<\/blockquote>\n<p>\u8fd9\u7bc7\u6587\u7ae0\u662f\u6709\u5173 go \u4e2d\u5904\u7406\u5e76\u53d1\u7684\u7cfb\u5217\u6587\u7ae0\u7684\u4e00\u90e8\u5206\uff1a<\/p>\n<ul>\n<li>gosync.mutex\uff1a\u6b63\u5e38\u548c\u9965\u997f\u6a21\u5f0f<\/li>\n<li>gosync.waitgroup \u548c\u5bf9\u9f50\u95ee\u9898<\/li>\n<li>gosync.pool \u53ca\u5176\u80cc\u540e\u7684\u673a\u5236<\/li>\n<li>\u4f7f\u7528sync.cond\uff0c\u6700\u88ab\u5ffd\u89c6\u7684\u540c\u6b65\u673a\u5236\uff08\u6211\u4eec\u6765\u4e86\uff09<\/li>\n<li>gosync.map\uff1a\u9002\u5408\u6b63\u786e\u5de5\u4f5c\u7684\u6b63\u786e\u5de5\u5177<\/li>\n<li>go singleflight \u878d\u5165\u60a8\u7684\u4ee3\u7801\uff0c\u800c\u4e0d\u662f\u60a8\u7684\u6570\u636e\u5e93<\/li>\n<\/ul>\n<p>\u5728go\u4e2d\uff0csync.cond\u662f\u4e00\u4e2a\u540c\u6b65\u539f\u8bed\uff0c\u5c3d\u7ba1\u5b83\u4e0d\u50cfsync.mutex\u6216sync.waitgroup\u90a3\u6837\u5e38\u7528\u3002\u60a8\u5f88\u5c11\u4f1a\u5728\u5927\u591a\u6570\u9879\u76ee\u4e2d\u751a\u81f3\u5728\u6807\u51c6\u5e93\u4e2d\u770b\u5230\u5b83\uff0c\u800c\u5176\u4ed6\u540c\u6b65\u673a\u5236\u5f80\u5f80\u4f1a\u53d6\u4ee3\u5b83\u3002<\/p>\n<p>\u4e5f\u5c31\u662f\u8bf4\uff0c\u4f5c\u4e3a\u4e00\u540d go \u5de5\u7a0b\u5e08\uff0c\u4f60\u4e0d\u4f1a\u771f\u7684\u5e0c\u671b\u81ea\u5df1\u5728\u9605\u8bfb\u4f7f\u7528sync.cond \u7684\u4ee3\u7801\u65f6\u5374\u4e0d\u77e5\u9053\u53d1\u751f\u4e86\u4ec0\u4e48\uff0c\u56e0\u4e3a\u6bd5\u7adf\u5b83\u662f\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u672c\u6b21\u8ba8\u8bba\u5c06\u5e2e\u52a9\u60a8\u7f29\u5c0f\u8fd9\u4e00\u5dee\u8ddd\uff0c\u66f4\u597d\u7684\u662f\uff0c\u5b83\u4f1a\u8ba9\u60a8\u66f4\u6e05\u695a\u5730\u4e86\u89e3\u5b83\u5728\u5b9e\u8df5\u4e2d\u7684\u5b9e\u9645\u8fd0\u4f5c\u65b9\u5f0f\u3002<\/p>\n<h2> \u4ec0\u4e48\u662fsync.cond\uff1f <\/h2>\n<p>\u90a3\u4e48\uff0c\u8ba9\u6211\u4eec\u6765\u5206\u6790\u4e00\u4e0bsync.cond \u7684\u610f\u4e49\u3002<\/p>\n<p>\u5f53 goroutine \u9700\u8981\u7b49\u5f85\u7279\u5b9a\u4e8b\u60c5\u53d1\u751f\u65f6\uff0c\u4f8b\u5982\u67d0\u4e9b\u5171\u4eab\u6570\u636e\u66f4\u6539\uff0c\u5b83\u53ef\u4ee5\u201c\u963b\u585e\u201d\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u53ea\u662f\u6682\u505c\u5176\u5de5\u4f5c\uff0c\u76f4\u5230\u83b7\u5f97\u7ee7\u7eed\u7684\u8bb8\u53ef\u3002\u6700\u57fa\u672c\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5faa\u73af\uff0c\u751a\u81f3\u53ef\u80fd\u6dfb\u52a0\u4e00\u4e2a time.sleep \u6765\u9632\u6b62 cpu \u56e0\u5fd9\u7b49\u5f85\u800c\u75af\u72c2\u3002<\/p>\n<p>\u8fd9\u53ef\u80fd\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<pre>\/\/ wait until condition is true\nfor !condition {  \n}\n\n\/\/ or \nfor !condition {\n    time.sleep(100 * time.millisecond)\n}\n<\/pre>\n<p>\u73b0\u5728\uff0c\u8fd9\u5e76\u4e0d\u662f\u771f\u6b63\u6709\u6548\uff0c\u56e0\u4e3a\u8be5\u5faa\u73af\u4ecd\u5728\u540e\u53f0\u8fd0\u884c\uff0c\u6d88\u8017 cpu \u5468\u671f\uff0c\u5373\u4f7f\u6ca1\u6709\u4efb\u4f55\u66f4\u6539\u3002<\/p>\n<p>\u8fd9\u5c31\u662fsync.cond \u53d1\u6325\u4f5c\u7528\u7684\u5730\u65b9\uff0c\u5b83\u662f\u8ba9 goroutine \u534f\u8c03\u5de5\u4f5c\u7684\u66f4\u597d\u65b9\u6cd5\u3002\u4ece\u6280\u672f\u4e0a\u8bb2\uff0c\u5982\u679c\u60a8\u6765\u81ea\u66f4\u5b66\u672f\u7684\u80cc\u666f\uff0c\u90a3\u4e48\u5b83\u662f\u4e00\u4e2a\u201c\u6761\u4ef6\u53d8\u91cf\u201d\u3002<\/p>\n<ul>\n<li>\u5f53\u4e00\u4e2agoroutine\u6b63\u5728\u7b49\u5f85\u67d0\u4ef6\u4e8b\u53d1\u751f\u65f6\uff08\u7b49\u5f85\u67d0\u4e2a\u6761\u4ef6\u6210\u7acb\uff09\uff0c\u5b83\u53ef\u4ee5\u8c03\u7528wait()\u3002<\/li>\n<li>\u53e6\u4e00\u4e2a goroutine\uff0c\u4e00\u65e6\u77e5\u9053\u6761\u4ef6\u53ef\u80fd\u6ee1\u8db3\uff0c\u5c31\u53ef\u4ee5\u8c03\u7528 signal() \u6216 broadcast() \u6765\u5524\u9192\u7b49\u5f85\u7684 goroutine\uff0c\u5e76\u8ba9\u5b83\u4eec\u77e5\u9053\u662f\u65f6\u5019\u7ee7\u7eed\u524d\u8fdb\u4e86\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u662fsync.cond\u7684\u57fa\u672c\u63a5\u53e3\uff1a<\/p>\n<pre>\/\/ suspends the calling goroutine until the condition is met\nfunc (c *cond) wait() {}\n\n\/\/ wakes up one waiting goroutine, if there is one\nfunc (c *cond) signal() {}\n\n\/\/ wakes up all waiting goroutines\nfunc (c *cond) broadcast() {}\n<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241110\/17312207906730553613a76.png\" style=\"max-width:100%\" class=\"aligncenter\"><\/p>\n<p> sync.cond \u6982\u8ff0 <\/p>\n<p>\u597d\u5427\uff0c\u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u5feb\u901f\u7684\u4f2a\u793a\u4f8b\u3002\u8fd9\u6b21\uff0c\u6211\u4eec\u6709\u4e00\u4e2a pok\u00e9mon \u4e3b\u9898\uff0c\u5047\u8bbe\u6211\u4eec\u6b63\u5728\u7b49\u5f85\u4e00\u4e2a\u7279\u5b9a\u7684 pok\u00e9mon\uff0c\u5e76\u4e14\u6211\u4eec\u5e0c\u671b\u5728\u5b83\u51fa\u73b0\u65f6\u901a\u77e5\u5176\u4ed6 goroutines\u3002<\/p>\n<pre>var pokemonlist = []string{\"pikachu\", \"charmander\", \"squirtle\", \"bulbasaur\", \"jigglypuff\"}\nvar cond = sync.newcond(&amp;sync.mutex{})\nvar pokemon = \"\"\n\nfunc main() {\n    \/\/ consumer\n    go func() {\n        cond.l.lock()\n        defer cond.l.unlock()\n\n        \/\/ waits until pikachu appears\n        for pokemon != \"pikachu\" {\n            cond.wait()\n        }\n        println(\"caught\" + pokemon)\n        pokemon = \"\"\n    }()\n\n    \/\/ producer\n    go func() {\n        \/\/ every 1ms, a random pok\u00e9mon appears\n        for i := 0; i &lt; 100; i++ {\n            time.sleep(time.millisecond)\n\n            cond.l.lock()\n            pokemon = pokemonlist[rand.intn(len(pokemonlist))]\n            cond.l.unlock()\n\n            cond.signal()\n        }\n    }()\n\n    time.sleep(100 * time.millisecond) \/\/ lazy wait\n}\n\n\/\/ output:\n\/\/ caught pikachu\n<\/pre>\n<p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u4e00\u4e2a goroutine \u6b63\u5728\u7b49\u5f85\u76ae\u5361\u4e18\u51fa\u73b0\uff0c\u800c\u53e6\u4e00\u4e2a goroutine\uff08\u751f\u4ea7\u8005\uff09\u4ece\u5217\u8868\u4e2d\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u795e\u5947\u5b9d\u8d1d\uff0c\u5e76\u5728\u65b0\u795e\u5947\u5b9d\u8d1d\u51fa\u73b0\u65f6\u5411\u6d88\u8d39\u8005\u53d1\u51fa\u4fe1\u53f7\u3002<\/p>\n<p>\u5f53\u751f\u4ea7\u8005\u53d1\u9001\u4fe1\u53f7\u65f6\uff0c\u6d88\u8d39\u8005\u9192\u6765\u5e76\u68c0\u67e5\u662f\u5426\u51fa\u73b0\u4e86\u6b63\u786e\u7684\u795e\u5947\u5b9d\u8d1d\u3002\u5982\u679c\u6709\uff0c\u6211\u4eec\u5c31\u6355\u83b7\u795e\u5947\u5b9d\u8d1d\uff0c\u5982\u679c\u6ca1\u6709\uff0c\u6d88\u8d39\u8005\u5c31\u56de\u53bb\u7761\u89c9\u5e76\u7b49\u5f85\u4e0b\u4e00\u4e2a\u3002<\/p>\n<p>\u95ee\u9898\u662f\uff0c\u751f\u4ea7\u8005\u53d1\u9001\u4fe1\u53f7\u548c\u6d88\u8d39\u8005\u5b9e\u9645\u9192\u6765\u4e4b\u95f4\u5b58\u5728\u5dee\u8ddd\u3002\u4e0e\u6b64\u540c\u65f6\uff0cpok\u00e9mon \u53ef\u80fd\u4f1a\u53d1\u751f\u53d8\u5316\uff0c\u56e0\u4e3a\u6d88\u8d39\u8005 goroutine \u53ef\u80fd\u4f1a\u665a\u4e8e 1 \u6beb\u79d2\uff08\u5f88\u5c11\uff09\u9192\u6765\uff0c\u6216\u8005\u5176\u4ed6 goroutine \u4f1a\u4fee\u6539\u5171\u4eab\u7684 pokemon\u3002\u6240\u4ee5sync.cond \u57fa\u672c\u4e0a\u662f\u5728\u8bf4\uff1a<em>&#8216;\u563f\uff0c\u6709\u4e9b\u4e1c\u897f\u6539\u53d8\u4e86\uff01\u9192\u8fc7\u6765\u770b\u770b\uff0c\u4f46\u5982\u679c\u592a\u665a\u4e86\uff0c\u53ef\u80fd\u53c8\u4f1a\u53d8\u4e86\u3002&#8217;<\/em> <\/p>\n<p>\u5982\u679c\u6d88\u8d39\u8005\u8d77\u665a\u4e86\uff0cpok\u00e9mon \u53ef\u80fd\u4f1a\u9003\u8dd1\uff0c\u800c goroutine \u4f1a\u91cd\u65b0\u8fdb\u5165\u7761\u7720\u72b6\u6001\u3002<\/p>\n<blockquote>\n<p><strong><em>\u201c\u54c8\uff0c\u6211\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u901a\u9053\u6765\u5c06 pokemon \u540d\u79f0\u6216\u4fe1\u53f7\u53d1\u9001\u7ed9\u53e6\u4e00\u4e2a goroutine\u201d<\/em><\/strong><\/p>\n<\/blockquote>\n<p>\u5f53\u7136\u3002\u4e8b\u5b9e\u4e0a\uff0c\u5728 go \u4e2d\uff0c\u901a\u9053\u901a\u5e38\u6bd4sync.cond\u66f4\u53d7\u6b22\u8fce\uff0c\u56e0\u4e3a\u5b83\u4eec\u66f4\u7b80\u5355\uff0c\u66f4\u60ef\u7528\uff0c\u5e76\u4e14\u4e3a\u5927\u591a\u6570\u5f00\u53d1\u4eba\u5458\u6240\u719f\u6089\u3002<\/p>\n<p>\u5728\u4e0a\u9762\u7684\u60c5\u51b5\u4e0b\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u901a\u8fc7\u901a\u9053\u53d1\u9001 pok\u00e9mon \u540d\u79f0\uff0c\u6216\u8005\u4ec5\u4f7f\u7528\u7a7a struct{} \u6765\u53d1\u51fa\u4fe1\u53f7\u800c\u4e0d\u53d1\u9001\u4efb\u4f55\u6570\u636e\u3002\u4f46\u6211\u4eec\u7684\u95ee\u9898\u4e0d\u4ec5\u4ec5\u662f\u901a\u8fc7\u901a\u9053\u4f20\u9012\u6d88\u606f\uff0c\u800c\u662f\u5904\u7406\u5171\u4eab\u72b6\u6001\u3002 <\/p>\n<p>\u6211\u4eec\u7684\u4f8b\u5b50\u975e\u5e38\u7b80\u5355\uff0c\u4f46\u662f\u5982\u679c\u591a\u4e2a goroutine \u8bbf\u95ee\u5171\u4eab\u7684 pokemon \u53d8\u91cf\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u5982\u679c\u6211\u4eec\u4f7f\u7528\u901a\u9053\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1a<\/p>\n<ul>\n<li>\u5982\u679c\u6211\u4eec\u4f7f\u7528\u901a\u9053\u53d1\u9001 pok\u00e9mon \u540d\u79f0\uff0c\u6211\u4eec\u4ecd\u7136\u9700\u8981\u4e00\u4e2a\u4e92\u65a5\u4f53\u6765\u4fdd\u62a4\u5171\u4eab\u7684 pokemon \u53d8\u91cf\u3002<\/li>\n<li>\u5982\u679c\u6211\u4eec\u4ec5\u4f7f\u7528\u901a\u9053\u6765\u53d1\u51fa\u4fe1\u53f7\uff0c\u5219\u4ecd\u7136\u9700\u8981\u4e92\u65a5\u4f53\u6765\u7ba1\u7406\u5bf9\u5171\u4eab\u72b6\u6001\u7684\u8bbf\u95ee\u3002<\/li>\n<li>\u5982\u679c\u6211\u4eec\u5728\u751f\u4ea7\u8005\u4e2d\u68c0\u67e5\u76ae\u5361\u4e18\uff0c\u7136\u540e\u901a\u8fc7\u901a\u9053\u53d1\u9001\u5b83\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u4e00\u4e2a\u4e92\u65a5\u9501\u3002\u6700\u91cd\u8981\u7684\u662f\uff0c\u6211\u4eec\u8fdd\u53cd\u4e86\u5173\u6ce8\u70b9\u5206\u79bb\u539f\u5219\uff0c\u5373\u751f\u4ea7\u8005\u627f\u62c5\u4e86\u771f\u6b63\u5c5e\u4e8e\u6d88\u8d39\u8005\u7684\u903b\u8f91\u3002<\/li>\n<\/ul>\n<p>\u4e5f\u5c31\u662f\u8bf4\uff0c\u5f53\u591a\u4e2a goroutine \u4fee\u6539\u5171\u4eab\u6570\u636e\u65f6\uff0c\u4ecd\u7136\u9700\u8981\u4e92\u65a5\u4f53\u6765\u4fdd\u62a4\u5b83\u3002\u5728\u8fd9\u4e9b\u60c5\u51b5\u4e0b\uff0c\u60a8\u7ecf\u5e38\u4f1a\u770b\u5230\u901a\u9053\u548c\u4e92\u65a5\u4f53\u7684\u7ec4\u5408\uff0c\u4ee5\u786e\u4fdd\u6b63\u786e\u7684\u540c\u6b65\u548c\u6570\u636e\u5b89\u5168\u3002<\/p>\n<blockquote>\n<p><strong><em>\u201c\u597d\u7684\uff0c\u4f46\u662f\u5e7f\u64ad\u4fe1\u53f7\u5462\uff1f\u201d<\/em><\/strong><\/p>\n<\/blockquote>\n<p>\u597d\u95ee\u9898\uff01\u60a8\u786e\u5b9e\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u5730\u5173\u95ed\u901a\u9053\uff08close(ch)\uff09\u6765\u4f7f\u7528\u901a\u9053\u5411\u6240\u6709\u7b49\u5f85\u7684 goroutine \u6a21\u4eff\u5e7f\u64ad\u4fe1\u53f7\u3002\u5f53\u60a8\u5173\u95ed\u901a\u9053\u65f6\uff0c\u4ece\u8be5\u901a\u9053\u63a5\u6536\u7684\u6240\u6709 goroutine \u90fd\u4f1a\u6536\u5230\u901a\u77e5\u3002\u4f46\u8bf7\u8bb0\u4f4f\uff0c\u5173\u95ed\u7684\u901a\u9053\u65e0\u6cd5\u91cd\u590d\u4f7f\u7528\uff0c\u4e00\u65e6\u5173\u95ed\uff0c\u5b83\u5c31\u4f1a\u4fdd\u6301\u5173\u95ed\u72b6\u6001\u3002<\/p>\n<p>\u987a\u4fbf\u8bf4\u4e00\u53e5\uff0c\u5b9e\u9645\u4e0a\u6709\u4eba\u5728\u8ba8\u8bba\u5728 go 2 \u4e2d\u5220\u9664sync.cond\uff1a\u63d0\u6848\uff1async\uff1a\u5220\u9664 cond \u7c7b\u578b\u3002<\/p>\n<blockquote>\n<p><strong><em>\u201c\u90a3\u4e48\uff0csync.cond \u6709\u4ec0\u4e48\u7528\u5462\uff1f\u201d<\/em><\/strong><\/p>\n<\/blockquote>\n<p>\u55ef\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0csync.cond \u53ef\u80fd\u6bd4\u901a\u9053\u66f4\u5408\u9002\u3002<\/p>\n<ol>\n<li>\u4f7f\u7528\u901a\u9053\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u53d1\u9001\u503c\u7684\u65b9\u5f0f\u5411\u4e00\u4e2a goroutine \u53d1\u9001\u4fe1\u53f7\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5173\u95ed\u901a\u9053\u6765\u901a\u77e5\u6240\u6709 goroutine\uff0c\u4f46\u4f60\u4e0d\u80fd\u540c\u65f6\u6267\u884c\u8fd9\u4e24\u79cd\u64cd\u4f5c\u3002 sync.cond \u4e3a\u60a8\u63d0\u4f9b\u66f4\u7ec6\u7c92\u5ea6\u7684\u63a7\u5236\u3002\u4f60\u53ef\u4ee5\u8c03\u7528 signal() \u6765\u5524\u9192\u5355\u4e2a goroutine\uff0c\u6216\u8005\u8c03\u7528 broadcast() \u6765\u5524\u9192\u6240\u6709 goroutine\u3002 <\/li>\n<li>\u5e76\u4e14\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u591a\u6b21\u8c03\u7528 broadcast()\uff0c\u800c\u901a\u9053\u4e00\u65e6\u5173\u95ed\u5c31\u65e0\u6cd5\u6267\u884c\u6b64\u64cd\u4f5c\uff08\u5173\u95ed\u5df2\u5173\u95ed\u7684\u901a\u9053\u4f1a\u5f15\u53d1\u6050\u614c\uff09\u3002<\/li>\n<li>\u901a\u9053\u4e0d\u63d0\u4f9b\u4fdd\u62a4\u5171\u4eab\u6570\u636e\u7684\u5185\u7f6e\u65b9\u6cd5 &#8211; \u60a8\u9700\u8981\u4f7f\u7528\u4e92\u65a5\u4f53\u5355\u72ec\u7ba1\u7406\u5b83\u3002\u53e6\u4e00\u65b9\u9762\uff0csync.cond \u901a\u8fc7\u5c06\u9501\u5b9a\u548c\u4fe1\u53f7\u53d1\u9001\u5230\u4e00\u4e2a\u5305\u4e2d\uff0c\u4e3a\u60a8\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u52a0\u96c6\u6210\u7684\u65b9\u6cd5\uff08\u4ee5\u53ca\u66f4\u597d\u7684\u6027\u80fd\uff09\u3002<\/li>\n<\/ol>\n<blockquote>\n<p><strong><em>\u201c\u4e3a\u4ec0\u4e48\u8981\u5728sync.cond\u4e2d\u5d4c\u5165lock\uff1f\u201d<\/em><\/strong><\/p>\n<\/blockquote>\n<p>\u7406\u8bba\u4e0a\uff0c\u50cfsync.cond \u8fd9\u6837\u7684\u6761\u4ef6\u53d8\u91cf\u4e0d\u5fc5\u7ed1\u5b9a\u5230\u9501\u5373\u53ef\u4f7f\u5176\u4fe1\u53f7\u6b63\u5e38\u5de5\u4f5c\u3002 <\/p>\n<p>\u60a8\u53ef\u4ee5\u8ba9\u7528\u6237\u5728\u6761\u4ef6\u53d8\u91cf\u4e4b\u5916\u7ba1\u7406\u81ea\u5df1\u7684\u9501\uff0c\u8fd9\u542c\u8d77\u6765\u50cf\u662f\u63d0\u4f9b\u4e86\u66f4\u5927\u7684\u7075\u6d3b\u6027\u3002\u8fd9\u5e76\u4e0d\u662f\u771f\u6b63\u7684\u6280\u672f\u9650\u5236\uff0c\u66f4\u591a\u7684\u662f\u4eba\u4e3a\u9519\u8bef\u3002<\/p>\n<p>\u624b\u52a8\u7ba1\u7406\u5f88\u5bb9\u6613\u5bfc\u81f4\u9519\u8bef\uff0c\u56e0\u4e3a\u8be5\u6a21\u5f0f\u4e0d\u592a\u76f4\u89c2\uff0c\u60a8\u5fc5\u987b\u5728\u8c03\u7528 wait() \u4e4b\u524d\u89e3\u9501\u4e92\u65a5\u4f53\uff0c\u7136\u540e\u5728 goroutine \u5524\u9192\u65f6\u518d\u6b21\u9501\u5b9a\u5b83\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u53ef\u80fd\u4f1a\u8ba9\u4eba\u611f\u89c9\u5c34\u5c2c\uff0c\u800c\u4e14\u5f88\u5bb9\u6613\u51fa\u9519\uff0c\u6bd4\u5982\u5fd8\u8bb0\u5728\u6b63\u786e\u7684\u65f6\u95f4\u9501\u5b9a\u6216\u89e3\u9501\u3002<\/p>\n<p>\u4f46\u662f\u4e3a\u4ec0\u4e48\u56fe\u6848\u770b\u8d77\u6765\u6709\u70b9\u4e0d\u5bf9\u52b2\uff1f<\/p>\n<p>\u901a\u5e38\uff0c\u8c03\u7528 cond.wait() \u7684 goroutine \u9700\u8981\u5728\u5faa\u73af\u4e2d\u68c0\u67e5\u67d0\u4e9b\u5171\u4eab\u72b6\u6001\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>for !checksomesharedstate() {\n    cond.wait()\n}\n<\/pre>\n<p>sync.cond \u4e2d\u5d4c\u5165\u7684\u9501\u5e2e\u52a9\u6211\u4eec\u5904\u7406\u9501\u5b9a\/\u89e3\u9501\u8fc7\u7a0b\uff0c\u4f7f\u4ee3\u7801\u66f4\u7b80\u6d01\u4e14\u4e0d\u6613\u51fa\u9519\uff0c\u6211\u4eec\u5c06\u5f88\u5feb\u8be6\u7ec6\u8ba8\u8bba\u8be5\u6a21\u5f0f\u3002<\/p>\n<h2> \u5982\u4f55\u4f7f\u7528\uff1f <\/h2>\n<p>\u5982\u679c\u4ed4\u7ec6\u89c2\u5bdf\u524d\u9762\u7684\u793a\u4f8b\uff0c\u60a8\u4f1a\u6ce8\u610f\u5230\u6d88\u8d39\u8005\u4e2d\u7684\u4e00\u81f4\u6a21\u5f0f\uff1a\u6211\u4eec\u603b\u662f\u5728\u7b49\u5f85 (.wait()) \u6761\u4ef6\u4e4b\u524d\u9501\u5b9a\u4e92\u65a5\u4f53\uff0c\u5e76\u5728\u6ee1\u8db3\u6761\u4ef6\u540e\u89e3\u9501\u5b83\u3002 <\/p>\n<p>\u53e6\u5916\uff0c\u6211\u4eec\u5c06\u7b49\u5f85\u6761\u4ef6\u5305\u88c5\u5728\u4e00\u4e2a\u5faa\u73af\u4e2d\uff0c\u8fd9\u91cc\u590d\u4e60\u4e00\u4e0b\uff1a<\/p>\n<pre>\/\/ consumer\ngo func() {\n    cond.l.lock()\n    defer cond.l.unlock()\n\n    \/\/ waits until pikachu appears\n    for pokemon != \"pikachu\" {\n        cond.wait()\n    }\n    println(\"caught\" + pokemon)\n}()\n<\/pre>\n<h3> \u6761\u4ef6\u7b49\u5f85() <\/h3>\n<p>\u5f53\u6211\u4eec\u5728sync.cond \u4e0a\u8c03\u7528wait() \u65f6\uff0c\u6211\u4eec\u662f\u5728\u544a\u8bc9\u5f53\u524d\u7684goroutine \u7b49\u5f85\uff0c\u76f4\u5230\u6ee1\u8db3\u67d0\u4e9b\u6761\u4ef6\u3002 <\/p>\n<p>\u8fd9\u662f\u5e55\u540e\u53d1\u751f\u7684\u4e8b\u60c5\uff1a<\/p>\n<ol>\n<li>\u8be5 goroutine \u88ab\u6dfb\u52a0\u5230\u5176\u4ed6\u4e5f\u5728\u7b49\u5f85\u76f8\u540c\u6761\u4ef6\u7684 goroutine \u5217\u8868\u4e2d\u3002\u6240\u6709\u8fd9\u4e9b goroutine \u90fd\u88ab\u963b\u585e\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u4eec\u65e0\u6cd5\u7ee7\u7eed\uff0c\u76f4\u5230\u88ab signal() \u6216 broadcast() \u8c03\u7528\u201c\u5524\u9192\u201d\u4e3a\u6b62\u3002<\/li>\n<li>\u8fd9\u91cc\u7684\u5173\u952e\u90e8\u5206\u662f\uff0c\u5728\u8c03\u7528 wait() \u4e4b\u524d\u5fc5\u987b\u9501\u5b9a\u4e92\u65a5\u9501\uff0c\u56e0\u4e3a wait() \u505a\u4e86\u4e00\u4e9b\u91cd\u8981\u7684\u4e8b\u60c5\uff0c\u5b83\u4f1a\u5728\u8ba9 goroutine \u4f11\u7720\u4e4b\u524d\u81ea\u52a8\u91ca\u653e\u9501\uff08\u8c03\u7528 unlock()\uff09\u3002\u8fd9\u5141\u8bb8\u5176\u4ed6 goroutine \u5728\u539f\u59cb goroutine \u7b49\u5f85\u65f6\u83b7\u53d6\u9501\u5e76\u5b8c\u6210\u5176\u5de5\u4f5c\u3002<\/li>\n<li>\u5f53\u7b49\u5f85\u7684 goroutine \u88ab\u5524\u9192\uff08\u901a\u8fc7 signal() \u6216 broadcast()\uff09\u65f6\uff0c\u5b83\u4e0d\u4f1a\u7acb\u5373\u6062\u590d\u5de5\u4f5c\u3002\u9996\u5148\uff0c\u5b83\u5fc5\u987b\u91cd\u65b0\u83b7\u53d6\u9501\uff08lock()\uff09\u3002<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241110\/1731220791673055370d0f6.jpg\" class=\"aligncenter\"><\/p>\n<p> sync.cond.wait() \u65b9\u6cd5 <\/p>\n<p>\u4ee5\u4e0b\u662f wait() \u5728\u5e95\u5c42\u7684\u5de5\u4f5c\u539f\u7406\uff1a<\/p>\n<pre>func (c *cond) wait() {\n    \/\/ check if cond has been copied\n    c.checker.check()\n\n    \/\/ get the ticket number\n    t := runtime_notifylistadd(&amp;c.notify)\n\n    \/\/ unlock the mutex     \n    c.l.unlock()\n\n    \/\/ suspend the goroutine until being woken up\n    runtime_notifylistwait(&amp;c.notify, t)\n\n    \/\/ re-lock the mutex\n    c.l.lock()\n}\n<\/pre>\n<p>\u867d\u7136\u5f88\u7b80\u5355\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u603b\u7ed3\u51fa4\u4e2a\u8981\u70b9\uff1a<\/p>\n<ol>\n<li>\u6709\u4e00\u4e2a\u68c0\u67e5\u5668\u53ef\u4ee5\u9632\u6b62\u590d\u5236 cond \u5b9e\u4f8b\uff0c\u5982\u679c\u8fd9\u6837\u505a\u4f1a\u51fa\u73b0\u6050\u614c\u3002<\/li>\n<li>\u8c03\u7528 cond.wait() \u4f1a\u7acb\u5373\u89e3\u9501\u4e92\u65a5\u4f53\uff0c\u56e0\u6b64\u5728\u8c03\u7528 cond.wait() \u4e4b\u524d\u5fc5\u987b\u9501\u5b9a\u4e92\u65a5\u4f53\uff0c\u5426\u5219\u4f1a\u51fa\u73b0\u6050\u614c\u3002<\/li>\n<li>\u88ab\u5524\u9192\u540e\uff0ccond.wait() \u4f1a\u91cd\u65b0\u9501\u5b9a\u4e92\u65a5\u4f53\uff0c\u8fd9\u610f\u5473\u7740\u60a8\u5728\u4f7f\u7528\u5b8c\u5171\u4eab\u6570\u636e\u540e\u9700\u8981\u518d\u6b21\u89e3\u9501\u5b83\u3002<\/li>\n<li>sync.cond \u7684\u5927\u90e8\u5206\u529f\u80fd\u662f\u5728 go \u8fd0\u884c\u65f6\u4e2d\u901a\u8fc7\u540d\u4e3a notificationlist \u7684\u5185\u90e8\u6570\u636e\u7ed3\u6784\u5b9e\u73b0\u7684\uff0c\u8be5\u7ed3\u6784\u4f7f\u7528\u57fa\u4e8e\u7968\u636e\u7684\u7cfb\u7edf\u8fdb\u884c\u901a\u77e5\u3002<\/li>\n<\/ol>\n<p>\u7531\u4e8e\u8fd9\u79cd\u9501\u5b9a\/\u89e3\u9501\u884c\u4e3a\uff0c\u5728\u4f7f\u7528sync.cond.wait() \u65f6\u60a8\u5c06\u9075\u5faa\u4e00\u4e2a\u5178\u578b\u6a21\u5f0f\u4ee5\u907f\u514d\u5e38\u89c1\u9519\u8bef\uff1a<\/p>\n<pre>c.L.Lock()\nfor !condition() {\n    c.Wait()\n}\n\/\/ ... make use of condition ...\nc.L.Unlock()\n<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241110\/1731220792673055380e9a5.jpg\" style=\"max-width:100%\" class=\"aligncenter\"><\/p>\n<p> \u4f7f\u7528sync.cond.wait()\u7684\u5178\u578b\u6a21\u5f0f <\/p>\n<blockquote>\n<p><strong><em>\u201c\u4e3a\u4ec0\u4e48\u4e0d\u76f4\u63a5\u4f7f\u7528 c.wait() \u800c\u4e0d\u4f7f\u7528\u5faa\u73af\u5462\uff1f\u201d<\/em><\/strong><\/p>\n<\/blockquote>\n<hr>\n<blockquote>\n<p>\u8fd9\u662f\u5e16\u5b50\u7684\u6458\u5f55\uff1b\u5b8c\u6574\u7684\u5e16\u5b50\u53ef\u4ee5\u5728\u8fd9\u91cc\u627e\u5230\uff1ahttps:\/\/victoriametrics.com\/blog\/go-sync-cond\/<\/p>\n<\/blockquote>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300aGosyncCond\uff0c\u6700\u88ab\u5ffd\u89c6\u7684\u540c\u6b65\u673a\u5236\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\uff01<\/p>\n<p> \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb \u5220\u9664  <\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   Sass \u4e2d\u7684\u5360\u4f4d\u7b26\u9009\u62e9\u5668\u662f\u4ec0\u4e48\uff1f\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u5982\u4f55\u89e3\u51b3 PDF.js \u5728\u7ebf\u67e5\u770b\u542b\u6709\u767e\u5206\u53f7\u7684\u6587\u4ef6\u540d\u7684\u95ee\u9898\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5077\u5077\u52aa\u529b\uff0c\u6084\u65e0\u58f0\u606f\u5730\u53d8\u5f3a\uff0c\u7136\u540e\u60ca&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-206354","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/206354","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=206354"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/206354\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=206354"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=206354"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=206354"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}