#include<pthread.h>
#include<stdio.h>
{
protected:
nocopyable(){}
~nocopyable(){}
private:
nocopyable(const nocopyable &);//用来防止友元和外部赋值调用
const nocopyable & operator=(const nocopyable &);
};
class man {
public:
man()
{
std::cout<<"man construct"<<std::endl;
}
};
class Singleton:nocopyable
{
public:
static man * instance()
{
pthread_once(&m_ponce,&Singleton::init);
return m_p;
}
static void init()
{
std::cout<<"init operator"<<std::endl;
m_p = new man();
}
public:
static pthread_once_t m_ponce;
static man *m_p;
};
pthread_once_t Singleton::m_ponce = PTHREAD_ONCE_INIT;
man* Singleton::m_p= NULL;
int main()
{
man* m = Singleton::instance();
return 0;
}
#include<stdio.h>
#include<iostream>
//不可复制基类
class nocopyable{
protected:
nocopyable(){}
~nocopyable(){}
private:
nocopyable(const nocopyable &);//用来防止友元和外部赋值调用
const nocopyable & operator=(const nocopyable &);
};
class man {
public:
man()
{
std::cout<<"man construct"<<std::endl;
}
};
class Singleton:nocopyable
{
public:
static man * instance()
{
pthread_once(&m_ponce,&Singleton::init);
return m_p;
}
static void init()
{
std::cout<<"init operator"<<std::endl;
m_p = new man();
}
public:
static pthread_once_t m_ponce;
static man *m_p;
};
pthread_once_t Singleton::m_ponce = PTHREAD_ONCE_INIT;
man* Singleton::m_p= NULL;
int main()
{
man* m = Singleton::instance();
return 0;
}