#include <iostream>
#include <string>
using namespace std;
/*
class mm {
private:
static mm *aa;
public:
static mm& instance() {
if (!aa)
aa = new mm;
return *aa;
}
void outoup() {
cout <<"aaaa" <<endl;
}
};
mm* mm::aa = 0;
*/
template <typename T>
class Singleton {
private:
static T *aa;
static pthread_mutex_t mutex;
public:
static T& instance() {
pthread_mutex_lock(&mutex);
if (!aa)
aa = new T;
pthread_mutex_unlock(&mutex);
return *aa;
}
static void release() {
pthread_mutex_lock(&mutex);
if (aa) {
delete aa;
aa = 0;
}
pthread_mutex_unlock(&mutex);
//pthread_mutex_destroy(&mutex);
}
};
template <typename T> T* Singleton<T>::aa = 0;
template <typename T> pthread_mutex_t Singleton<T>::mutex = PTHREAD_MUTEX_INITIALIZER;
class Test: public Singleton<Test> {
public:
void output() {cout << "tset" <<endl;}
};
int main(int argc, char *argv[]) {
Test::instance().output();
return 0;
}
一个单例
最新推荐文章于 2019-06-13 11:04:52 发布