Programs that modify data being simultaneously accessed by multiple 
goroutines must serialize such access.   
https://go.dev/ref/mem   

$ go run -race racer.go
00
==================
WARNING: DATA RACE
Write at 0x00000054d5f8 by goroutine 6:
  main.main.func1()
      /home/peter/racer.go:7 +0x29

Previous read at 0x00000054d5f8 by main goroutine:
  main.main()
      /home/peter/racer.go:9 +0x3c

Goroutine 6 (running) created at:
  main.main()
      /home/peter/racer.go:6 +0x30
==================
Found 1 data race(s)
exit status 66
$ 

peter

On Friday, December 2, 2022 at 7:13:09 AM UTC-5 nobis...@gmail.com wrote:

> > So the second read can observe x = 0 even if the first read observes x = 
> 0.
>
> Sorry, I meant "So the second read can observe x = 0 even if the first 
> read observes x = 1." here.
>
> 2022年12月2日(金) 21:10 のびしー <nobis...@gmail.com>:
>
>> Hello, I have another question regarding the Go Memory Model. 
>>
>> (1) Can this program print "10", according to the Memory Model?
>> (2) If that is forbidden, which part of the Go Memory Model excludes such 
>> behavior?
>>
>> https://go.dev/play/p/Fn5I0fjSiKj
>>
>> ```go
>> package main
>>
>> var x int = 0
>>
>> func main() {
>> go func() {
>> x = 1
>> }()
>> print(x) // first read: 1
>> print(x) // second read: 0
>> }
>> ```
>>
>> I draw a picture of the happens-before relation of this program here:
>>
>>
>> https://gist.github.com/nobishino/8150346c30101e2ca409ed83c6c25add?permalink_comment_id=4388680#gistcomment-4388680
>>
>> I think the answer to (1) is yes. 
>> Both reads are concurrent with x = 1, so each read can observe both x = 0 
>> and x = 1.
>> And there is no constraint between the results of the first read and the 
>> second read.
>> So the second read can observe x = 0 even if the first read observes x = 
>> 0.
>>
>> But I'm not very sure. Is this understanding correct?
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/6fae3e5e-2a09-4d54-82aa-54b2ef9a3a8cn%40googlegroups.com.

Reply via email to