I was asked the question during interview, and later found Meyers & Alexandrescu’s great article C++ and the Perils of Double-Checked Locking, one of the references in this greater Wikipedia page. DCLP is listed as one of anti pattern.

The essence of why DCLP doesn’t work is that compiler may rearrange instruction order, so another thread may reference a not-fully-initialized singleton. On a multiprocessor platform, cache coherence may also lead to problem where another thread on another processor gets a bad singleton.

Meyers offers several solutions:

  • Use multithread library, not simple synchronization construct like mutex.
  • Client code caches singleton instance locally
  • Use eager initialization for singleton, i.e. instantiate at startup
Advertisements