C++ 单例(singleton)模式实现

1、饿汉式单例类

#ifndef EAGER_SINGLETON_H
#define EAGER_SINGLETON_H

#include <iostream>
#include <pthread.h>
class EagerSingleton
{
    public:
        ~EagerSingleton() {
            delete instance_;
        }
        static EagerSingleton* getSingletonInstance() {
            return instance_;
        }
        unsigned add() {
            unsigned tmp(0);
            pthread_mutex_lock(&mutex_);
            count++;
            tmp = count;
            pthread_mutex_unlock(&mutex_);
            return tmp;
        }
    private:
        EagerSingleton(){
            std::cout << "call constructor" << std::endl;   
            count = 0;
        }
        unsigned count;
        static pthread_mutex_t mutex_;
        static EagerSingleton* instance_;

};
EagerSingleton* EagerSingleton::instance_ = new EagerSingleton;//类加载时初始化,确保唯一性
pthread_mutex_t EagerSingleton::mutex_ = PTHREAD_MUTEX_INITIALIZER;

#endif
#include "EagerSingleton.h"
#include <pthread.h>
#include <unistd.h>
#include <iostream>
void* run(void *arg)
{
    int *id = static_cast<int*>(arg);
    EagerSingleton* p = EagerSingleton::getSingletonInstance();

    std::cout << *id << "\t" << pthread_self() << "\t" << p << "\t" << p->add() << std::endl;
}
void test()
{
    for (int i = 0; i < 10; ++i) {
        pthread_t tid;
        pthread_create(&tid, NULL, run, static_cast<void*>(&i));

    }

}
int main()
{
    test();
    return 0;
}

测试结果:
这里写图片描述

2、懒汉式单例类

#ifndef LAZY_SINGLETON_H
#define LAZY_SINGLETON_H

#include <iostream>
#include <pthread.h>

//懒汉式,延迟加载(Lazy Load)技术
class LazySingleton
{
    public:
        ~LazySingleton() {
            if (instance_)
                delete instance_;
        }   
        //双重检查锁定(Double-Check Locking)
        static LazySingleton* getSingletonInstance() {
            if (instance_ == NULL) {
                pthread_mutex_lock(&mutex_);
                if (instance_ == NULL) {
                    instance_ = new LazySingleton;
                }
                pthread_mutex_unlock(&mutex_);
            }
            return instance_;
        }
        unsigned add() {
            unsigned tmp(0);
            pthread_mutex_lock(&mutex_);
            count++;
            tmp = count;    
            pthread_mutex_unlock(&mutex_);
            return tmp;
        }
    private:
        LazySingleton() {
            std::cout << "call constructor" << std::endl;
            count = 0;  
        }
        unsigned count;
        static pthread_mutex_t mutex_;
        static LazySingleton* instance_;
};

LazySingleton* LazySingleton::instance_ = NULL;
pthread_mutex_t LazySingleton::mutex_ = PTHREAD_MUTEX_INITIALIZER;
#endif
#include "LazySingleton.h"
#include <pthread.h>
#include <unistd.h>
#include <iostream>

void* run(void *arg)
{

    LazySingleton *p = NULL;
    do{ 
        p = LazySingleton::getSingletonInstance();
        usleep(100000);
    } while(p == NULL);

    std::cout << pthread_self() << "\t" << p << "\t" << p->add() << std::endl;
}
void test()
{
    for (int i = 0; i < 10; ++i) {
        pthread_t tid;
        pthread_create(&tid, NULL, run, NULL);

    }

}
int main()
{
    test();
    sleep(1000);
    return 0;
}

这里写图片描述

参考:
《设计模式的艺术——软件开发人员内功修炼之道》 刘伟 著

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值