[+golang-nuts again, please don't respond off-list. This is a public forum, everyone should benefit]
On Thu, Nov 18, 2021 at 3:47 AM Ting Yuan <yuanting1...@gmail.com> wrote: > Thanks a lot, your code is very useful and explains my confusion. I think > the underlying reason is because fmt.Println grabs a global mutex so it > actually construct two critical section before the writing of a (i.e. a = > 3). These critical section introduce a happens before relation so the race > detector failed to report them. If the order of the two critical sections > is reversed, the relation is broken and the race detector can alarm it. > You can verify that hypothesis by running this program <https://play.golang.org/p/qDUD6F_KQGn> under the race detector. It does the same thing and exhibits the same "occasionally racey" behavior as the original and lets you independently tinker with these ideas. And yeah, after playing around for a bit, I think you are correct. > > 在2021年11月17日星期三 UTC+8 下午3:19:05<axel.wa...@googlemail.com> 写道: > >> On Wed, Nov 17, 2021 at 6:43 AM Ting Yuan <yuanti...@gmail.com> wrote: >> >>> I am confused, why there is no data race in the above program. I'm using >>> go1.17.2 linux/amd64 >>> >> >> The race detector is a heuristic. That it doesn't report a race does not >> mean there is none. Races are non-deterministic, so it's very possible that >> any particular run just didn't trigger one. >> >> This slightly modified version of the same race triggers the >> race-detector reliably for me: >> >> func f() { >> wg := new(sync.WaitGroup) >> defer wg.Wait() >> >> a := 1 >> wg.Add(1) >> go func() { >> defer wg.Done() >> fmt.Print(a) >> a = 3 >> }() >> fmt.Print(a) >> } >> >> func main() { >> for i := 0; i < 1000; i++ { >> f() >> } >> } >> >> I just added a bit of synchronization to replace the time.Sleep and >> called the code 1000 times, to increase the likelihood of triggering the >> race. >> >> >>> -- >>> 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/3833aa84-9614-4815-ba1b-700b2de51df8n%40googlegroups.com >>> <https://groups.google.com/d/msgid/golang-nuts/3833aa84-9614-4815-ba1b-700b2de51df8n%40googlegroups.com?utm_medium=email&utm_source=footer> >>> . >> >> -- 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/CAEkBMfEDubQNXFE8R1Yju9sofG0KG8gVSdtYh2YcKoOsPAmBGA%40mail.gmail.com.