//为什么这样写单例模式还是能构造两个对象
#include<stdlib.h>
#include<pthread.h>
#include<iostream>
using namespace std;
template<typename T>
class Singleton
{
public:
static T& get_instance()
{
pthread_once(&ponce_,&Singleton::init);
// assert(value!=NULL);
return *value;
}
private:
Singleton();
~Singleton();
static void init()
{
cout<<"调用了初始化函数"<<endl;
value = new T(10);
}
private:
static pthread_once_t ponce_;
static T* value;
};
template<typename T>
pthread_once_t Singleton<T>::ponce_ = PTHREAD_ONCE_INIT;
template<typename T>
T* Singleton<T>::value = NULL;
int main()
{
int st = Singleton<int>::get_instance();
cout<<st<<endl;
int st1 = Singleton<int>::get_instance();
cout<<st1<<endl;
return 0;
}