3 ms·
> A global variable is more useful than singleton, since it comes with no battery, plan or pretense. Initialization and deinitialization of globals in C++ is
by quotemstr 14y ago
> A global variable is more useful than singleton, since it comes with no battery, plan or pretense.
Initialization and deinitialization of globals in C++ is a trainwreck. A singleton wrapper like the one I proposed upthread gives you deterministic initialization order and doesn't impose a runtime performance penalty.
> Singleton at minimal requires thread-safety
Both Windows and POSIX have locks that can be statically initialized. In Windows,
SRWLOCK g_thing_lock = SRWLOCK_INIT;
With pthreads,
pthread_mutex_t g_thing_lock = PTHREAD_MUTEX_INITIALIZER;
Now you can serialize access to your singleton without having to do any explicit and delicate lock initialization. Of course, this isn't the only approach --- you can make the singleton itself threadsafe and have your generic singleton wrapper serialize initialization using a similar approach. TIMTOWTDI.