This question was closed as duplicate and hence making the question more explicit and reopening the question:
The questions are :
1) Is the implementation below thread safe or not?
2) What are the problems with the below approach (except for complexity)
class Singleton
{
public:
static void init (){
static Singleton _single ;
cout<<"Called"<<endl;
m_Singleton = &_single ;
}
static Singleton & instance ()
{
static pthread_once_t once_flag = PTHREAD_ONCE_INIT;
cout<<(unsigned int)PTHREAD_ONCE_INIT<<endl;
pthread_once(&once_flag, &Singleton::init) ;
return *m_Singleton;
}
static Singleton* m_Singleton;
private:
Singleton (){}
Singleton (const Singleton&) ;
Singleton operator=(Singleton&) ;
};
I'm counting on that something's wrong with this implementation hence not suggested anywhere
Thanks