{"id":208249,"date":"2025-07-08T13:08:27","date_gmt":"2025-07-08T05:08:27","guid":{"rendered":"https:\/\/server.hk\/cnblog\/208249\/"},"modified":"2025-07-08T13:08:27","modified_gmt":"2025-07-08T05:08:27","slug":"%e5%90%8c%e6%ad%a5%e6%98%a0%e5%b0%84%e4%b8%ad%e9%94%ae%e7%9a%84%e5%b9%b6%e5%8f%91%e6%80%a7","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208249\/","title":{"rendered":"\u540c\u6b65\u6620\u5c04\u4e2d\u952e\u7684\u5e76\u53d1\u6027"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u540c\u6b65\u6620\u5c04\u4e2d\u952e\u7684\u5e76\u53d1\u6027<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-30 18:33:33<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5728Golang\u5b9e\u6218\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u8fd9\u6837\u90a3\u6837\u7684\u95ee\u9898\uff0c\u7136\u540e\u8981\u5361\u597d\u534a\u5929\uff0c\u7b49\u95ee\u9898\u89e3\u51b3\u4e86\u624d\u53d1\u73b0\u539f\u6765\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u662f\u6ca1\u6709\u638c\u63e1\u597d\u3002\u4eca\u5929\u5c31\u6574\u7406\u5206\u4eab\u300a\u540c\u6b65\u6620\u5c04\u4e2d\u952e\u7684\u5e76\u53d1\u6027\u300b\uff0c\u804a\u804a\uff0c\u5e0c\u671b\u53ef\u4ee5\u5e2e\u52a9\u5230\u6b63\u5728\u52aa\u529b\u8d5a\u94b1\u7684\u4f60\u3002<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u5c1d\u8bd5\u5728 golang \u4e2d\u4f7f\u7528\u540c\u6b65\u6620\u5c04\u6765\u63d0\u4f9b\u83b7\u53d6\u7279\u5b9a\u5b57\u7b26\u4e32\u9501\u5b9a\u7684\u80fd\u529b\uff08\u4f8b\u5982\u201clock1\u201d\uff09\u3002<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"sync\"\n    \"time\"\n)\n\nvar lockMap sync.Map\nfunc main(){\n\n    counter := func(key string,routineId string) {\n        _,ok := lockMap.Load(key)\n        if(ok){\n            fmt.Println(key+ \" skipped by \"+ routineId)\n            return}\n\n        lockMap.Store(key,true)\n        defer lockMap.Delete(key)\n        for i := 0; i &lt; 10; i++ {\n            fmt.Println(key+ \" locked by \"+ routineId)\n        }\n    }\n\n    \/\/ Starting some goroutines\n    go counter(\"LOCK1\",\"routine1\")\n    go counter(\"LOCK1\",\"routine2\")\n    go counter(\"LOCK1\",\"routine3\")\n    go counter(\"LOCK1\",\"routine4\")\n    go counter(\"LOCK1\",\"routine5\")\n    \/\/adding some sleep so the  routines can execute for sometime\n    time.Sleep(time.Second)\n}<\/pre>\n<p>\u6211\u77e5\u9053\u540c\u6b65\u6620\u5c04\u5728\u5e95\u5c42\u4f7f\u7528\u4e86 rwmutex\u3002 \u4f46\u662f\uff0c\u6211\u60f3\u4e86\u89e3\u540c\u6b65\u6620\u5c04\u662f\u5426\u5141\u8bb8\u540c\u65f6\u5728\u4e0d\u540c\u7684\u952e\u4e0a\u8fdb\u884c\u591a\u6b21\u5199\u5165\uff0c\u6216\u8005\u53ea\u6709\u4e00\u4e2a\u4f8b\u7a0b\u53ef\u4ee5\u4e00\u6b21\u5728\u6574\u4e2a\u6620\u5c04\u4e0a\u8fdb\u884c\u5199\u5165\uff1f<\/p>\n<p>\u5047\u8bbe\u4f8b\u7a0b1\u6b63\u5728\u8bbe\u7f6e\u952e\u201clock1\u201d\uff0c\u4f8b\u7a0b2\u662f\u5426\u80fd\u591f\u540c\u65f6\u8bbe\u7f6e\u65b0\u503c\u201clock2\u201d\uff0c\u8fd8\u662f\u5fc5\u987b\u7b49\u5f85\u7b2c\u4e00\u6b21\u5199\u5165\u5b8c\u6210\u3002<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u5bf9\u4e8e\u6bcf\u628a\u94a5\u5319\uff0c\u60a8\u90fd\u5e0c\u671b\u8bbf\u95ee\u73b0\u6709\u7684\u9501\uff0c\u6216\u8005\u5982\u679c\u6ca1\u6709\u9501\uff0c\u5219\u521b\u5efa\u4e00\u4e2a\u65b0\u9501\u3002\u4f7f\u7528 \uff0c\u60a8\u53ef\u4ee5\u539f\u5b50\u5730\u5b8c\u6210\u6b64\u64cd\u4f5c\u3002<\/p>\n<pre>\/\/ Pre-allocate a new lock, in case it's needed for this key.\nnewLockIfNeeded = new(sync.Mutex)\nlock, _ = lockMap.LoadOrStore(key, newLockIfNeeded)\n\/\/ Safely acquired the (potentially new) lock for the key\nlock.Lock()\ndefer lock.Unlock()\n\/\/ do work ...\n<\/pre>\n<p>\u6211\u4f1a\u5c1d\u8bd5\u5411op\u63d0\u4f9b\u76f4\u63a5\u7b54\u6848<\/p>\n<p><strong>\u662f\u7684\uff0c\u4f46\u5728\u7279\u5b9a\u6761\u4ef6\u4e0b<\/strong><\/p>\n<p>sync.map \u9488\u5bf9\u8bb8\u591a\u8bfb\u8005\u7684\u72ec\u7279\u5199\u5165\u8fdb\u884c\u4e86\u4f18\u5316\u3002\u5982\u679c\u6709\u591a\u4e2a\u4f8b\u7a0b\u66f4\u65b0\u76f8\u540c\u7684\u5bc6\u94a5\uff0c\u901f\u5ea6\u4f1a\u5f88\u6162\uff08\u8bf7\u53c2\u9605 \uff09\u3002\u5b83\u5728\u5185\u90e8\u5305\u542b\u4e00\u4e2a\u8bfb\u6620\u5c04\u3001\u4e00\u4e2a\u810f\u6620\u5c04\u548c\u5bf9\u6307\u9488\u7684\u539f\u5b50\u8bfb\/\u5199\u3002\u6709\u4e9b\u64cd\u4f5c\u4f7f\u7528\u4e92\u65a5\u4f53\uff0c\u800c\u5176\u4ed6\u64cd\u4f5c\u5219\u4e3a\u4e86\u901f\u5ea6\u800c\u7ed5\u8fc7\u5b83\u3002<\/p>\n<p><strong>\u662f\u7684\uff0c\u53ea\u6709\u5728\u4ee5\u4e0b\u60c5\u51b5\u4e0b\u624d\u5141\u8bb8\u540c\u65f6<em><strong>\u5199\u5165\/\u5220\u9664<\/strong><\/em>\uff1a<\/strong><\/p>\n<ul>\n<li>\u5bc6\u94a5\u5df2\u5b58\u5728 (<code>m.read.load()<\/code>)\n<ul>\n<li><code>atomic.loadpointer<\/code> \u83b7\u53d6\u5feb\u901f\u8bfb\u53d6\u7684\u5730\u56fe\uff0c\u4f46\u8c03\u7528\u8005\u4ecd\u7136\u53ef\u4ee5\u4e00\u8d77\u5230\u8fbe\u4e0b\u4e00\u6b65<\/li>\n<\/ul>\n<\/li>\n<li>\u4e0a\u8ff0\u5bc6\u94a5\u6ca1\u6709\u6216\u6ca1\u6709\u88ab\u66f4\u65b0\/\u5220\u9664\n<ul>\n<li>\u672a\u66f4\u65b0\uff1async.map \u68c0\u67e5 <code>expunged<\/code> \u6307\u9488\u4ee5\u67e5\u770b\u662f\u5426\u5df2\u5728\u5176\u4ed6\u5730\u65b9\u66f4\u65b0<\/li>\n<li>\u672a\u66f4\u65b0\uff1a\u68c0\u67e5\u5bc6\u94a5\u662f\u5426\u5df2\u5220\u9664\u65f6\u5bc6\u94a5\u672a\u66f4\u6539 (<code>compareandswappointer<\/code>)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><strong>\u662f\u7684\uff0c\u5982\u679c\u6ee1\u8db3\u4ee5\u4e0b\u4efb\u4e00\u6761\u4ef6\uff0c\u5219\u5141\u8bb8\u540c\u65f6<em><strong>\u8bfb\u53d6<\/strong><\/em><\/strong><\/p>\n<ul>\n<li>\u8be5\u5bc6\u94a5\u4e0d\u5b58\u5728\uff0c\u4e14\u672a\u6dfb\u52a0\u65b0\u5bc6\u94a5<\/li>\n<li>\u5bc6\u94a5\u5b58\u5728\u4e14\u672a\u66f4\u65b0\uff08\u5982\u4e0a\u6240\u793a\uff09<\/li>\n<\/ul>\n<p><strong>\u4e0d\uff0c\u5728\u4ee5\u4e0b\u60c5\u51b5\u4e0b\u4f7f\u7528\u4e92\u65a5\u9501<\/strong><\/p>\n<ul>\n<li>\u5b58\u50a8\u65b0\u5bc6\u94a5\n<ul>\n<li>\u793a\u4f8b\uff1a\u6279\u91cf\u6dfb\u52a0\u4e0d\u540c\u7684\u952e\u901f\u5ea6\u5f88\u6162<\/li>\n<\/ul>\n<\/li>\n<li>\u6dfb\u52a0\u65b0\u5bc6\u94a5\u540e\u52a0\u8f7d\u4e22\u5931\u7684\u5bc6\u94a5\n<ul>\n<li>\u793a\u4f8b\uff1a\u9891\u7e41\u5220\u9664\u4e14\u68c0\u67e5\u5220\u9664\u901f\u5ea6\u6162<\/li>\n<\/ul>\n<\/li>\n<li>\u8bfb\u53d6\u6216\u5199\u5165\u6b63\u5728\u66f4\u65b0\u7684\u5bc6\u94a5\n<ul>\n<li>\u793a\u4f8b\uff1a\u591a\u6b21\u66f4\u65b0\u540c\u4e00\u5bc6\u94a5\u7684\u901f\u5ea6\u5f88\u6162<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><strong>\u4f46\u662f\uff0c\u4e92\u65a5\u4f53\u4e0d\u4f1a\u963b\u6b62\u201c\u662f\u201d\u60c5\u51b5<\/strong><\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u540c\u6b65\u6620\u5c04\u4e2d\u952e\u7684\u5e76\u53d1\u6027\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#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-208249","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208249","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=208249"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208249\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208249"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}