{"id":32,"date":"2023-05-19T20:25:49","date_gmt":"2023-05-19T12:25:49","guid":{"rendered":"http:\/\/www.greatfar.cn\/?p=32"},"modified":"2023-06-01T15:37:00","modified_gmt":"2023-06-01T07:37:00","slug":"go-%e5%b9%b6%e5%8f%91%e9%94%81-mutex","status":"publish","type":"post","link":"https:\/\/www.greatfar.cn\/archives\/32","title":{"rendered":"go \u5e76\u53d1\u9501 Mutex"},"content":{"rendered":"\n
Mutex\u7684\u6982\u5ff5<\/strong><\/p>\n\n\n\n \u4f7f\u7528\u573a\u666f<\/strong><\/p>\n\n\n\n \u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee\u4e34\u754c\u533a\uff0c\u4e3a\u4fdd\u8bc1\u6570\u636e\u7684\u5b89\u5168\uff0c\u9501\u4f4f\u4e00\u4e9b\u5171\u4eab\u8d44\u6e90\uff0c \u4ee5\u9632\u6b62\u5e76\u53d1\u8bbf\u95ee\u8fd9\u4e9b\u5171\u4eab\u6570\u636e\u65f6\u53ef\u80fd\u5bfc\u81f4\u7684\u6570\u636e\u4e0d\u4e00\u81f4\u95ee\u9898<\/p>\n\n\n\n \u5e95\u5c42\u5b9e\u73b0\u7ed3\u6784<\/strong><\/p>\n\n\n\n \u4e92\u65a5\u9501\u5bf9\u5e94\u7684\u662f\u5e95\u5c42\u7ed3\u6784\u662fsync.Mutex\u7ed3\u6784\u4f53<\/a>\uff0c\uff0c\u4f4d\u4e8e src\/sync\/mutex.go\u4e2d<\/p>\n\n\n\n type Mutex struct { state int32 sema uint32 }<\/p>\n\n\n\n state\u8868\u793a\u9501\u7684\u72b6\u6001\uff0c\u6709\u9501\u5b9a\u3001\u88ab\u5524\u9192\u3001\u9965\u997f\u6a21\u5f0f\u7b49\uff0c\u5e76\u4e14\u662f\u7528state\u7684\u4e8c\u8fdb\u5236\u4f4d\u6765\u6807\u8bc6\u7684\uff0c\u4e0d\u540c\u6a21\u5f0f\u4e0b\u4f1a\u6709\u4e0d\u540c\u7684\u5904\u7406\u65b9\u5f0f<\/p>\n\n\n\n sema\u8868\u793a\u4fe1\u53f7\u91cf\uff0cmutex\u963b\u585e\u961f\u5217\u7684\u5b9a\u4f4d\u662f\u901a\u8fc7\u8fd9\u4e2a\u53d8\u91cf\u6765\u5b9e\u73b0\u7684\uff0c\u4ece\u800c\u5b9e\u73b0goroutine\u7684\u963b\u585e\u548c\u5524\u9192<\/p>\n\n\n\n \u64cd\u4f5c<\/strong><\/p>\n\n\n\n \u9501\u7684\u5b9e\u73b0\u4e00\u822c\u4f1a\u4f9d\u8d56\u4e8e\u539f\u5b50\u64cd\u4f5c\u3001\u4fe1\u53f7\u91cf\uff0c\u901a\u8fc7atomic \u5305\u4e2d\u7684\u4e00\u4e9b\u539f\u5b50\u64cd\u4f5c\u6765\u5b9e\u73b0\u9501\u7684\u9501\u5b9a\uff0c\u901a\u8fc7\u4fe1\u53f7\u91cf\u6765\u5b9e\u73b0\u7ebf\u7a0b\u7684\u963b\u585e\u4e0e\u5524\u9192<\/p>\n\n\n\n \u52a0\u9501<\/strong><\/p>\n\n\n\n \u901a\u8fc7\u539f\u5b50\u64cd\u4f5ccas\u52a0\u9501\uff0c\u5982\u679c\u52a0\u9501\u4e0d\u6210\u529f\uff0c\u6839\u636e\u4e0d\u540c\u7684\u573a\u666f\u9009\u62e9\u81ea\u65cb\u91cd\u8bd5\u52a0\u9501\u6216\u8005\u963b\u585e\u7b49\u5f85\u88ab\u5524\u9192\u540e\u52a0\u9501<\/p>\n\n\n\n func (m *Mutex) Lock() { \/\/ Fast path: \u5e78\u8fd0\u4e4b\u8def\uff0c\u4e00\u4e0b\u5c31\u83b7\u53d6\u5230\u4e86\u9501 if atomic.CompareAndSwapInt32(&m.state, 0, mutexLocked) { return } \/\/ Slow path\uff1a\u7f13\u6162\u4e4b\u8def\uff0c\u5c1d\u8bd5\u81ea\u65cb\u6216\u963b\u585e\u83b7\u53d6\u9501 m.lockSlow() }<\/p>\n\n\n\n \u89e3\u9501<\/strong><\/p>\n\n\n\n \u901a\u8fc7\u539f\u5b50\u64cd\u4f5cadd\u89e3\u9501\uff0c\u5982\u679c\u4ecd\u6709goroutine\u5728\u7b49\u5f85\uff0c\u5524\u9192\u7b49\u5f85\u7684goroutine<\/p>\n\n\n\n func (m *Mutex) Unlock() { \/\/ Fast path: \u5e78\u8fd0\u4e4b\u8def\uff0c\u89e3\u9501 new := atomic.AddInt32(&m.state, -mutexLocked) if new != 0 { \/\/ Slow path\uff1a\u5982\u679c\u6709\u7b49\u5f85\u7684goroutine\uff0c\u5524\u9192\u7b49\u5f85\u7684goroutine m.unlockSlow() } }<\/p>\n\n\n\n \u6ce8\u610f<\/strong><\/p>\n\n\n\n \u5728 Lock() \u4e4b\u524d\u4f7f\u7528 Unlock() \u4f1a\u5bfc\u81f4 panic \u5f02\u5e38<\/p>\n\n\n\n \u4f7f\u7528 Lock() \u52a0\u9501\u540e\uff0c\u518d\u6b21 Lock() \u4f1a\u5bfc\u81f4\u6b7b\u9501\uff08\u4e0d\u652f\u6301\u91cd\u5165\uff09\uff0c\u9700Unlock()\u89e3\u9501\u540e\u624d\u80fd\u518d\u52a0\u9501<\/p>\n\n\n\n \u9501\u5b9a\u72b6\u6001\u4e0e goroutine \u6ca1\u6709\u5173\u8054\uff0c\u4e00\u4e2a goroutine \u53ef\u4ee5 Lock\uff0c\u53e6\u4e00\u4e2a goroutine \u53ef\u4ee5 Unlock<\/p>\n\n\n\n \u4e92\u65a5\u9501\u5141\u8bb8\u81ea\u65cb\u7684\u6761\u4ef6<\/strong><\/p>\n\n\n\n <\/a><\/a>\u7ebf\u7a0b\u6ca1\u6709\u83b7\u53d6\u5230\u9501\u65f6\u5e38\u89c1\u67092\u79cd\u5904\u7406\u65b9\u5f0f<\/strong><\/p>\n\n\n\n Go\u8bed\u8a00\u4e2d\u7684Mutex\u5b9e\u73b0\u4e86\u81ea\u65cb\u4e0e\u963b\u585e\u4e24\u79cd\u573a\u666f\uff0c\u5f53\u6ee1\u8db3\u4e0d\u4e86\u81ea\u65cb\u6761\u4ef6\u65f6\uff0c\u5c31\u4f1a\u8fdb\u5165\u963b\u585e<\/p>\n\n\n\n \u5141\u8bb8\u81ea\u65cb\u7684\u6761\u4ef6<\/strong><\/p>\n\n\n\n \u53ef\u91cd\u5165\u9501<\/strong><\/p>\n\n\n\n <\/a><\/a>\u6982\u5ff5<\/strong><\/p>\n\n\n\n \u53ef\u91cd\u5165\u9501\u53c8\u79f0\u4e3a\u9012\u5f52\u9501\uff0c\u662f\u4ee5\u7ebf\u7a0b\u4e3a\u5355\u4f4d\uff0c\u5f53\u4e00\u4e2a\u7ebf\u7a0b\u83b7\u53d6\u5bf9\u8c61\u9501\u4e4b\u540e\uff0c\u8fd9\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u518d\u6b21\u83b7\u53d6\u672c\u5bf9\u8c61\u4e0a\u7684\u9501\uff0c\u800c\u5176\u4ed6\u7684\u7ebf\u7a0b\u662f\u4e0d\u53ef\u4ee5\u3002(\u901a\u4fd7\u6765\u8bf4\uff1a\u53ef\u91cd\u5165\u9501\u4e5f\u662f\u4e00\u79cd\u6280\u672f\uff0c\u4efb\u610f\u7ebf\u7a0b\u5728\u83b7\u53d6\u9501\u4e4b\u540e\uff0c\u80fd\u591f\u518d\u6b21\u83b7\u53d6\u9501\uff0c\u800c\u4e0d\u4f1a\u88ab\u9501\u6240\u6392\u65a5)\u53ef\u91cd\u5165\u9501\u7684\u610f\u4e49\u4e4b\u4e00\u5728\u4e8e\u9632\u6b62\u6b7b\u9501<\/p>\n\n\n\n Mutex\u4e0d\u662f\u53ef\u91cd\u5165\u9501<\/strong><\/p>\n\n\n\n Mutex \u4e0d\u662f\u53ef\u91cd\u5165\u7684\u9501\u3002Mutex \u7684\u5b9e\u73b0\u4e2d\u6ca1\u6709\u8bb0\u5f55\u54ea\u4e2a goroutine \u62e5\u6709\u8fd9\u628a\u9501\u3002\u7406\u8bba\u4e0a\uff0c\u4efb\u4f55 goroutine \u90fd\u53ef\u4ee5\u968f\u610f\u5730 Unlock \u8fd9\u628a\u9501\uff0c\u6240\u4ee5\u6ca1\u529e\u6cd5\u8ba1\u7b97\u91cd\u5165\u6761\u4ef6\uff0c\u5e76\u4e14Mutex \u91cd\u590dLock\u4f1a\u5bfc\u81f4\u6b7b\u9501<\/p>\n\n\n\n \u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\u5c31\u4f1a\u62a5\u9519\uff1a<\/p>\n\n\n\n package main import ( “fmt” “sync” ) var mu sync.Mutex var chain string func A(){ mu.Lock() defer mu.Unlock() chain = chain + “–>A” B() } func B(){ chain = chain + “–>B” C() } func C(){ mu.Lock() defer mu.Unlock() chain = chain + “–>C” } func main() { chain = “main” A() fmt.Println(chain) }<\/p>\n\n\n\n \u5b9e\u73b0\u53ef\u91cd\u5165\u9501<\/strong><\/p>\n\n\n\n \u6b63\u5e38\u6a21\u5f0f(\u975e\u516c\u5e73\u9501)<\/strong><\/p>\n\n\n\n \u5728\u521a\u5f00\u59cb\u7684\u65f6\u5019\uff0c\u662f\u5904\u4e8e\u6b63\u5e38\u6a21\u5f0f\uff08Barging\uff09\uff0c\u4e5f\u5c31\u662f\uff0c\u5f53\u4e00\u4e2aG1\u6301\u6709\u7740\u4e00\u4e2a\u9501\u7684\u65f6\u5019\uff0cG2\u4f1a\u81ea\u65cb\u7684\u53bb\u5c1d\u8bd5\u83b7\u53d6\u8fd9\u4e2a\u9501<\/p>\n\n\n\n \u5f53\u81ea\u65cb\u8d85\u8fc74\u6b21\u8fd8\u6ca1\u6709\u80fd\u83b7\u53d6\u5230\u9501\u7684\u65f6\u5019\uff0c\u8fd9\u4e2aG2\u5c31\u4f1a\u88ab\u52a0\u5165\u5230\u83b7\u53d6\u9501\u7684\u7b49\u5f85\u961f\u5217\u91cc\u9762\uff0c\u5e76\u963b\u585e\u7b49\u5f85\u5524\u9192<\/p>\n\n\n\n \u6b63\u5e38\u6a21\u5f0f\u4e0b\uff0c\u6240\u6709\u7b49\u5f85\u9501\u7684 goroutine \u6309\u7167 FIFO(\u5148\u8fdb\u5148\u51fa)\u987a\u5e8f\u7b49\u5f85\u3002\u5524\u9192\u7684goroutine \u4e0d\u4f1a\u76f4\u63a5\u62e5\u6709\u9501\uff0c\u800c\u662f\u4f1a\u548c\u65b0\u8bf7\u6c42\u9501\u7684 goroutine \u7ade\u4e89\u9501\u3002\u65b0\u8bf7\u6c42\u9501\u7684 goroutine \u5177\u6709\u4f18\u52bf\uff1a\u5b83\u6b63\u5728 CPU \u4e0a\u6267\u884c\uff0c\u800c\u4e14\u53ef\u80fd\u6709\u597d\u51e0\u4e2a\uff0c\u6240\u4ee5\u521a\u521a\u5524\u9192\u7684 goroutine \u6709\u5f88\u5927\u53ef\u80fd\u5728\u9501\u7ade\u4e89\u4e2d\u5931\u8d25\uff0c\u957f\u65f6\u95f4\u83b7\u53d6\u4e0d\u5230\u9501\uff0c\u5c31\u4f1a\u5207\u6362\u5230\u9965\u997f\u6a21\u5f0f<\/p>\n\n\n\n \u9965\u997f\u6a21\u5f0f(\u516c\u5e73\u9501)<\/strong><\/p>\n\n\n\n \u5f53\u4e00\u4e2a goroutine \u7b49\u5f85\u9501\u65f6\u95f4\u8d85\u8fc7 1 \u6beb\u79d2\u65f6\uff0c\u5b83\u53ef\u80fd\u4f1a\u9047\u5230\u9965\u997f\u95ee\u9898\u3002 \u5728\u7248\u672c1.9\u4e2d\uff0c\u8fd9\u79cd\u573a\u666f\u4e0bGo Mutex \u5207\u6362\u5230\u9965\u997f\u6a21\u5f0f\uff08handoff\uff09\uff0c\u89e3\u51b3\u9965\u997f\u95ee\u9898<\/p>\n\n\n\n \u5982\u679c\u4e00\u4e2aG\u8fdb\u5165\u4e86\u9965\u997f\u6a21\u5f0f\uff0c\u90a3\u4e5f\u4e0d\u53ef\u80fd\u8bf4\u6c38\u8fdc\u7684\u4fdd\u6301\u4e00\u4e2a\u9965\u997f\u7684\u72b6\u6001\uff0c\u603b\u5f52\u4f1a\u6709\u5403\u9971\u7684\u65f6\u5019\uff0c\u4e5f\u5c31\u662f\u603b\u6709\u90a3\u4e48\u4e00\u523bMutex\u4f1a\u56de\u5f52\u5230\u6b63\u5e38\u6a21\u5f0f\uff0c\u90a3\u4e48\u56de\u5f52\u6b63\u5e38\u6a21\u5f0f\u5fc5\u987b\u5177\u5907\u7684\u6761\u4ef6\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a<\/p>\n\n\n\n G\u7684\u6267\u884c\u65f6\u95f4\u5c0f\u4e8e1ms<\/p>\n\n\n\n \u7b49\u5f85\u961f\u5217\u5df2\u7ecf\u5168\u90e8\u6e05\u7a7a\u4e86<\/p>\n\n\n\n \u5f53\u6ee1\u8db3\u4e0a\u8ff0\u4e24\u4e2a\u6761\u4ef6\u7684\u4efb\u610f\u4e00\u4e2a\u7684\u65f6\u5019\uff0cMutex\u4f1a\u5207\u6362\u56de\u6b63\u5e38\u6a21\u5f0f\uff0c\u800cGo\u7684\u62a2\u9501\u7684\u8fc7\u7a0b\uff0c\u5c31\u662f\u5728\u8fd9\u4e2a\u6b63\u5e38\u6a21\u5f0f\u548c\u9965\u997f\u6a21\u5f0f\u4e2d\u6765\u56de\u5207\u6362\u8fdb\u884c\u7684<\/p>\n\n\n\n \u8bfb\u5199\u4e92\u65a5\u9501RWMutex<\/strong><\/p>\n\n\n\n <\/a><\/a>\u6982\u5ff5<\/strong><\/p>\n\n\n\n \u8bfb\u5199\u4e92\u65a5\u9501RWMutex\uff0c\u662f\u5bf9Mutex\u7684\u4e00\u4e2a\u6269\u5c55\uff0c\u5f53\u4e00\u4e2a goroutine \u83b7\u5f97\u4e86\u8bfb\u9501\u540e\uff0c\u5176\u4ed6 goroutine\u53ef\u4ee5\u83b7\u53d6\u8bfb\u9501\uff0c\u4f46\u4e0d\u80fd\u83b7\u53d6\u5199\u9501\uff1b\u5f53\u4e00\u4e2a goroutine \u83b7\u5f97\u4e86\u5199\u9501\u540e\uff0c\u5176\u4ed6 goroutine\u65e2\u4e0d\u80fd\u83b7\u53d6\u8bfb\u9501\u4e5f\u4e0d\u80fd\u83b7\u53d6\u5199\u9501\uff08\u53ea\u80fd\u5b58\u5728\u4e00\u4e2a\u5199\u8005\u6216\u591a\u4e2a\u8bfb\u8005\uff0c\u53ef\u4ee5\u540c\u65f6\u8bfb\uff09<\/p>\n\n\n\n \u4f7f\u7528\u573a\u666f<\/strong><\/p>\n\n\n\n \u8bfb\u591a\u4e8e\u5199\u7684\u60c5\u51b5\uff08\u65e2\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\uff0c\u53c8\u4fdd\u8bc1\u6027\u80fd\u4e0d\u592a\u5dee\uff09<\/p>\n\n\n\n \u6ce8\u610f\u70b9<\/strong><\/p>\n\n\n\n \u8bfb\u9501\u6216\u5199\u9501\u5728 Lock() \u4e4b\u524d\u4f7f\u7528 Unlock() \u4f1a\u5bfc\u81f4 panic \u5f02\u5e38<\/p>\n\n\n\n \u4f7f\u7528 Lock() \u52a0\u9501\u540e\uff0c\u518d\u6b21 Lock() \u4f1a\u5bfc\u81f4\u6b7b\u9501\uff08\u4e0d\u652f\u6301\u91cd\u5165\uff09\uff0c\u9700Unlock()\u89e3\u9501\u540e\u624d\u80fd\u518d\u52a0\u9501<\/p>\n\n\n\n \u9501\u5b9a\u72b6\u6001\u4e0e goroutine \u6ca1\u6709\u5173\u8054\uff0c\u4e00\u4e2a goroutine \u53ef\u4ee5 RLock\uff08Lock\uff09\uff0c\u53e6\u4e00\u4e2a goroutine \u53ef\u4ee5 RUnlock\uff08Unlock\uff09<\/p>\n\n\n\n \u4e92\u65a5\u9501\u548c\u8bfb\u5199\u9501\u7684\u533a\u522b<\/strong><\/p>\n\n\n\n <\/p>\n","protected":false},"excerpt":{"rendered":" Mutex\u7684\u6982\u5ff5 \u4f7f\u7528\u573a\u666f \u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee\u4e34\u754c\u533a\uff0c\u4e3a\u4fdd\u8bc1\u6570\u636e\u7684\u5b89\u5168...<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.greatfar.cn\/wp-json\/wp\/v2\/posts\/32"}],"collection":[{"href":"https:\/\/www.greatfar.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.greatfar.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.greatfar.cn\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.greatfar.cn\/wp-json\/wp\/v2\/comments?post=32"}],"version-history":[{"count":1,"href":"https:\/\/www.greatfar.cn\/wp-json\/wp\/v2\/posts\/32\/revisions"}],"predecessor-version":[{"id":34,"href":"https:\/\/www.greatfar.cn\/wp-json\/wp\/v2\/posts\/32\/revisions\/34"}],"wp:attachment":[{"href":"https:\/\/www.greatfar.cn\/wp-json\/wp\/v2\/media?parent=32"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.greatfar.cn\/wp-json\/wp\/v2\/categories?post=32"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.greatfar.cn\/wp-json\/wp\/v2\/tags?post=32"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}\n
<\/figure>\n\n\n\n
<\/figure>\n\n\n\n
<\/figure>\n\n\n\n
<\/figure>\n\n\n\n
\n
\n
\n
\n