C++单例实现

单例是经常用到的一种设计模式,看似简单实则要考虑很多方面,如线程安全、懒加载性能等,本文提供C++单例的两种实现,具有较高的适用性。

局部静态变量实现

class Singleton
{
public:
    static Singleton& instance();

protected: //访问修饰符声明为protected是为了继承;如不被继承声明为private
    Singleton() = default;
    ~Singleton() = default;

private:
    /*
    拷贝和赋值构造函数声明为delete时,访问修饰符声明为public、protected、private均可
    Singleton(const Singleton&) = delete;
    Singletone& operator=(const Singleton&) = delete;
    */
    Singleton(const Singleton&);
    Singleton& operator=(const Singleton&);
};

Singleton& Singleton::instance()
{
    static Singleton _s_instance;
    return _s_instance;
}

说明:

  1. 线程安全问题,C++11后静态变量初始化保证线程安全。但是要注意以下问题,C++11依赖TLS(线程本地存储)实现静态局部变量的线程安全初始化,Windows Vista及以后版本系统支持, Windows XP, Windows Server 2003不支持,因此XP等系统不能使用此单例模式的实现。详情参见

使用call_once实现

#include <memory>
#include <mutex>

class Singleton
{
public:
    static std::shared_ptr<Singleton> instance();

    //智能指针的析构函数释放资源时,需要能访问到Singleton的析构函数,因此次实现声明为public
    //其他单例实现方式时,析构函数声明为public或者private均可
    ~Singleton() = default; 

private:
    Singleton() = default;

    Singleton(const Singleton&);
    Singleton& operator=(const Singleton&);

private:
    static std::shared_ptr<Singleton> _s_instance;
    static std::once_flag _s_once_flag;
};

std::shared_ptr<Singleton> Singleton::_s_instance = nullptr;
std::once_flag Singleton::_s_once_flag;

std::shared_ptr<Singleton> Singleton::instance()
{
    std::call_once(_s_once_flag, [&]()
    {
        _s_instance.reset(new Singleton());
    });

    return _s_instance;
}

说明:

  1. 该单例利用std::call_once和std::once_flag实现,std::call_once的调用是线程安全的,且为懒加载方式,详情参见
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值