C++多线程编程第七讲--单例设计模式共享数据分析,解决,call_once

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//(1)设计模式大概谈
// 使得程序灵活,但是代码阅读非常的晦涩。
//(2)单例设计模式
// 使用频率比较高。只能创建一个对象。
#include<iostream>
 
using namespace std;
 
class MyCAS
{
private:
    MyCAS() {};      //私有化构造函数
 
private:
    static MyCAS* m_instance;
 
    class Release
    {
    public:
        ~Release()
        {
            if (MyCAS::m_instance != nullptr)
            {
                delete MyCAS::m_instance;
                MyCAS::m_instance = nullptr;
            }
        }
    };
 
    static  Release r;
 
public:
    static MyCAS* GetInstance()
    {
        if (m_instance == nullptr)
        {
            m_instance = new MyCAS();
        }
 
        return m_instance;
    }
 
    void print()
    {
        cout << this << endl;
        cout << "test..." << endl;
    }
};
 
MyCAS* MyCAS::m_instance = nullptr;
 
int main()
{
    MyCAS* p_a = MyCAS::GetInstance();
 
    MyCAS* p_b = MyCAS::GetInstance();
 
    p_a->print();
    p_b->print();
 
    return 0;
}
//(3)单例设计模式共享数问题分析、解决
// 建议在主线程中创建其他线程之前就把单例类给创建完成。
#include<iostream>
#include<thread>
#include<mutex>
 
using namespace std;
 
std::mutex my_mutex;
 
class MyCAS
{
private:
    MyCAS() {};      //私有化构造函数
 
private:
    static MyCAS* m_instance;
 
    class Release
    {
    public:
        ~Release()
        {
            if (MyCAS::m_instance != nullptr)
            {
                delete MyCAS::m_instance;
                MyCAS::m_instance = nullptr;
            }
        }
    };
 
    static  Release r;
 
public:
    static MyCAS* GetInstance()
    {
        if (m_instance == nullptr)
        {
            std::unique_lock<mutex> my_unique_lock(my_mutex);      //自动加锁
            if (m_instance == nullptr)
            {
                m_instance = new MyCAS();
            }
        }
 
        return m_instance;
    }
 
    void print()
    {
        cout << this << endl;
        cout << "test..." << endl;
    }
};
 
MyCAS* MyCAS::m_instance = nullptr;
 
 
//线程入口函数
void my_thread()
{
    MyCAS* p_a = MyCAS::GetInstance();
    p_a->print();
}
 
int main()
{
    thread thread1(my_thread);
    thread thread2(my_thread);
 
    thread1.join();
    thread2.join();
 
    return 0;
}
//(4)std::call_once()    函数模板,C++11引入的函数   该函数的第二个参数是一个函数名a,保证函数a只能调用一次。
//是具备互斥量的能力的,效率上比互斥量更高效。
//需要与一个std::once_flag配合使用
#include<iostream>
#include<thread>
#include<mutex>
 
using namespace std;
 
std::mutex my_mutex;
 
std::once_flag g_flag;      //系统定义的标记
 
class MyCAS
{
private:
    MyCAS() {};      //私有化构造函数
 
private:
    static MyCAS* m_instance;
 
    static void CreateInstance()     //只调用一次
    {
        cout << "CreateInstance() running ..." << endl;
        m_instance = new MyCAS();
        cout << "CreateInstance() finished" << endl;
    }
 
    class Release
    {
    public:
        ~Release()
        {
            if (MyCAS::m_instance != nullptr)
            {
                delete MyCAS::m_instance;
                MyCAS::m_instance = nullptr;
            }
        }
    };
 
    static Release r;
 
public:
    static MyCAS* GetInstance()
    {
        std::call_once(g_flag, CreateInstance);     //call_once是一个原子操作,根据g_flag保证第二个参数中的函数只能被调用一次
         
        cout << "GetInstance finished..." << endl;
 
        return m_instance;
    }
 
    void print()
    {
        cout << this << endl;
        cout << "test..." << endl;
    }
};
 
MyCAS* MyCAS::m_instance = nullptr;
 
 
//线程入口函数
void my_thread()
{
    MyCAS* p_a = MyCAS::GetInstance();
    p_a->print();
}
 
int main()
{
    thread thread1(my_thread);
    thread thread2(my_thread);
 
    thread1.join();
    thread2.join();
 
    return 0;
}

  

posted on   xcxfury001  阅读(28)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示