模板类
#pragma once
template<typename T>
class EargerSingleton {
public:
static T& getInstance() {
return instance;
}
protected:
EargerSingleton() = default;
~EargerSingleton() = default;
EargerSingleton(const EargerSingleton&) = delete;
EargerSingleton& operator=(const EargerSingleton&) = delete;
private:
static T instance;
};
template <class T>
T EargerSingleton<T>::instance;
单例类
#pragma once
#include "EargerSingleton.hpp"
#include "iostream"
#include <thread>
using namespace std;
class Manager3 :public EargerSingleton<Manager3>
{
friend class EargerSingleton<Manager3>;
protected:
Manager3() {
mCount = 3;
}
~Manager3() = default;
public:
void Print() {
cout << this_thread::get_id() << "-count:" << mCount << endl;
}
private:
int mCount;
};
测试入口类?
#include "Manager3.hpp"
void fun() {
Manager3::getInstance().Print();
}
void main() {
cout.sync_with_stdio(true);
thread t(fun);
thread t2(fun);
thread t3(fun);
t.join();
t2.join();
t3.join();
system("pause");
}