Created
August 18, 2019 09:34
-
-
Save Tomotoes/36b5932022da4a3daa1969739a9028d3 to your computer and use it in GitHub Desktop.
Officially provided Context package
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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