Double-Checked Locking Anti-Pattern

Detects broken double-checked locking implementations that create race conditions in singleton initialization.

zakirkun afb4109 2 files · 2.0 KB Updated

File contents

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.Once for lazy initialization
  • C++: Use std::call_once or std::atomic

zakirkun/ice-tea/tree/main/skills/race-condition/double-check-locking commit afb410988a

Frequently asked questions

npx skillmds@latest add zakirkun/double-checked-locking-anti-pattern