c++如何实现单例模式_C++单例类设计与线程安全实现

单例模式确保类唯一实例并提供全局访问点,c++中推荐使用局部静态变量实现线程安全的懒加载,简洁高效;对于需控制构造时机或旧标准环境,可采用双检锁结合智能指针管理内存,避免泄漏。

c++如何实现单例模式_C++单例类设计与线程安全实现

单例模式确保一个类只有一个实例,并提供全局访问点。在C++中实现单例模式,既要考虑设计简洁性,也要处理多线程环境下的安全问题。

基础单例类设计

最简单的单例实现是使用静态成员函数和静态实例:

 class Singleton { public:     static Singleton& getInstance() {         static Singleton instance;         return instance;     } <pre class='brush:php;toolbar:false;'>Singleton(const Singleton&) = delete; Singleton& operator=(const Singleton&) = delete;

private: Singleton() = default; ~Singleton() = default; };

说明: C++11起,局部静态变量的初始化是线程安全的。这意味着多个线程同时调用getInstance()不会导致重复创建实例,编译器会自动加锁保证构造只执行一次。

懒加载与双检锁(适用于老标准)

在C++11之前或需要显式控制构造时机时,可采用双检锁模式:

立即学习C++免费学习笔记(深入)”;

 #include <mutex> <p>class LazySingleton { public: static LazySingleton* getInstance() { if (instance == nullptr) { std::lock<em>guard<std::mutex> lock(mutex</em>); if (instance == nullptr) { instance = new LazySingleton(); } } return instance; }</p><pre class='brush:php;toolbar:false;'>static void destroyInstance() {     std::lock_guard<std::mutex> lock(mutex_);     delete instance;     instance = nullptr; }  LazySingleton(const LazySingleton&) = delete; LazySingleton& operator=(const LazySingleton&) = delete;

private: LazySingleton() = default; ~LazySingleton() = default;

static LazySingleton* instance; static std::mutex mutex_;

};

c++如何实现单例模式_C++单例类设计与线程安全实现

设计师AI工具箱

最懂设计师的效率提升平台,实现高效设计出图和智能改图,室内设计,毛坯渲染,旧房改造 ,软装设计

c++如何实现单例模式_C++单例类设计与线程安全实现 124

查看详情 c++如何实现单例模式_C++单例类设计与线程安全实现

// 静态成员定义 LazySingleton* LazySingleton::instance = nullptr; std::mutex LazySingleton::mutex_;

关键点:

  • 第一次检查避免每次都加锁,提升性能
  • 第二次检查防止多个线程同时通过第一层判断后重复创建
  • 需手动管理内存释放(可通过智能指针优化)

使用智能指针优化资源管理

结合std::unique_ptr可自动释放资源:

 #include <mutex> #include <memory> <p>class SmartSingleton { public: static SmartSingleton& getInstance() { std::call_once(initFlag, &SmartSingleton::init); return *instance; }</p><pre class='brush:php;toolbar:false;'>SmartSingleton(const SmartSingleton&) = delete; SmartSingleton& operator=(const SmartSingleton&) = delete;

private: SmartSingleton() = default; ~SmartSingleton() = default;

static void init() {     instance.reset(new SmartSingleton()); }  static std::unique_ptr<SmartSingleton> instance; static std::once_flag initFlag;

};

std::unique_ptr<SmartSingleton> SmartSingleton::instance = nullptr; std::once_flag SmartSingleton::initFlag;

优势:
std::call_once确保初始化逻辑只执行一次,配合std::once_flag实现高效线程安全,且由智能指针管理生命周期,避免内存泄漏。

基本上就这些。现代C++推荐优先使用局部静态变量方式,简单、安全、高效。若需延迟初始化或兼容旧环境,再考虑双检锁+智能指针方案。不复杂但容易忽略细节,尤其是析构顺序和跨线程访问问题。

上一篇
下一篇
text=ZqhQzanResources