单例模式实现(C++)

0. 概述

创建全局唯一对象,保证一个类只有一个实例

解决思路:

  • 默认构造函数私有
  • 使用一个静态方法作为构造函数,返回的是引用

1. 饿汉式

/**
 * 单例模式饿汉式实现
 */
class Singleton {
private:
    Singleton() {} //构造函数私有
    static Singleton instance; //实例static,并且是private
public:
    static Singleton& getInstance() {
        return instance; // 返回实例引用
    }
};

优点: 将耗时的初始化提前,不会出现首次调用性能问题;

缺点:不支持延迟加载(也就是真正用到的时候再创建实例);static Singleton instance;和static Singleton& getInstance()二者的初始化顺序不确定,如果在初始化完成之前调用 getInstance() 方法会返回一个未定义的实例。不能保证线程唯一,若需要线程唯一,需要加锁。

2. 懒汉式

/**
 * 单例模式懒汉式实现
 */
class Singleton {
private:
    Singleton() {} //构造函数私有
public:
    static Singleton& getInstance() {
        static Singleton instance;
        return instance;
    }
};

优点:延时加载,c++11后是线程安全的,C++11规定了local static在多线程条件下的初始化行为,要求编译器保证了内部静态变量的线程安全性。

测试代码:

#include <bits/stdc++.h>
using namespace std;

/**
 * 单例模式懒汉式实现
 */
class Singleton {
 private:
  Singleton(string value) : value_(value){} //构造函数私有
  string value_; //为了测试线程安全
 public:
  /**
 * Singletons should not be cloneable.
 */
  Singleton(Singleton &other) = delete;
  /**
   * Singletons should not be assignable.
   */
  void operator=(const Singleton &) = delete;

  static Singleton* GetInstance(const string& value) {
    static Singleton* instance = new Singleton(value);
    return instance;
  }

  //为了测试线程安全
  string value() {
    return value_;
  }
};

/**
 * 一个线程的主函数
 */
void ThreadFoo() {
  this_thread::sleep_for(chrono::milliseconds(1000));
  Singleton* singleton = Singleton::GetInstance("Foo");
  cout << singleton->value() << "\n";
}

/**
 * 另外一个线程的主函数
 */
void ThreadBar() {
  this_thread::sleep_for(chrono::milliseconds(1000));
  Singleton* singleton = Singleton::GetInstance("BAR");
  cout << singleton->value() << "\n";
}

int main() {
  cout << "If you see the same value, then singleton was reused(yay!\n" <<
       "If you see different values, then 2 singletons were created (booo!!)\n\n" <<
       "RESULT:\n";
  thread t1(ThreadFoo);
  thread t2(ThreadBar);
  t1.join();
  t2.join();
  return 0;
}

输出结果:

