I'm quite certain that code is correct. There's no data race here
since the write and the non-atomic read are both inside the mutex.

The memory model rules about mutexes formalize how they serialize the
code inside them. Because the unlock happens-before the next lock, and
because sequential code in a single goroutine has the happens-before
order that the source code expresses, this read:

if o.done == 0

and this write:

atomic.StoreUint32(&o.done, 1)

are not concurrent. (This is just a slightly more precise way of
stating the natural, intuitive behavior of a mutex.) Therefore, that
read *must* observe the previously written value.

On Thu, Dec 28, 2017 at 7:11 PM, Dave Cheney <d...@cheney.net> wrote:
> I think you are right, all access to that field should go via atomic 
> load/store operations. I don’t think it’s safe to mix those operations wth 
> normal loads and stores even within the same goroutine.
>
> --
> 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.
> For more options, visit https://groups.google.com/d/optout.

-- 
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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to