o.done is only written while o.m is locked. So there can't be any concurrent writes to that particular read. The store has to use atomic, as there *are* concurrent reads not protected by the mutex.
On Sat, Jun 18, 2022 at 10:26 AM Xiaofan Hu <bom.d....@gmail.com> wrote: > Hi, I have a question about sync.Once. > > In *Once.doSlow > <https://github.com/golang/go/blob/release-branch.go1.18/src/sync/once.go#L66>, > it isn't using atomic.LoadUint32 after acquiring the lock, isn't there a > risk of race condition/data race here? > > ``` > func (o *Once) doSlow(f func()) { > o.m.Lock() > defer o.m.Unlock() > if *o.done == 0* { // <-- potential race condition? > defer atomic.StoreUint32(&o.done, 1) > f() > } > } > ``` > > -- > 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/4324f1da-3958-4d24-b11d-c25f4a406e12n%40googlegroups.com > <https://groups.google.com/d/msgid/golang-nuts/4324f1da-3958-4d24-b11d-c25f4a406e12n%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/CAEkBMfHJ5i05AFGMhkiBCDPPjKhFNYtOOFoJgO3hk6-rgmxvGQ%40mail.gmail.com.