~
[scx.git] / include / Singleton.hpp
blobc8e367c6fde7ea0ee09613ac1d5dfd5cc05b6fed
1 #ifndef SCX_SINGLETON_HPP
2 #define SCX_SINGLETON_HPP
4 #include <pthread.h>
6 namespace scx {
8 template<typename T>
9 class Singleton
11 public:
12 static T& Instance()
14 pthread_once(&control, &Singleton::Init);
15 return *ptrInstance;
18 private:
19 static void Init()
21 ptrInstance = new T;
24 private:
25 static pthread_once_t control;
26 static T* ptrInstance;
29 template<typename T>
30 pthread_once_t Singleton<T>::control = PTHREAD_ONCE_INIT;
32 template<typename T>
33 T* Singleton<T>::ptrInstance = NULL;
36 #endif