1 Star 0 Fork 0

Golang语言社区/go-concurrentMap

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
once.go 1.37 KB
一键复制 编辑 原始数据 按行查看 历史
fanliao 提交于 2014-11-06 01:29 +08:00 . re-design hash function
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package concurrent
import (
"sync"
"sync/atomic"
)
// Once is an object that will perform exactly one action.
type Once struct {
m sync.Mutex
done uint32
}
// Do calls the function f if and only if Do is being called for the
// first time for this instance of Once. In other words, given
// var once Once
// if once.Do(f) is called multiple times, only the first call will invoke f,
// even if f has a different value in each invocation. A new instance of
// Once is required for each function to execute.
//
// Do is intended for initialization that must be run exactly once. Since f
// is niladic, it may be necessary to use a function literal to capture the
// arguments to a function to be invoked by Do:
// config.once.Do(func() { config.init(filename) })
//
// Because no call to Do returns until the one call to f returns, if f causes
// Do to be called, it will deadlock.
//
func (o *Once) Do(f func()) {
if atomic.LoadUint32(&o.done) == 1 {
return
}
// Slow-path.
o.m.Lock()
defer o.m.Unlock()
if o.done == 0 {
f()
atomic.StoreUint32(&o.done, 1)
}
}
func (o *Once) IsDone() bool {
if atomic.LoadUint32(&o.done) == 1 {
return true
}
return false
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wwwgolangltd/go-concurrentMap.git
git@gitee.com:wwwgolangltd/go-concurrentMap.git
wwwgolangltd
go-concurrentMap
go-concurrentMap
7d2d7a5ea67b

搜索帮助