go


How to understand the “Incorrect synchronization” samples in Go Memory Model


I am just starting learning golang, when reading Go Memory Model I got a question to understand what it says about "Another incorrect idiom is busy waiting for a value,"
var a string
var done bool
func setup() {
a = "hello, world"
done = true
}
func main() {
go setup()
for !done {
}
print(a)
}
It says:
"Worse, there is no guarantee that the write to done will ever be observed by main, since there are no synchronization events between the two threads. The loop in main is not guaranteed to finish."
I know that the order of writes to 'a' and 'done' is not deterministic in setup(), My question is: why main is not guaranteed to see the write to done?
Thank you
package main
var a string
var done bool
func setup() {
a = "hello, world"
done = true
}
func main() {
go setup()
for !done {
}
println(a)
}
You have two goroutines, main and setup. For example, assume that they are running on separate threads on separate CPUs with local memory cache. Assume that both main and setup read the done variable into a local CPU memory cache. The setup goroutine updates done in its local memory cache, which does lazy writes. Since there is no cache synchronization event between the independent main and setup goroutines there is no guarantee of cache coherency, no guarantee that main memory and both CPU memory caches will be synchronized. Different hardware handles this differently. In Go, only the lowest common denominator can be guaranteed.
See Cache coherence.

Related Links

Golang PutItem DynamoDB: Runtime Error invalid memory address or nil pointer dereference
golang what is import side effect
what does not being thread safe means about maps in Go?
Sending SIGTSTP suspends entire program
How to find a package name from given call in runtime?
How fast is the go 1.5 gc with terabytes of RAM?
How do I format a currency with commas and 2 decimal places?
How to contain space in value string of link flag when using go build
Extract from tar file in Go
GoLang: Reading a File Basics
How can two different types implement the same method in golang using interfaces?
How can I convert string to integer in golang
mgo: Query ObjectId for range of time values
From UnixNano() to Time{}
BlueMix not starting due to health check timeout
Is there a way to append/remove a resource to a binary at execution time?

Categories

HOME
phpword
amp-html
pyqt
mysql-workbench
mstest
package
sublimetext
replace
alarm
static-site
maxscript
jquery-ui
google-chrome-app
activecollab
redis-sentinel
google-form
stellar.js
favicon
screen-readers
nodemcu
database-connection
symbols
visual-glitch
href
onload
fable-f#
oculus
failover
panoramas
card.io
hunspell
modalviewcontroller
lubridate
hivemq
watchman
modernizr
mongoose-schema
importerror
facebook-pixel
tinymce-3
d3-force-directed
juice-ui
slackware
simplepie
microstation
resourcemanager
avspeechsynthesizer
firebase-security
kango-framework
angular-ui-typeahead
nio2
np-spring
maybe
ons-api
rubaxa-sortable
binomial-theorem
packet-sniffers
sonarqube-5.0
cbind
strawberry-perl
renderer
android-audiorecord
model-driven-development
mbunit
asdf
dolby-audio-api
octal
pagedown
chart-director
virtual-pc
twisted.web
wcf-configuration
objectquery
convention
addchild
zen
alsb

Resources

Encrypt Message