17.C++设计模式-单例

#include 
using namespace std;
#include 
/*
    懒汉式
*/
class SingleTon {
private:
    SingleTon() {
        
    }
private:
    static SingleTon* ton;
    static int count;
    static mutex mu;
public:
    static SingleTon* getInstance() {
        if (ton == NULL) {
            mu.lock();
            if (ton == NULL)
            {
                ton = new SingleTon;
            }
            mu.unlock();
        }
        return ton;
    }

    void add() {
        count++;
        cout<add();
    SingleTon* s1 = SingleTon::getInstance();
    s1->add();


    SingleTon2* s3 = SingleTon2::getInstance();
    s3->add();
    SingleTon2* s4 = SingleTon2::getInstance();
    s4->add();

    system("pause");
}

你可能感兴趣的:(17.C++设计模式-单例)