何时使用互斥量
When to use mutexes
Go 真的需要它们吗?我已经多次阅读 https://gobyexample.com/mutexes 和 运行 示例代码,当我删除 mutex.Lock()
和 mutex.Unlock()
时,它的工作原理完全相同。
是的,go中的map不是线程安全的。在此示例中,地图访问可能发生在多个线程中。当映射从多个线程写入或从多个线程写入和读取时,将发生未定义的行为。但是,何时以及是否会发生这种情况取决于时机。因此,删除互斥量可能会导致数据损坏或在将来的某个时候崩溃。
您无法同时从多个 go 例程访问 map
。
它可能会工作一段时间,但注定会失败,或者导致意想不到的结果。
Mutex 保证一次只能有一个 go 例程对 Lock
和 Unlock
之间的代码片段进行操作。
或者,您可以使用 sync.Map
,它对于读写是线程安全的。
m := new(sync.Map)
go func() {
for r := 0; true; r++ {
m.Store(r, r)
time.Sleep(time.Millisecond)
}
}()
go func() {
for r := 0; true; r++ {
res, ok := m.Load(r)
if ok {
fmt.Println(res)
}
time.Sleep(10 * time.Millisecond)
}
}()
sync.Map并不总是意味着线程安全
sync.Map
读取(加载)和写入(存储)是原子的,即一次从不同的 go 例程调用它们将工作,因为 expected/won 不会损坏数据或抛出错误。
但是,组合不同的 sync.Map
可能不是原子的,因此不是线程安全的。
例如,
val, ok := m.Load("someKey")
if !ok {
m.Store("someKey", LoadData())
}
如果此代码同时从不同的 go 例程运行,则两个 go 例程都有可能进入 if
语句并加载数据,即使这不是预期的。
所以有时你可能最终需要使用互斥锁而不是 sync.Map
val, ok := m.Load("someKey")
if !ok {
mutex.Lock()
defer mutex.Unlock()
val, ok = m.Load("someKey")
if !ok {
m.Store("someKey", LoadData())
}
}
I've read https://gobyexample.com/mutexes and run example code
multiple times, when I remove mutex.Lock() and mutex.Unlock() it works
exactly the same.
正如预期的那样,当我删除互斥锁时,我遇到了数据争用和恐慌。
输出:
$ go run -race racer.go
fatal error: concurrent map read and map write
==================
WARNING: DATA RACE
Write at 0x00c00009a690 by goroutine 113:
runtime.mapassign_fast64()
/home/peter/go/src/runtime/map_fast64.go:92 +0x0
main.main.func2()
/home/peter/gopath/src/so/racer.go:56 +0xba
Previous read at 0x00c00009a690 by goroutine 64:
runtime.mapaccess1_fast64()
/home/peter/go/src/runtime/map_fast64.go:12 +0x0
main.main.func1()
/home/peter/gopath/src/so/racer.go:37 +0x72
Goroutine 113 (running) created at:
main.main()
/home/peter/gopath/src/so/racer.go:51 +0x124
Goroutine 64 (running) created at:
main.main()
/home/peter/gopath/src/so/racer.go:29 +0xe0
==================
==================
WARNING: DATA RACE
Write at 0x00c00009a690 by goroutine 115:
runtime.mapassign_fast64()
/home/peter/go/src/runtime/map_fast64.go:92 +0x0
main.main.func2()
/home/peter/gopath/src/so/racer.go:56 +0xba
Previous read at 0x00c00009a690 by goroutine 39:
runtime.mapaccess1_fast64()
/home/peter/go/src/runtime/map_fast64.go:12 +0x0
main.main.func1()
/home/peter/gopath/src/so/racer.go:37 +0x72
Goroutine 115 (running) created at:
main.main()
/home/peter/gopath/src/so/racer.go:51 +0x124
Goroutine 39 (running) created at:
main.main()
/home/peter/gopath/src/so/racer.go:29 +0xe0
==================
==================
WARNING: DATA RACE
Read at 0x00c0001f0048 by goroutine 79:
main.main.func1()
/home/peter/gopath/src/so/racer.go:37 +0x80
Previous write at 0x00c0001f0048 by goroutine 113:
main.main.func2()
/home/peter/gopath/src/so/racer.go:56 +0xcf
Goroutine 79 (running) created at:
main.main()
/home/peter/gopath/src/so/racer.go:29 +0xe0
Goroutine 113 (running) created at:
main.main()
/home/peter/gopath/src/so/racer.go:51 +0x124
==================
==================
WARNING: DATA RACE
Read at 0x00c0001f0050 by goroutine 81:
main.main.func1()
/home/peter/gopath/src/so/racer.go:37 +0x80
Previous write at 0x00c0001f0050 by goroutine 115:
main.main.func2()
/home/peter/gopath/src/so/racer.go:56 +0xcf
Goroutine 81 (running) created at:
main.main()
/home/peter/gopath/src/so/racer.go:29 +0xe0
Goroutine 115 (running) created at:
main.main()
/home/peter/gopath/src/so/racer.go:51 +0x124
==================
==================
WARNING: DATA RACE
Read at 0x00c0001f0050 by goroutine 106:
main.main.func1()
/home/peter/gopath/src/so/racer.go:37 +0x80
Previous write at 0x00c0001f0050 by goroutine 115:
main.main.func2()
/home/peter/gopath/src/so/racer.go:56 +0xcf
Goroutine 106 (running) created at:
main.main()
/home/peter/gopath/src/so/racer.go:29 +0xe0
Goroutine 115 (running) created at:
main.main()
/home/peter/gopath/src/so/racer.go:51 +0x124
==================
==================
WARNING: DATA RACE
Read at 0x00c0001f0050 by goroutine 94:
main.main.func1()
/home/peter/gopath/src/so/racer.go:37 +0x80
Previous write at 0x00c0001f0050 by goroutine 115:
main.main.func2()
/home/peter/gopath/src/so/racer.go:56 +0xcf
Goroutine 94 (running) created at:
main.main()
/home/peter/gopath/src/so/racer.go:29 +0xe0
Goroutine 115 (running) created at:
main.main()
/home/peter/gopath/src/so/racer.go:51 +0x124
==================
==================
WARNING: DATA RACE
Write at 0x00c0001f0050 by goroutine 114:
main.main.func2()
/home/peter/gopath/src/so/racer.go:56 +0xcf
Previous write at 0x00c0001f0050 by goroutine 115:
main.main.func2()
/home/peter/gopath/src/so/racer.go:56 +0xcf
Goroutine 114 (running) created at:
main.main()
/home/peter/gopath/src/so/racer.go:51 +0x124
Goroutine 115 (running) created at:
main.main()
/home/peter/gopath/src/so/racer.go:51 +0x124
==================
exit status 2
$
racer.go
:
package main
import (
"fmt"
"math/rand"
//*"sync"
"sync/atomic"
"time"
)
func main() {
// For our example the state will be a map.
var state = make(map[int]int)
// This mutex will synchronize access to state.
//*var mutex = &sync.Mutex{}
// We’ll keep track of how many read and write operations we do.
var readOps uint64
var writeOps uint64
// Here we start 100 goroutines to execute repeated reads against the state, once per millisecond in each goroutine.
for r := 0; r < 100; r++ {
go func() {
total := 0
for {
// For each read we pick a key to access, Lock() the mutex to ensure exclusive access to the state, read the value at the chosen key, Unlock() the mutex, and increment the readOps count.
key := rand.Intn(5)
//*mutex.Lock()
total += state[key]
//*mutex.Unlock()
atomic.AddUint64(&readOps, 1)
// Wait a bit between reads.
time.Sleep(time.Millisecond)
}
}()
}
// We’ll also start 10 goroutines to simulate writes, using the same pattern we did for reads.
for w := 0; w < 10; w++ {
go func() {
for {
key := rand.Intn(5)
val := rand.Intn(100)
//*mutex.Lock()
state[key] = val
//*mutex.Unlock()
atomic.AddUint64(&writeOps, 1)
time.Sleep(time.Millisecond)
}
}()
}
// Let the 10 goroutines work on the state and mutex for a second.
time.Sleep(time.Second)
// Take and report final operation counts.
readOpsFinal := atomic.LoadUint64(&readOps)
fmt.Println("readOps:", readOpsFinal)
writeOpsFinal := atomic.LoadUint64(&writeOps)
fmt.Println("writeOps:", writeOpsFinal)
// With a final lock of state, show how it ended up.
//*mutex.Lock()
fmt.Println("state:", state)
//*mutex.Unlock()
}
Go 真的需要它们吗?我已经多次阅读 https://gobyexample.com/mutexes 和 运行 示例代码,当我删除 mutex.Lock()
和 mutex.Unlock()
时,它的工作原理完全相同。
是的,go中的map不是线程安全的。在此示例中,地图访问可能发生在多个线程中。当映射从多个线程写入或从多个线程写入和读取时,将发生未定义的行为。但是,何时以及是否会发生这种情况取决于时机。因此,删除互斥量可能会导致数据损坏或在将来的某个时候崩溃。
您无法同时从多个 go 例程访问 map
。
它可能会工作一段时间,但注定会失败,或者导致意想不到的结果。
Mutex 保证一次只能有一个 go 例程对 Lock
和 Unlock
之间的代码片段进行操作。
或者,您可以使用 sync.Map
,它对于读写是线程安全的。
m := new(sync.Map)
go func() {
for r := 0; true; r++ {
m.Store(r, r)
time.Sleep(time.Millisecond)
}
}()
go func() {
for r := 0; true; r++ {
res, ok := m.Load(r)
if ok {
fmt.Println(res)
}
time.Sleep(10 * time.Millisecond)
}
}()
sync.Map并不总是意味着线程安全
sync.Map
读取(加载)和写入(存储)是原子的,即一次从不同的 go 例程调用它们将工作,因为 expected/won 不会损坏数据或抛出错误。
但是,组合不同的 sync.Map
可能不是原子的,因此不是线程安全的。
例如,
val, ok := m.Load("someKey")
if !ok {
m.Store("someKey", LoadData())
}
如果此代码同时从不同的 go 例程运行,则两个 go 例程都有可能进入 if
语句并加载数据,即使这不是预期的。
所以有时你可能最终需要使用互斥锁而不是 sync.Map
val, ok := m.Load("someKey")
if !ok {
mutex.Lock()
defer mutex.Unlock()
val, ok = m.Load("someKey")
if !ok {
m.Store("someKey", LoadData())
}
}
I've read https://gobyexample.com/mutexes and run example code multiple times, when I remove mutex.Lock() and mutex.Unlock() it works exactly the same.
正如预期的那样,当我删除互斥锁时,我遇到了数据争用和恐慌。
输出:
$ go run -race racer.go
fatal error: concurrent map read and map write
==================
WARNING: DATA RACE
Write at 0x00c00009a690 by goroutine 113:
runtime.mapassign_fast64()
/home/peter/go/src/runtime/map_fast64.go:92 +0x0
main.main.func2()
/home/peter/gopath/src/so/racer.go:56 +0xba
Previous read at 0x00c00009a690 by goroutine 64:
runtime.mapaccess1_fast64()
/home/peter/go/src/runtime/map_fast64.go:12 +0x0
main.main.func1()
/home/peter/gopath/src/so/racer.go:37 +0x72
Goroutine 113 (running) created at:
main.main()
/home/peter/gopath/src/so/racer.go:51 +0x124
Goroutine 64 (running) created at:
main.main()
/home/peter/gopath/src/so/racer.go:29 +0xe0
==================
==================
WARNING: DATA RACE
Write at 0x00c00009a690 by goroutine 115:
runtime.mapassign_fast64()
/home/peter/go/src/runtime/map_fast64.go:92 +0x0
main.main.func2()
/home/peter/gopath/src/so/racer.go:56 +0xba
Previous read at 0x00c00009a690 by goroutine 39:
runtime.mapaccess1_fast64()
/home/peter/go/src/runtime/map_fast64.go:12 +0x0
main.main.func1()
/home/peter/gopath/src/so/racer.go:37 +0x72
Goroutine 115 (running) created at:
main.main()
/home/peter/gopath/src/so/racer.go:51 +0x124
Goroutine 39 (running) created at:
main.main()
/home/peter/gopath/src/so/racer.go:29 +0xe0
==================
==================
WARNING: DATA RACE
Read at 0x00c0001f0048 by goroutine 79:
main.main.func1()
/home/peter/gopath/src/so/racer.go:37 +0x80
Previous write at 0x00c0001f0048 by goroutine 113:
main.main.func2()
/home/peter/gopath/src/so/racer.go:56 +0xcf
Goroutine 79 (running) created at:
main.main()
/home/peter/gopath/src/so/racer.go:29 +0xe0
Goroutine 113 (running) created at:
main.main()
/home/peter/gopath/src/so/racer.go:51 +0x124
==================
==================
WARNING: DATA RACE
Read at 0x00c0001f0050 by goroutine 81:
main.main.func1()
/home/peter/gopath/src/so/racer.go:37 +0x80
Previous write at 0x00c0001f0050 by goroutine 115:
main.main.func2()
/home/peter/gopath/src/so/racer.go:56 +0xcf
Goroutine 81 (running) created at:
main.main()
/home/peter/gopath/src/so/racer.go:29 +0xe0
Goroutine 115 (running) created at:
main.main()
/home/peter/gopath/src/so/racer.go:51 +0x124
==================
==================
WARNING: DATA RACE
Read at 0x00c0001f0050 by goroutine 106:
main.main.func1()
/home/peter/gopath/src/so/racer.go:37 +0x80
Previous write at 0x00c0001f0050 by goroutine 115:
main.main.func2()
/home/peter/gopath/src/so/racer.go:56 +0xcf
Goroutine 106 (running) created at:
main.main()
/home/peter/gopath/src/so/racer.go:29 +0xe0
Goroutine 115 (running) created at:
main.main()
/home/peter/gopath/src/so/racer.go:51 +0x124
==================
==================
WARNING: DATA RACE
Read at 0x00c0001f0050 by goroutine 94:
main.main.func1()
/home/peter/gopath/src/so/racer.go:37 +0x80
Previous write at 0x00c0001f0050 by goroutine 115:
main.main.func2()
/home/peter/gopath/src/so/racer.go:56 +0xcf
Goroutine 94 (running) created at:
main.main()
/home/peter/gopath/src/so/racer.go:29 +0xe0
Goroutine 115 (running) created at:
main.main()
/home/peter/gopath/src/so/racer.go:51 +0x124
==================
==================
WARNING: DATA RACE
Write at 0x00c0001f0050 by goroutine 114:
main.main.func2()
/home/peter/gopath/src/so/racer.go:56 +0xcf
Previous write at 0x00c0001f0050 by goroutine 115:
main.main.func2()
/home/peter/gopath/src/so/racer.go:56 +0xcf
Goroutine 114 (running) created at:
main.main()
/home/peter/gopath/src/so/racer.go:51 +0x124
Goroutine 115 (running) created at:
main.main()
/home/peter/gopath/src/so/racer.go:51 +0x124
==================
exit status 2
$
racer.go
:
package main
import (
"fmt"
"math/rand"
//*"sync"
"sync/atomic"
"time"
)
func main() {
// For our example the state will be a map.
var state = make(map[int]int)
// This mutex will synchronize access to state.
//*var mutex = &sync.Mutex{}
// We’ll keep track of how many read and write operations we do.
var readOps uint64
var writeOps uint64
// Here we start 100 goroutines to execute repeated reads against the state, once per millisecond in each goroutine.
for r := 0; r < 100; r++ {
go func() {
total := 0
for {
// For each read we pick a key to access, Lock() the mutex to ensure exclusive access to the state, read the value at the chosen key, Unlock() the mutex, and increment the readOps count.
key := rand.Intn(5)
//*mutex.Lock()
total += state[key]
//*mutex.Unlock()
atomic.AddUint64(&readOps, 1)
// Wait a bit between reads.
time.Sleep(time.Millisecond)
}
}()
}
// We’ll also start 10 goroutines to simulate writes, using the same pattern we did for reads.
for w := 0; w < 10; w++ {
go func() {
for {
key := rand.Intn(5)
val := rand.Intn(100)
//*mutex.Lock()
state[key] = val
//*mutex.Unlock()
atomic.AddUint64(&writeOps, 1)
time.Sleep(time.Millisecond)
}
}()
}
// Let the 10 goroutines work on the state and mutex for a second.
time.Sleep(time.Second)
// Take and report final operation counts.
readOpsFinal := atomic.LoadUint64(&readOps)
fmt.Println("readOps:", readOpsFinal)
writeOpsFinal := atomic.LoadUint64(&writeOps)
fmt.Println("writeOps:", writeOpsFinal)
// With a final lock of state, show how it ended up.
//*mutex.Lock()
fmt.Println("state:", state)
//*mutex.Unlock()
}