Has anyone mentioned std::call_once
and std::once_flag
?Most other approaches - including double checked locking - are broken.
One major problem in singleton pattern implementation is safe initialization. The only safe way is to guard the initialization sequence with synchronizing barriers. But those barriers themselves need to be safely initiated. std::once_flag
is the mechanism to get guaranteed safe initialization.