On Wed, Nov 17, 2021 at 6:43 AM Ting Yuan <yuanting1...@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+unsubscr...@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/CAEkBMfFqZN9pKU1XWhYB4hfC8jTggzCAhVn-ABPuAns-T4%2BDFg%40mail.gmail.com.

Reply via email to