1 基本概念

单例Singleton是设计模式的一种,其特点是只提供唯一一个类的实例,具有全局变量的特点,在任何位置都可以通过接口获取到那个唯一实例;

  • 全局只有一个实例:static 特性,同时禁止用户自己声明并定义实例(把构造函数设为 private)
  • 线程安全
  • 禁止赋值和拷贝
  • 用户通过接口获取实例:使用 static 类成员函数

2 单例的实现

2.1 基础实现

我们很容易想到通过设置一个全局变量,就可以将某个类的实例限制为单例模式。

代码清单 2.1.1 全局变量方式实现单例模式
  1. //Singleton.h
  2. #ifndef SINGLETON_H
  3. #define SINGLETON_H
  4. #include <iostream>
  5. using namespace std;
  6. class Singleton{
  7. public:
  8. Singleton();
  9. static Singleton * getInstance(void);
  10. void showinfo(void);
  11. };
  12. #endif
  1. //Singleton.cpp
  2. Singleton * Singleton::getInstance(void)
  3. {
  4. if(gInstance == NULL)
  5. {
  6. gInstance = new Singleton();
  7. }
  8. return gInstance;
  9. }
  10. Singleton::Singleton()
  11. {
  12. cout << "Singleton()" << endl;
  13. }
  14. void Singleton::showinfo(void)
  15. {
  16. cout << "This is Singleton" << endl;
  17. }

然后在main函数中调用

  1. #include "Singleton.h"
  2. int main(void)
  3. {
  4. Singleton *s = Singleton::getInstance();
  5. Singleton *p = Singleton::getInstance();
  6. s->showinfo();
  7. p->showinfo();
  8. return 0;
  9. }

运行结果:

Singleton() This is Singleton This is Singleton

可见通过全局变量的限制,Singleton只能进行一次实例化的操作。但是在多个线程同时创建这个类的实例时,全局变量的这种方式显示是极不安全的。虽然概率极低,但是我们应该考虑到这种多个线程的场景。

代码清单 2.1.2 线程创建
  1. #include "Singleton.h"
  2. #include <pthread.h>
  3. #include <unistd.h>
  4. void *start_routine_thread1(void *arg)
  5. {
  6. cout<<"this is thread 1 ..."<<endl;
  7. Singleton *s = Singleton::getInstance();
  8. s->showinfo();
  9. return NULL;
  10. }
  11. void *start_routine_thread2(void *arg)
  12. {
  13. cout<<"this is thread 2 ..."<<endl;
  14. Singleton *s = Singleton::getInstance();
  15. s->showinfo();
  16. return NULL;
  17. }
  18. int main(void)
  19. {
  20. pthread_t thread1ID;
  21. pthread_t thread2ID;
  22. pthread_create(&thread1ID, NULL, start_routine_thread1, NULL);
  23. pthread_create(&thread2ID, NULL, start_routine_thread2, NULL);
  24. return 0;
  25. }

注意使用pthread时,g++编译时要链接pthread库

2.2 互斥锁防止临界冲突

为了防止多个线程同时调用一段代码,采用锁的机制来防止这类冲突,避免实例重复创建。

代码清单 2.2.1 增加互斥锁
  1. #include "Singleton.h"
  2. #include <unistd.h>
  3. static Singleton * gInstance;
  4. static pthread_mutex_t g_tMutex = PTHREAD_MUTEX_INITIALIZER;
  5. Singleton * Singleton::getInstance(void)
  6. {
  7. if(gInstance == NULL)
  8. {
  9. pthread_mutex_lock(&g_tMutex);
  10. if(gInstance == NULL)
  11. gInstance = new Singleton();
  12. pthread_mutex_unlock(&g_tMutex);
  13. }
  14. return gInstance;
  15. }
  16. Singleton::Singleton()
  17. {
  18. cout << "Singleton()" << endl;
  19. }
  20. void Singleton::showinfo(void)
  21. {
  22. cout << "This is Singleton" << endl;
  23. }

2.3 构造函数设置为private

为了防止用new方法来绕过getInstance接口,我们有必要把构造函数定义为私有成员。来防止用户直接使用该接口。

代码清单 2.3.1 隐藏构造函数
  1. #ifndef SINGLETON_H
  2. #define SINGLETON_H
  3. #include <iostream>
  4. using namespace std;
  5. class Singleton{
  6. Singleton();
  7. public:
  8. static Singleton * getInstance(void);
  9. void showinfo(void);
  10. };
  11. #endif

2.4 将全局变量转化为类的静态成员

// 懒汉模式 饿汉模式

提前生成好,