-rw-r--r-- | noncore/multimedia/opieplayer2/singleton.h | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/noncore/multimedia/opieplayer2/singleton.h b/noncore/multimedia/opieplayer2/singleton.h index 83e228f..825344e 100644 --- a/noncore/multimedia/opieplayer2/singleton.h +++ b/noncore/multimedia/opieplayer2/singleton.h @@ -17,12 +17,14 @@ Boston, MA 02111-1307, USA. */ #ifndef SINGLETON_H #define SINGLETON_H +#include "threadutil.h" + template <class Product> struct DefaultSingletonCreator { static Product *create() { return new Product; } }; @@ -39,14 +41,17 @@ template > class Singleton { public: static T &self() { - if ( !s_self ) - s_self = Creator<T>::create(); + if ( !s_self ) { + ThreadUtil::AutoLock lock( s_guard ); + if ( !s_self ) + s_self = Creator<T>::create(); + } return *s_self; } protected: Singleton() { s_self = static_cast<T *>( this ); } @@ -55,14 +60,18 @@ protected: private: Singleton( const Singleton<T, Creator> &rhs ); Singleton<T, Creator> &operator=( const Singleton<T, Creator> &rhs ); static T *s_self; + static ThreadUtil::Mutex s_guard; }; template <class T, template <class> class Creator> T *Singleton<T, Creator>::s_self = 0; +template <class T, template <class> class Creator> +ThreadUtil::Mutex Singleton<T, Creator>::s_guard; + #endif // SINGLETON_H /* vim: et sw=4 ts=4 */ |