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

转载于:https://www.cnblogs.com/bysir/p/5891017.html

你可能感兴趣的:(golang锁使用(map读写))