c++11设计模式 单例模式 线程安全 泛型单例

本文主要总结几种单例实现的方式,实现单例时需要主要考虑线程安全。再者可以考虑泛型单例模式的实现,可以传入不同个数的参数和不同类型的参数来构造单例。

双重校验加锁

为了线程安全都会考虑到上锁。同时为了提高效率一般会使用双重校验加锁的方式,如下示例代码:

#include <mutex>
#include <memory>

template<typename T>
class Singleton {
 public:
    static T* GetInstance() {
        static std::mutex m_mutex;
        if (nullptr == m_instance_) {
            std::lock_guard<std::mutex> lock(m_mutex);
            if (nullptr == m_instance_) {
                m_instance_=std::make_shared<T>();
            }
        }
        return m_instance_.get();
    }

 private:
    static std::shared_ptr<T> m_instance_;
};
template<typename T>
std::shared_ptr<T> Singleton<T>::m_instance_ = nullptr;

第一次判断非空表示如果单例被创建了,就不需要同步了,可以直接获取单例。否则需要同步。第二次判断表示在同步的线程中有一个线程创建了单例,其他的线程也可以直接获取单例返回。

call_once

为了保证在多线程环境只构造一次对象,这时就可以使用std::call_once来保证。如下示例:

#include <mutex>
#include <thread>
#include <memory>
template<typename T>
class Singleton {
 public:
    static T* GetInstance() {
        if (nullptr == m_instance_) {
            std::call_once(m_flag_, []{m_instance_=std::make_shared<T>();});
        }
        return m_instance_.get();
    }


 private:
    static std::once_flag m_flag_;
    static std::shared_ptr<T> m_instance_;
};

template<typename T>
std::shared_ptr<T> Singleton<T>::m_instance_ = nullptr;

template<typename T>
std::once_flag Singleton<T>::m_flag_;

这里使用了std::call_once,别忘了#include<thread>,同时编译时添加-pthread参数。

静态局部变量

C++11 保证静态局部变量的初始化过程是线程安全的,即某个线程如果在初始化T,其他线程执行到T的初始化这一行的时候就会被挂起。

template<typename T>
class Singleton {
 public:
    static T* GetInstance() {
        static std::shared_ptr<T> m_instance = std::make_shared<T>();
        return m_instance.get();
    }
};

关于静态局部变量的初始化可以看看这个

泛型单例模式

下面考虑实现一个泛型的单例模式。

#include <memory>
#include <iostream>
#include <string>

template<typename T>
class Singleton {
 public:
    template<typename...Args>
    static T* GetInstance(Args&&...args) {
        static std::shared_ptr<T> m_instance = std::make_shared<T>(std::forward<Args>(args)...);
        return m_instance.get();
    }
};

class Test1 {
 public:
    Test1(const int i) {std::cout<< "Test1" <<std::endl;}
};

class Test2 {
 public:
    Test2(const std::string& str) { std::cout<< "Test2" <<std::endl;}
};

class Test3 {
 public:
    Test3(const float a, const float b) { std::cout<< "Test3" <<std::endl;}
};

int main() {
    auto test1 = Singleton<Test1>::GetInstance(1);
    auto test2 = Singleton<Test2>::GetInstance("test");
    auto test3 = Singleton<Test3>::GetInstance(0.1, 0.2);
    return 0;
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值