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":{}}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章