by echlebek · 8y ago · 0 comments
For example, the author's final example. (And maybe others, I didn't read them all.)
$ go run -race racy.go
==================
WARNING: DATA RACE
Write at 0x00c42009800f by goroutine 6:
main.main.func1()
/home/eric/racy.go:12 +0x38
Previous read at 0x00c42009800f by main goroutine:
main.main()
/home/eric/racy.go:15 +0x8b
Goroutine 6 (running) created at:
main.main()
/home/eric/racy.go:11 +0x76
==================
done!
Found 1 data race(s)
exit status 66
edit: add code listing package main
import (
"fmt"
"runtime"
)
func main() {
done := false
go func(){
done = true
}()
for !done {
runtime.Gosched()
}
fmt.Println("done!")
}That code is intentionally simplified to make a point about the scheduling gotcha :-) The idea there was to show that a tight loop in one goroutine ( where the scheduler doesn't have an opportunity to execute) can prevent other goroutines from executing.
After setting done = true, is there no guarantee that the goroutine will complete before the for loop ends?
Of course synchronization in-general is a must. But using a bool here with one writer and continuous-reading-until is probably the least-dangerous-of-possible-examples.. even a number or anything slicish incl strings combined with an operator other than equals would, in the same code-snippet length, make the dangers illustrable and live-observable when running even without `-race` =)
You are expecting the read operation to be done either before or after the write, which would be the case if atomicity was guaranteed. There might be ugly intermediate states, or maybe there aren't, but don't know because there are no guarantees.
See also: http://preshing.com/20130618/atomic-vs-non-atomic-operations...
func main() {
doneMutex := sync.Mutex{}
done := false
go func(){
doneMutex.Lock()
defer doneMutex.Unlock()
done = true
}()
getDone := func() bool {
doneMutex.Lock()
defer doneMutex.Unlock()
return done
}
for !getDone() {
runtime.Gosched()
}
fmt.Println("done!")
}
Another option would be to use a channel in place of done.Effectively, the lesson here is that go doesn't have concurrently safe types (e.g. no concurrent map) nor generics to create them, so all concurrent code must be built around the builtin generic concurrent-safe type (channels) or must make careful use of mutexes / the atomic package / etc.
Idiomatic concurrent go either is built around channels or mutexes, and in either case the compiler won't tell you if you messed up, only the runtime race detector and testing will save you.
Would using a sync.WaitGroup and closure be wrong? Is mutex better for shared memory?
>The Map type is optimized for two common use cases: (1) when the entry for a given key is only ever written once but read many times, as in caches that only grow, or (2) when multiple goroutines read, write, and overwrite entries for disjoint sets of keys. In these two cases, use of a Map may significantly reduce lock contention compared to a Go map paired with a separate Mutex or RWMutex.