zoukankan      html  css  js  c++  java
  • 单键模式

    #include <mutex>
    #include <iostream>
    using namespace std;
    
    class Single{
    public:
        static Single*  ShareInstance();
        static void ReleaseInstance();
    
    private:
        Single();
        ~Single();
        Single(const Single &other);
        Single& operator=(const Single &other);
        
    private:
        static Single *m_pObj;
        static mutex *m_pMutex;
    };
    
    
    
    #include "Single.h"
    Single* Single::m_pObj = NULL;
    mutex* Single::m_pMutex = new mutex();
    Single* Single::ShareInstance(){
        if (NULL == m_pObj) {
            m_pMutex->lock();
            m_pObj = new Single();
            m_pMutex->unlock();
        }
        return m_pObj;
    }
    
    void Single::ReleaseInstance(){
        if (m_pObj) {
            delete m_pObj;
            m_pObj = NULL;
        }
    }
    
    Single::Single(){
        
    }
    Single::~Single(){
        
    }

    EX:

    #include <iostream>
    class SingleEx{
    public:
        static SingleEx& ShareInstance();
    private:
        SingleEx();
        ~SingleEx();
        SingleEx(const SingleEx &other);
        SingleEx& operator =(const SingleEx &other);
     
    };
    
    #include "SingleEx.h"
    
    
    SingleEx& SingleEx::ShareInstance(){
        static SingleEx obj;
        return obj;
    }
    
    SingleEx::SingleEx(){
        
    }
    
    SingleEx::~SingleEx(){
        
    }
  • 相关阅读:
    Git远程操作详解
    Markdown介绍
    Git & GitHub 学习
    小甲鱼Python笔记(类)
    小甲鱼Python笔记(下)
    小甲鱼Python笔记(上)
    compare.go
    compact_op.go
    cluster.go
    client.go
  • 原文地址:https://www.cnblogs.com/zhidao-chen/p/3836198.html
Copyright © 2011-2022 走看看