gpt4 book ai didi

go - 何时使用互斥量

转载 作者:IT王子 更新时间:2023-10-29 02:30:53 24 4
gpt4 key购买 nike

Go 真的需要它们吗?我读过 https://gobyexample.com/mutexes并多次运行示例代码,当我删除 mutex.Lock()mutex.Unlock() 时,它的工作原理完全相同。

最佳答案

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.


正如预期的那样,当我删除互斥量时,我遇到了数据争用和 panic 。


输出:

$ 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 - 何时使用互斥量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55196635/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com