The question seems to be: "I know this is a data race, but isn't this a *safe* data race?"
To which the answer should be: "There are no safe data races". https://docs.google.com/document/d/1WAT22FtFdMt43i3O8YrnlQTNTzZ3IoJBtu3P2DNRytg/ # was previously: https://software.intel.com/en-us/blogs/2013/01/06/benign-data-races-what-could-possibly-go-wrong On Saturday 6 July 2024 at 22:23:44 UTC+1 burak serdar wrote: > The way I read the section about programs with memory races > (https://go.dev/ref/mem#restrictions), that program should always > print 1. > > "A read r of a memory location x holding a value that is not larger > than a machine word must observe some write w such that r does not > happen before w and there is no write w' such that w happens before w' > and w' happens before r. That is, each read must observe a value > written by a preceding or concurrent write." > > i=0 happens before all i=1s, and at least one i=1 happens before the read > of i. > > > On Sat, Jul 6, 2024 at 3:04 PM Ian Lance Taylor <ia...@golang.org> wrote: > > > > On Sat, Jul 6, 2024 at 7:34 AM 'qiu laidongfeng2' via golang-nuts > > <golan...@googlegroups.com> wrote: > > > > > > I know not to write code with data races? > > > The problem is that I find that this code always outputs 1 on the > amd64 machine, > > > I am not sure what effect this data competition has on the execution > result, > > > whether it is an inevitable result of the amd64 machine to always > output 1 or I am lucky to encounter that it is always output 1, so I ask. > > > > I would be surprised if that program ever prints anything other than 1 > > in practice. But I am sometimes surprised. > > > > Ian > > > > > > > 在2024年7月6日星期六 UTC+8 22:20:57<Ian Lance Taylor> 写道: > > >> > > >> On Sat, Jul 6, 2024 at 7:04 AM 'qiu laidongfeng2' via golang-nuts > > >> <golan...@googlegroups.com> wrote: > > >> > > > >> > Does data race in this program affect execution results? > > >> > In amd64, it always output1. > > >> > > >> If you want to write code like this, use the sync/atomic package. > > >> Don't write code with data races. > > >> > > >> Ian > > > > > > -- > > > 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...@googlegroups.com. > > > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/6a2c3ebf-b5e9-4a18-bcf3-d7a4b5fc24a4n%40googlegroups.com > . > > > > -- > > 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...@googlegroups.com. > > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/CAOyqgcWv7w%3DUEG0eZsZFYTpodX2cgXmXO_6QkgxeJ5A7VKDXAg%40mail.gmail.com > . > -- 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/8a611b07-7940-494b-88d5-27ec2fd57403n%40googlegroups.com.