C++ 单例模式实现

 #include <iostream>
#include <mutex> std::mutex mtx;
class Singleton {
private:
Singleton() {}
Singleton(const Singleton& a);
Singleton& operator=(const Singleton&);
static Singleton * instance;
public:
static Singleton* getInstance() {
if (instance == nullptr) {
mtx.lock();
if((instance == nullptr)){
instance = new Singleton();
}
mtx.unlock();
}
return instance;
}
};
Singleton* Singleton::instance = nullptr; int main(){
Singleton* instance1 = Singleton::getInstance();
Singleton* instance2 = Singleton::getInstance();
if(instance1 == instance2){
std::cout << "singleton" << std::endl;
}
return ;
}
上一篇:vue解决IOS10低版本白屏问题


下一篇:解决vue低版本安卓手机兼容性问题