Thanks! Note To Self (or anyone whom may find it useful): Since sync.Once is usually used for doing initialization, it matters that the consecutive calls wait until the first call to f() gets completed. With provided implementation in the first post, there is no such guarantee.
On Friday, February 16, 2018 at 5:07:53 PM UTC+3:30, [email protected] wrote: > > > > On Friday, February 16, 2018 at 3:27:04 PM UTC+2, dc0d wrote: >> >> Why sync.Once uses a sync.Mutex in addition to atomic functions? >> >> What are the drawbacks/shortcomings/deficiencies of this implementation? >> >> type Once struct { >> done uint32 >> } >> >> >> func (o *Once) Do(f func()) { >> if !atomic.CompareAndSwapUint32(&o.done, 0, 1) { >> return >> } >> f() >> } >> >> > > You implementation returns immediately, if f() is still running, while in > original implementation all concurrent calls to Do() returns when f() > return. > > Djadala > > > -- 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 [email protected]. For more options, visit https://groups.google.com/d/optout.
