设计模式之单体模式

一,应用场景

     全局范围内只生成一个对象,比如常见项目中的配置管理部分,通常就是一处初始化,全局使用。

     单线程可以用单线程的模式,多线程就需要考虑到线程安全问题,可以借助pthread_once_t,pthread_mutex_t来实现线程安全

二,源码

     包括单线程版本与线程安全版本,附简单的测试代码。

    

/*************************************************************************
    > File Name: singlton.cpp
    > Author:zhangtx
    > Mail: 18510665908@163.com 
    > Created Time: 2015年06月06日 星期六 09时48分45秒
 ************************************************************************/
#include<iostream>
#include<pthread.h>
using namespace std;
class TestC
{
    public:
        TestC()
        {
            cout<<"TestC is constructed"<<endl;
        }
        void run()
        {
            cout<<pthread_self()<<endl;
        }
};


#ifdef SINGLETHREAD
template <class T>
class Instance
{
    private:
        static T *m_value;
    private:
        Instance(){

        };
        ~Instance(){

        };
        Instance(const Instance &instance)
        {
        }
        Instance &operator=(const Instance &instance)
        {
        }
    public:
        static T * getInstance();
};

template<class T>
T * Instance<T>::m_value=NULL;

template<class T>
T *Instance<T>::getInstance()
{
    if (m_value==NULL)
        m_value=new T();
    return m_value;
}


int main(int argc,char *argv[])
{
    Instance<TestC>::getInstance();
    Instance<TestC>::getInstance();
    Instance<TestC>::getInstance();
}
#else


template<class T>
class Instance
{
    private:
        static T *m_value;
        static pthread_once_t m_once;
    private:
        Instance()
        {
        };
        ~Instance()
        {
        };
        Instance(const Instance &instance)
        {
        };
        Instance &operator=(const Instance &instance)
        {

        }
        static void init()
        {
            m_value=new T();
        }
    public:
        static T *getInstance();
};
template<class T>
T * Instance<T>::m_value=NULL;
template<class T>
pthread_once_t Instance<T>::m_once=PTHREAD_ONCE_INIT;

template<class T>
T *Instance<T>::getInstance()
{
    pthread_once(&m_once,&init);
    return m_value;
}

void *ThreadFunc(void *)
{
    while(true)
    {
        Instance<TestC>::getInstance()->run();
        sleep(10);
    }
}
int main(int argc,char *argv[])
{
    int threadCount=5;

    pthread_t tid[5];
    for(int idx=0;idx<threadCount;idx++)
    {
        pthread_create(&tid[idx],NULL,&ThreadFunc,NULL);
    }

    for(int idx=0;idx<threadCount;idx++)
    {
        pthread_join(tid[idx],NULL);
    }
    return 0;
}

#endif

三,运行结果

    多线程场景

[root@M-192-168-10-225 algo]# g++ singleSing.cpp  -lpthread
[root@M-192-168-10-225 algo]# ./a.out
TestC is constructed
139764064061184
139764053571328
139764043081472
139763940062976
139764032591616
139764053571328
139764064061184
139764032591616
139763940062976
139764043081472

单线程场景

[root@M-192-168-10-225 algo]# g++ singleSing.cpp 
[root@M-192-168-10-225 algo]# ./a.out
TestC is constructed



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值