单例模式

单例实现方式一:懒汉模式

 1 // 单例:懒汉模式,即在使用的时候才会创建实例对象。
 2 // 懒汉模式存在线程安全的问题,当对象没有被创建的时候,多个线程同时访问,则会出现多个线程创建实例对象的问题,需要加锁
 3 #include<mutex>
 4 #include<iostream>
 5 #include<cstdlib>
 6 using namespace std;
 7 
 8 mutex my_mutex;
 9 
10 class Test
11 {
12 public:
13     ~Test();
14     // 获取实例对象
15     static Test* getInstance();
16 private:
17     Test();
18     Test(const Test& t);
19     // 单一的实例对象,
20     static Test* t; // 指针,没有初始化
21 };
22 // 类外初始化
23 Test* Test::t = NULL;
24 
25 Test::Test()
26 {
27 }
28 
29 Test::Test(const Test& t)
30 {
31 }
32 
33 Test::~Test()
34 {
35 }
36 
37 Test* Test::getInstance()
38 {
39     // 多线程同时访问的时候, 就会发生多个线程创建实例对象的线程安全问题
40     // 使用互斥锁
41     if (t == NULL)
42     {
43         my_mutex.lock();
44         if (t == NULL)
45         {
46             // 创建
47             t = new Test;
48         }
49         my_mutex.unlock();
50     }
51     return t;
52 }
53 
54 int main()
55 {
56     Test *t1 = Test::getInstance();
57     Test* t2 = Test::getInstance();
58     cout << t1 << endl;
59     cout << t2 << endl;
60     system("pause");
61     return EXIT_SUCCESS;
62 }

单例实现方式二:饿汉模式,实例对象被使用之前就已经被创建,不存在线程安全问题。

饿汉模式实现一:

 1 // 饿汉模式一:实例对象在使用之前就已经被创建
 2 #include<cstdlib>
 3 #include<iostream>
 4 using namespace std;
 5 class MyTest2
 6 {
 7 public:
 8     ~MyTest2();
 9     static MyTest2* getInstance();
10 private:
11     MyTest2();
12     MyTest2(const MyTest2& t);
13     static MyTest2 m_t;
14 };
15 MyTest2 MyTest2::m_t;
16 
17 MyTest2::MyTest2()
18 {
19 }
20 
21 MyTest2::MyTest2(const MyTest2& t)
22 {
23 }
24 
25 MyTest2::~MyTest2()
26 {
27 }
28 MyTest2* MyTest2::getInstance()
29 {
30     return &m_t;
31 }
32 
33 int main()
34 {
35     MyTest2 *t1 = MyTest2::getInstance();
36     cout << t1 << endl;
37     MyTest2* t2 = MyTest2::getInstance();
38     cout << t2 << endl;
39     system("pause");
40     return EXIT_SUCCESS;
41 }

饿汉模式实现二:

 1 // 饿汉模式:实例对象在使用之前就已经被创建
 2 #include<cstdlib>
 3 #include<iostream>
 4 using namespace std;
 5 class MyTest3
 6 {
 7 public:
 8     ~MyTest3();
 9     static MyTest3* getInstance();
10 private:
11     MyTest3();
12     MyTest3(const MyTest3& t);
13     static MyTest3 *m_tt;
14 };
15 MyTest3* MyTest3::m_tt = new MyTest3;
16 
17 MyTest3::MyTest3()
18 {
19 }
20 
21 MyTest3::MyTest3(const MyTest3& t)
22 {
23 }
24 
25 MyTest3::~MyTest3()
26 {
27 }
28 MyTest3* MyTest3::getInstance()
29 {
30     return m_tt;
31 }
32 
33 int main()
34 {
35     MyTest3* t1 = MyTest3::getInstance();
36     cout << t1 << endl;
37     MyTest3* t2 = MyTest3::getInstance();
38     cout << t2 << endl;
39     system("pause");
40     return EXIT_SUCCESS;
41 }

单例实现方式三:使用静态局部变量方式实现,该方式只有支持C++11标准才行。

 1 // C++11新特性,使用静态局部变量实现
 2 #include<cstdlib>
 3 #include<iostream>
 4 using namespace std;
 5 class MyTest1
 6 {
 7 public:
 8     ~MyTest1();
 9     static MyTest1* getInstance();
10 private:
11     MyTest1();
12     MyTest1(const MyTest1& myt);
13 };
14 
15 MyTest1::MyTest1()
16 {
17 }
18 
19 MyTest1::MyTest1(const MyTest1& myt)
20 {
21 }
22 
23 MyTest1::~MyTest1()
24 {
25 }
26 
27 MyTest1* MyTest1::getInstance()
28 {
29     // 线程安全, 并且只会被初始化一次
30     static MyTest1* t = new MyTest1;
31     return t;
32 }
33 int main()
34 {
35     MyTest1* t1 = MyTest1::getInstance();
36     cout << t1 << endl;
37     MyTest1* t2 = MyTest1::getInstance();
38     cout << t2 << endl;
39     system("pause");
40     return EXIT_SUCCESS;
41 }
原文地址:https://www.cnblogs.com/qingpeng/p/13546989.html