golang锁使用(map读写)

var source *memorySource
var present bool

p.lock.RLock()
if source, present = p.sources[name]; !present {
	// The source wasn't found, so we'll create it.
	p.lock.RUnlock()
	p.lock.Lock()
	if source, present = p.sources[name]; !present {
		source = &memorySource{
			name: name,
			metrics: map[string]*memoryMetric{},
		}

		// Insert the newly created *memorySource.
		p.sources[name] = source
	}
	p.lock.Unlock()
} else {
	p.lock.RUnlock()
}

怕失效就自己再收藏一次

转载自 : http://studygolang.com/articles/2775

posted @ 2016-09-20 23:27  bysir  阅读(2018)  评论(0编辑  收藏  举报