Skip to content

Instantly share code, notes, and snippets.

@Tomotoes
Created August 18, 2019 09:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tomotoes/36b5932022da4a3daa1969739a9028d3 to your computer and use it in GitHub Desktop.
Save Tomotoes/36b5932022da4a3daa1969739a9028d3 to your computer and use it in GitHub Desktop.
Officially provided Context package
package sync
import (
"sync"
"sync/atomic"
)
type Once struct {
m sync.Mutex
done uint32
}
func (o *Once) Do(f func()) {
if atomic.LoadUint32(&o.done) == 1 {
return
}
o.m.Lock()
defer o.m.Unlock()
if o.done == 0 {
defer atomic.StoreUint32(&o.done, 1)
f()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment