Hi,

I still don't understand this issue completely.

Sure, the call to *wg.Done()* will "happen before" *wg.Wait()* finishes, 
but the assignment *"result = &foo{1}"* might still happen concurrently.

I think the problem is the semantics of the *defer* and the discussion is 
reduced completely to the question: is the assignment *"result = &foo{1}"* 
going 
to *happen before" the deferred called to *"wg.Done()"*?

I don't see any occurrence of 'defer' in https://golang.org/ref/mem nor a 
proper description in https://golang.org/ref/spec#Defer_statements, so I 
feel inclined to conclude this behavior is undefined by the spec.

Daniel.


On Tuesday, August 23, 2016 at 3:23:27 PM UTC+2, Ian Lance Taylor wrote:
>
> On Tue, Aug 23, 2016 at 3:14 AM, Yulrizka <yulr...@gmail.com <javascript:>> 
> 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.

Reply via email to