祝福大家.
我正在尝试编写一个线程安全的懒惰单例以备将来使用.这是我能想到的最好的.有人能发现任何问题吗?关键假设是静态初始化在动态初始化之前发生在单个线程中.(这将用于商业项目,公司不使用提升:(,生活将是轻而易举的:)
PS:没有检查这个编译,我的道歉.
/*
There are two difficulties when implementing the singleton pattern:
Problem (a): The "global variable instantiation fiasco". TODO: URL
This is due to the unspecified order in which global variables are initialised. Static class members are equivalent
to a global variable in C++ during initialisation.
Problem (b): Multi-threading.
Care must be taken to ensure that the mutex initialisation is handled properly with respect to problem (a).
*/
/*
Things achieved, maybe:
*) Portable
*) Lazy creation.
*) …Run Code Online (Sandbox Code Playgroud)