Go 併發編程-共享變量

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在之前的文章中,我們詳細說了 Go 語言中 goroutine + channel 通過通信的方式來共享內存,從而實現併發編程。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"但同時 Go 也提供了傳統通過共享變量,也就是共享內存的方式來實現併發。這篇文章會介紹 Go","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"提供的相關機制。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"1. 什麼是競態","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在一個 Go 程序運行起來之後,會有很多的 goroutine 同時運行,每個 goroutine 中代碼的執行是順序的,如果我們無法確定兩個 goroutine 中代碼的執行順序。就可以說這兩個 goroutine 是併發執行的。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果一段代碼無論是順序執行還是併發執行,結果都是正確的,那就可以說這個代碼是併發安全的。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"併發不安全的代碼出現的問題有多種,比如死鎖、活鎖、經態等等。死鎖和活鎖都表示代碼已經無法繼續執行了,而競態則表示代碼是可以執行的,但是有可能會出現錯誤的結果。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有一個典型的例子就是向銀行賬戶中存款:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"go"},"content":[{"type":"text","text":"var balance int\nfunc Deposit(amount int) {\n balance = balance + amount\n}\nfunc Balance() int {\n return balance\n}\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"假如現在有兩個人同時向這個賬戶中存款,各自存了 100 次:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"go"},"content":[{"type":"text","text":"for i := 0; i < 100; i++ {\n go func() {\n Deposit(100)\n }()\n\n go func() {\n Deposit(100)\n }()\n}\n// 休眠一秒,讓上面的 goroutine 執行完成\ntime.Sleep(1 * time.Second)\nfmt.Println(Balance())\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果程序正確,那麼最後的輸出應該是 20000,但多次運行,結果可能是 19800、19900 或者其他的值。這個時候,我們就會說這個程序存在數據競態。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這個問題的根本原因是 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"balance = balance + amount","attrs":{}}],"attrs":{}},{"type":"text","text":" 這行代碼在 CPU 上的執行操作不是原子的,有可能執行到一半的時候會被打斷。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"2. 如何消除競態","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"發生了競態,就要想辦法解決。總的來說,解決競態有三種辦法:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"不要修改變量","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果一個變量不需要修改,在任何地方訪問都是安全的,但這個方法卻無法解決上面的問題。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"不要多個 goroutine 中去訪問同一個變量","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們前面說聊過的 goroutine + channel 就是這樣的一個思路,通過 channel 阻塞來更新變量,這也符合 Go 代碼的設計理念:","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"不要通過共享內存來通信,而應該通過通信來共享內存","attrs":{}},{"type":"text","text":"。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"同一時間只允許一個 goroutine 訪問變量","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果在同一時間只能有一個 goroutine 訪問變量,其他的 goruotine 需要等到當前的訪問結束之後,才能訪問,這樣也可以消除競態,下面將要說到的工具就是用來保證同一時間只能有一個 goroutine 來訪問變量。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"3. Go 提供的併發工具","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在上面我們已經說到了解決競態的三種辦法,下面的這些工具就是 Go 中用來實現同一時間只能有一個 goroutine 訪問變量。我們分別來看一下:","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"3.1 互斥鎖","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這個是解決競態最經典的工具,它的原理就是如果要訪問一個資源,那麼就必須要拿到這個資源的鎖,只有拿到鎖纔有資格訪問資源,其他的 goroutine 想要訪問,必須等到當前 goroutine 釋放了鎖,搶到鎖之後再訪問","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在使用之前,需要先爲資源申請一把鎖,使用的就是 sync.Mutex,這是 Go 語言中互斥鎖的實現:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"go"},"content":[{"type":"text","text":"var mu sync.Mutex\nvar balance int\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"每個拿到鎖的 goroutine 都需要保證在對變量的訪問結束之後,把鎖釋放掉,即使發生在異常情況,也需要釋放,這裏可以使用 defer 來保證最終會釋放鎖:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"go"},"content":[{"type":"text","text":"func Deposit(amount int) {\n mu.Lock()\n defer mu.Unlock()\n balance = balance + amount\n}\n\nfunc Balance() int {\n mu.Lock()\n defer mu.Unlock()\n return balance\n}\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"把代碼改完之後,再去運行上面存款的代碼,無論運行多少遍,最終的結果都是 20000,到這裏,我們競態的問題就算是解決了,但是還有點小問題。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"3.2 讀寫互斥鎖","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上面的互斥鎖解決了訪問數據的競態問題,但是還有個小問題,就是讀餘額的操作有點低效,每次來讀餘額的時候,都還需要去搶鎖,實際上,這個變量如果沒有改變,即使同時被多個 goroutine 讀,也不會產生併發安全的問題。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們想要的一個理想的場景就是,如果這個變量沒有在寫入,就可以運行多個 goroutine 同時讀,這樣可以大大提高效率。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Go 也提供了這個工具,那就是","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"讀寫鎖","attrs":{}},{"type":"text","text":"。這個鎖讀與讀是不互斥的,簡單來說就是這個鎖可以保證同時只能有一個 goroutine 在寫入,如果有 goroutine 在寫入,其他的 goroutine 既不能讀,也不能寫,但允許多個 goroutine 同時來讀。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們把上面的代碼再改一下,只需要改一個地方:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"go"},"content":[{"type":"text","text":"var mu sync.RWMutex // 替換 sync.Mutex\nvar balance int\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這樣改完之後,上面存款的代碼還是會一直輸出 20000,但同時可以允許多個 goroutine 同時讀餘額。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"大多數 Go 語言中的競態問題都可以使用這兩個工具來解決。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"3.3 Once","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Go 語言中還提供了這樣的一個工具,可以保證代碼只會執行一遍,多用於資源初始化等場景。使用的方式也很簡單:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"go"},"content":[{"type":"text","text":"o := &sync.Once{}\nfor i := 0; i < 100; i++ {\n o.Do(func(){\n go func() {\n Deposit(100)\n }()\n\n go func() {\n Deposit(100)\n }()\n })\n}\n// 休眠一秒,讓上面的 goroutine 執行完成\ntime.Sleep(1 * time.Second)\nfmt.Println(Balance())\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果上面的代碼使用 Once 來控制之後,都只會存一次,所以上面的代碼會永遠輸出 200。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"3.4 競態檢測器","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"很多處在競態的錯誤很難發現,Go 語言中提供了一個工具,可以幫忙檢查代碼中是否存在競態。使用起來很簡單,只需要在以下命令之後加上 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"-race","attrs":{}}],"attrs":{}},{"type":"text","text":" 參數就可以:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"$ go run -race\n\n$ go build -race\n\n$ go test -race\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"加上這個參數之後,編譯器會對代碼在執行時對所有共享變量的訪問,如果發現一個 goroutine 寫入一個變量之後,沒有任何同步的操作,就有另外一個 goroutine 讀寫了這個變量,那就說明這裏存在競態,就會報錯。比如下面的代碼:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"bash"},"content":[{"type":"text","text":"data := 1\n\ngo func() {\n data = 2\n}()\n\ngo func() {\n data = 3\n}()\n\ntime.Sleep(2 * time.Second)\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"運行 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"go run -race main.go","attrs":{}}],"attrs":{}},{"type":"text","text":" 之後,會報下面的錯誤:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"bash"},"content":[{"type":"text","text":"Found 1 data race(s)\nexit status 66\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"4. 小結","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Go 中也提供了傳統語言所提供的併發編程機制,也可以通過共享內存的方法來實現併發編程。Go 提供的接口相對來說比較簡潔,提供的能力卻足夠強大。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#FF7021","name":"orange"}}],"text":"文 / Rayjun","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"本文首發於微信公衆號【Rayjun】","attrs":{}}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章