Crypto++ 8.7
Free C++ class library of cryptographic schemes
|
Restricts the instantiation of a class to one static object without locks. More...
#include <misc.h>
Public Member Functions | |
Singleton (F objectFactory=F()) | |
const T & | Ref (...) const |
Return a reference to the inner Singleton object. More... | |
Restricts the instantiation of a class to one static object without locks.
T | the class or type |
F | the object factory for T |
instance | an instance counter for the class object |
This class safely initializes a static object in a multi-threaded environment. For C++03 and below it will do so without using locks for portability. If two threads call Ref() at the same time, they may get back different references, and one object may end up being memory leaked. This is by design and it avoids a subtle initialization problem in a multi-threaded environment with thread local storage on early Windows platforms, like Windows XP and Windows 2003.
For C++11 and above, a standard double-checked locking pattern with thread fences are used. The locks and fences are standard and do not hinder portability.
Microsoft's C++11 implementation provides the necessary primitive support on Windows Vista and above when using Visual Studio 2015 (cl.exe
version 19.00). If C++11 is desired, you should set WINVER
or _WIN32_WINNT
to 0x600 (or above), and compile with Visual Studio 2015.
|
inline |
const T & Singleton< T, F, instance >::Ref | ( | ... | ) | const |
Return a reference to the inner Singleton object.
T | the class or type |
F | the object factory for T |
instance | an instance counter for the class object |
Ref() is used to create the object using the object factory. The object is only created once with the limitations discussed in the class documentation.