Double-Checked Locking Race Condition
Overview
Double-checked locking is a common but often incorrectly implemented pattern for lazy singleton initialization. Without proper memory ordering or volatile declarations, the second check can observe a partially-initialized object due to instruction reordering.
In Java pre-5.0 (without volatile), this is broken. In Go, use sync.Once instead.
Remediation
- Java: Declare singleton field
volatile - Go: Use
sync.Oncefor lazy initialization - C++: Use
std::call_onceorstd::atomic