单例模式——饿汉式和懒汉式
单例模式:一个类只允许存在唯一的对象,并提供它的访问方式
创建思路:
1.禁止在类的外部创建对象:私有化构造函数
2.在类的内部提供唯一的对象:静态成员变量
3.提供访问唯一对象的方法:静态成员函数
创建方式:
1.饿汉式:无论用或不用,陈晓旭启动即创建
2.懒汉式:用的时候创建,不用的时候销毁
1 //单例模式 饿汉式 2 //无论用不用,程序启动即创建 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 7 class Singleton{ 8 public: 9 static Singleton& getInstance(){ 10 return s_instance; 11 } 12 int getData(){ 13 return m_data; 14 } 15 16 private: 17 Singleton(int data=0):m_data(data){} 18 Singleton(const Singleton& that); 19 20 static Singleton s_instance; 21 int m_data; 22 }; 23 Singleton Singleton::s_instance(666); 24 25 int main(){ 26 Singleton& s = Singleton::getInstance(); 27 cout << s.getData()<< endl; 28 29 return 0; 30 }
1 //单例对象:懒汉式 2 //用时创建,不用就销毁,需要手动调用销毁 3 //如果是多个使用者,单例对象的析构函数应该是最后一个使用者调用,故加入计数 4 #include <iostream> 5 using namespace std; 6 7 class Singleton{ 8 public: 9 static Singleton& getInstance(){ 10 if(p_instance == NULL){ 11 p_instance = new Singleton(12345); 12 } 13 s_count++; 14 return *p_instance; 15 } 16 //单例对象不用即销毁 17 //单例对象可能有多个使用者,应该时最后一个使用者调用release才去delete 18 void release(){ 19 if(--s_count == 0 && p_instance != NULL){ 20 delete p_instance; 21 p_instance = NULL; 22 } 23 } 24 void print(){ 25 cout << m_data << endl; 26 } 27 private: 28 Singleton(int data=0):m_data(data){cout << "单例对象被创建了" << endl;} 29 Singleton(const Singleton& that); 30 ~Singleton(){cout << "单例对象被销毁了" << endl;} 31 32 int m_data; 33 static Singleton* p_instance; 34 static int s_count;//计数:记录单例使用者的个数 35 }; 36 Singleton* Singleton::p_instance = NULL; 37 int Singleton::s_count = 0; 38 39 int main(){ 40 cout << "main" << endl; 41 Singleton& s1 = Singleton::getInstance(); 42 cout << &s1 << endl; 43 s1.print(); 44 Singleton& s2 = Singleton::getInstance(); 45 cout << &s2 << endl; 46 s2.print(); 47 Singleton& s3 = Singleton::getInstance(); 48 cout << &s3 << endl; 49 s3.print(); 50 s3.release(); 51 s2.release(); 52 s1.release(); 53 54 return 0; 55 }
1 //单例模式:懒汉式 2 //将上面的代码用classA将其封装 3 //利用A的构造函数和析构函数,自动创建和释放 4 //完成了多线程的安全单例模式 5 #include <iostream> 6 #include <pthread.h> 7 #include <cstdio> 8 using std::cout; 9 using std::endl; 10 11 //静态初始化锁 12 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 13 14 class Singleton{ 15 public: 16 static Singleton& getInstance(){ 17 pthread_mutex_lock(&mutex); //加锁 18 if(p_instance == NULL){ 19 p_instance = new Singleton(666); 20 } 21 m_count++; 22 pthread_mutex_unlock(&mutex); //解锁 23 return *p_instance; 24 } 25 26 void destroy(){ 27 cout << "destroy" << endl; 28 pthread_mutex_lock(&mutex); //加锁 29 if(--m_count == 0 && p_instance != NULL){ 30 delete p_instance; 31 p_instance = NULL; 32 } 33 pthread_mutex_unlock(&mutex); //解锁 34 } 35 int getData(){ 36 return m_data; 37 } 38 private: 39 Singleton(int data=0):m_data(data){cout << "Singleton" << endl;} 40 Singleton(const Singleton& that); 41 ~Singleton(){cout << "~Singleton" << endl;} 42 43 static Singleton* p_instance; 44 int m_data; 45 static int m_count; 46 }; 47 48 Singleton* Singleton::p_instance = NULL; 49 int Singleton::m_count = 0; 50 51 class A{ 52 public: 53 A():s(Singleton::getInstance()){} 54 ~A(){s.destroy();} 55 int get(){ 56 return s.getData(); 57 } 58 private: 59 Singleton& s; 60 }; 61 62 void* funa(void* arg){ 63 A a; 64 cout << "funa" << endl; 65 } 66 67 void* funb(void* arg){ 68 A a; 69 cout << "funb" << endl; 70 } 71 72 int main(){ 73 pthread_t pa, pb;//创建2个线程 74 pthread_create(&pa,NULL,funa,NULL); 75 pthread_create(&pb,NULL,funb,NULL); 76 77 pthread_detach(pa); 78 pthread_detach(pb); 79 80 getchar(); 81 //等待,销毁线程 82 //pthread_join(pa,NULL); 83 //pthread_join(pb,NULL); 84 85 //销毁mutex锁 86 pthread_mutex_destroy(&mutex); 87 return 0; 88 }