On Tue, Aug 23, 2016 at 3:14 AM, Yulrizka <yulri...@gmail.com> wrote: > > But the case here is indeed as Marvin explained, There is race condition. > But in my example, I make sure that wg done is to protect result pointer > before calling it to caller. > > with this structure some one could easily introduce a race condition if he > tried to access the result pointer. > > What I would like to understand if there is case that my snippet causes race > condition. > > This is my understanding. > 1. Heap memory is shared between go rountine in the same process > 2. main routine create a pointer to some struct on the heap (initially nil) > 3. the go routine allocate memory on the heap for the `foo` struct > 4. the go routine assign main's result pointer > 5. wg.Done() ensure that step 4 is done before it released > 6. process will always returns value generated by the go routine. > > And I would like to make sure that my understanding of heap is correct. That > is shared per process and there are no heap copying between go routine.
That is correct. There is no race in the original program you sent because the wg.Done acts as a store-release and the wg.Wait acts as a load-acquire. 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.