Re: [go-nuts] Re: A question about A sync.Once implementation.

2019-05-08 Thread White Pure
Hi, You are right, I did some modification to your code and make it able to reproduce everytime: https://play.golang.org/p/y6vxC_DNjp9 Thanks! 在 2019年5月8日星期三 UTC+8下午7:15:26,rog写道: > > It seems clear to me that C ("can run but has not implemented the > singleton pattern, function f may r

Re: [go-nuts] Re: A question about A sync.Once implementation.

2019-05-08 Thread roger peppe
It seems clear to me that C ("can run but has not implemented the singleton pattern, function f may run multi times") is not the correct answer, because I'm pretty sure that f cannot run more than once. However, it's still not a correct Once implementation, because as has already been pointed out,

Re: [go-nuts] Re: A question about A sync.Once implementation.

2019-05-07 Thread Kurtis Rader
On Tue, May 7, 2019 at 9:42 PM White Pure wrote: > Hi, > Thanks for your reply. > The second bug is a known issue, so let’s ignore that. In that case, I > think function f still can only be executed once. > But why does the load and store of o.done need to be done using atomic > opera

[go-nuts] Re: A question about A sync.Once implementation.

2019-05-07 Thread White Pure
Hi, Thanks for your reply. The second bug is a known issue, so let’s ignore that. In that case, I think function f still can only be executed once. But why does the load and store of o.done need to be done using atomic operations? I suppose there’s mutex assuring the happens-before.

[go-nuts] Re: A question about A sync.Once implementation.

2019-05-07 Thread wudi . daniel
Hi, Thanks for your reply. The second bug is a known issue, so let’s ignore that. In that case, I think function f still can only be executed once. But why does the load and store of o.done need to be done using atomic operations? I suppose there’s mutex assuring the happens-before.

[go-nuts] Re: A question about A sync.Once implementation.

2019-05-07 Thread Robert Johnstone
Hello, The code contains two bugs. 1) The load and store of o.done needs to be done using atomic stores. 2) The statement to set o.done = 1 needs to be after the call to f. Otherwise, another goroutine can hit the check at the top of Done and return before the code in f has completed. On