Skip to main content
Journey Uncommon Logo
JourneyUncommon
hardGoGo memory model and happens-beforeSingle-choice MCQ

Why is the double-checked locking pattern below still racy under the Go memory model, even though once is guarded by the mutex on the slow path?

var mu sync.Mutex var inited bool var instance *T func get() *T { if inited { // unsynchronized read return instance } mu.Lock() defer mu.Unlock() if !inited { instance = &T{} inited = true } return instance }