If you see the same value, then singleton was reused(yay!
If you see different values, then 2 singletons were created (booo!!)

RESULT:
BAR
BAR

 或者

If you see the same value, then singleton was reused(yay!
If you see different values, then 2 singletons were created (booo!!)

RESULT:
Foo
Foo

同4的结果,该次运行中多个线程之间实例是唯一的,只是不能确定哪一个线程最先初始化,最先初始化的就按照那种方式实例化对象 

3. 基础单例的另外实现(线程不唯一,类似于实现1)

参考资料

#include <bits/stdc++.h>
using namespace std;

class Singleton {
 private:
  // 构造函数设置为私有
  Singleton(const string value) : value_(value) {}

  static Singleton* singleton_;
  string value_;

 public:
  // 删除拷贝构造函数
  Singleton(Singleton&) = delete;

  // 删除赋值操作运算符
  Singleton& operator=(const Singleton&) = delete;

  // 获取实例的静态方法
  static Singleton* GetInstance(const string& value) {
    if (nullptr == singleton_) {
      singleton_ = new Singleton(value);
    }
    return singleton_;
  }

  // 为了测试
  string value() const {
    return value_;
  }
};

Singleton* Singleton::singleton_ = nullptr;

/**
 * 一个线程的主函数
 */
void ThreadFoo() {
  this_thread::sleep_for(chrono::milliseconds(1000));
  Singleton* singleton = Singleton::GetInstance("Foo");
  cout << singleton->value() << "\n";
}

/**
 * 另外一个线程的主函数
 */
void ThreadBar() {
  this_thread::sleep_for(chrono::milliseconds(1000));
  Singleton* singleton = Singleton::GetInstance("BAR");
  cout << singleton->value() << "\n";
}


int main() {
  cout << "If you see the same value, then singleton was reused(yay!\n" <<
          "If you see different values, then 2 singletons were created (booo!!)\n\n" <<
          "RESULT:\n";
  thread t1(ThreadFoo);
  thread t2(ThreadBar);
  t1.join();
  t2.join();

  return 0;
}

更加完整,补充了拷贝构造函数,指针实现,并且用多线程验证

结果:

If you see the same value, then singleton was reused(yay!
If you see different values, then 2 singletons were created (booo!!)

RESULT:
Foo
BAR

线程之间不唯一

4. 线程唯一的另外实现

这里线程安全指的是多线程的实例能否唯一

参考资料

思路:使用互斥锁

#include <bits/stdc++.h>

/**
 * The Singleton class defines the `GetInstance` method that serves as an
 * alternative to constructor and lets clients access the same instance of this
 * class over and over.
 */
class Singleton
{

  /**
   * The Singleton's constructor/destructor should always be private to
   * prevent direct construction/desctruction calls with the `new`/`delete`
   * operator.
   */
 private:
  static Singleton * pinstance_;
  static std::mutex mutex_;

 protected:
  Singleton(const std::string value): value_(value)
  {
  }
  ~Singleton() {}
  std::string value_;

 public:
  /**
   * Singletons should not be cloneable.
   */
  Singleton(Singleton &other) = delete;
  /**
   * Singletons should not be assignable.
   */
  void operator=(const Singleton &) = delete;
  /**
   * This is the static method that controls the access to the singleton
   * instance. On the first run, it creates a singleton object and places it
   * into the static field. On subsequent runs, it returns the client existing
   * object stored in the static field.
   */

  static Singleton *GetInstance(const std::string& value);
  /**
   * Finally, any singleton should define some business logic, which can be
   * executed on its instance.
   */
  void SomeBusinessLogic()
  {
    // ...
  }

  std::string value() const{
    return value_;
  }
};

/**
 * Static methods should be defined outside the class.
 */

Singleton* Singleton::pinstance_{nullptr};
std::mutex Singleton::mutex_;

/**
 * The first time we call GetInstance we will lock the storage location
 *      and then we make sure again that the variable is null and then we
 *      set the value. RU:
 */
Singleton *Singleton::GetInstance(const std::string& value)
{
  std::lock_guard<std::mutex> lock(mutex_);
  if (pinstance_ == nullptr)
  {
    pinstance_ = new Singleton(value);
  }
  return pinstance_;
}

void ThreadFoo(){
  // Following code emulates slow initialization.
  std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  Singleton* singleton = Singleton::GetInstance("FOO");
  std::cout << singleton->value() << "\n";
}

void ThreadBar(){
  // Following code emulates slow initialization.
  std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  Singleton* singleton = Singleton::GetInstance("BAR");
  std::cout << singleton->value() << "\n";
}

int main()
{
  std::cout <<"If you see the same value, then singleton was reused (yay!\n" <<
            "If you see different values, then 2 singletons were created (booo!!)\n\n" <<
            "RESULT:\n";
  std::thread t1(ThreadFoo);
  std::thread t2(ThreadBar);
  t1.join();
  t2.join();

  return 0;
}

总结:各个线程之间实例唯一了,但是不能确定是具体是哪一个,结果是先执行的线程的那一个实例,但是实际中无法确定到底是哪个,而且性能较差 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
单例模式是一种设计模式,它确保一个类只有一个实例,并提供一个全局访问点来获取该实例。在C++中,有多种实现单例模式的方式。 引用\[1\]中的代码演示了一种基于局部静态对象实现单例模式。在这个例子中,Singleton类的构造函数和析构函数都是私有的,这样就禁止了用户自己声明和定义实例。通过getInstance()函数获取唯一的实例。这种实现方式具有线程安全性。 引用\[2\]中提到了C++实现单例的几种方式。其中,最推荐的是懒汉式单例(magic static)——局部静态变量。这种方式的代码简洁,没有智能指针调用和双重检查锁定模式的风险。 引用\[3\]中的代码展示了使用局部静态变量实现的懒汉式单例。SingletonPattern_V3类的构造函数和析构函数都是私有的,通过Instance()函数获取唯一的实例。 综上所述,C++中可以使用局部静态变量来实现单例模式,确保全局只有一个实例,并提供全局访问点。这种方式具有线程安全性,代码简洁。 #### 引用[.reference_title] - *1* [设计模式之单例模式(C++)](https://blog.csdn.net/zhaitianbao/article/details/128946441)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [C++ 单例模式](https://blog.csdn.net/u011718663/article/details/115922357)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值