日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > c/c++ >内容正文

c/c++

C++单例模式:单例模式遇到多线程

發(fā)布時(shí)間:2025/3/15 c/c++ 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++单例模式:单例模式遇到多线程 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

單例模式介紹

單例模式主要有2中形式,一種是餓漢式,一種是懶漢式。

餓漢式:程序一加載單例模式就已經(jīng)創(chuàng)建了,也就很饑餓嘛。因?yàn)槭?span style="color:#ff0000">靜態(tài)屬性進(jìn)行單例初始化,所以優(yōu)點(diǎn)是線程是安全的缺點(diǎn)無(wú)論用戶是否使用單例對(duì)象都會(huì)創(chuàng)建單例對(duì)象。

懶漢式:當(dāng)用戶使用單例對(duì)象時(shí),才去創(chuàng)建單例對(duì)象,所以很懶惰嘛。優(yōu)點(diǎn)是用戶不使用就不會(huì)創(chuàng)建對(duì)象,缺點(diǎn)是 當(dāng)遇到多線程是,是線程不安全的,但是我們可以使用加強(qiáng)版的也就是線程安全的懶漢式下面我們會(huì)進(jìn)行講解。

餓漢式和懶漢式代碼

代碼展示

#include <iostream>using namespace std; class Singleton_Hungry { private:Singleton_Hungry(){cout << "我是餓漢式,在程序加載時(shí),我就已經(jīng)存在了。" << endl;}static Singleton_Hungry* singleton; public:static Singleton_Hungry* getInstace(){return singleton;}}; //靜態(tài)屬性類外初始化 Singleton_Hungry* Singleton_Hungry::singleton = new Singleton_Hungry;class Singleton_Lazy { private:Singleton_Lazy(){cout << "我是懶漢式,在別人需要我的時(shí)候,我才現(xiàn)身。" << endl;}static Singleton_Lazy* singleton; public:static Singleton_Lazy* getInstance(){if (NULL == singleton){singleton = new Singleton_Lazy;}return singleton;} }; Singleton_Lazy* Singleton_Lazy::singleton = NULL;int main(int argc, char *argv[]) {Singleton_Hungry *hungry1 = Singleton_Hungry::getInstace();Singleton_Hungry *hungry2 = Singleton_Hungry::getInstace();cout << "hungry1地址:" << hungry1 << ",hungry2地址:" << hungry2 << endl;Singleton_Lazy *lazy1 = Singleton_Lazy::getInstance();Singleton_Lazy *lazy2 = Singleton_Lazy::getInstance();cout << "lazy1地址:" << lazy1 << ",lazy2地址:" << lazy1 << endl;return 0; }

運(yùn)行結(jié)果


懶漢式遇到多線程

由于懶漢式在用戶需要是才進(jìn)行單例對(duì)象的創(chuàng)建,如果遇到多線程容易發(fā)生內(nèi)存泄漏,我們可以用c++中的線程互斥對(duì)象mutex來(lái)進(jìn)行加強(qiáng)。多線程用thread類進(jìn)模擬。

代碼展示

#include <iostream> #include <mutex> #include <thread>using namespace std; mutex mu;//線程互斥對(duì)象 class Singleton_Hungry { private:Singleton_Hungry(){cout << "我是餓漢式,在程序加載時(shí),我就已經(jīng)存在了。" << endl;}static Singleton_Hungry* singleton; public:static Singleton_Hungry* getInstace(){return singleton;}}; //靜態(tài)屬性類外初始化 Singleton_Hungry* Singleton_Hungry::singleton = new Singleton_Hungry;class Singleton_Lazy { private:Singleton_Lazy(){cout << "我是懶漢式,在別人需要我的時(shí)候,我才現(xiàn)身。" << endl;}static Singleton_Lazy* singleton; public:static Singleton_Lazy* getInstance(){if (NULL == singleton){mu.lock();//關(guān)閉鎖if (NULL == singleton){singleton = new Singleton_Lazy;}mu.unlock();//打開鎖}return singleton;} }; Singleton_Lazy* Singleton_Lazy::singleton = NULL; void thread01() {for (int i = 0; i < 5; i++){cout << "thread01 working...." << endl;Singleton_Lazy *lazy1 = Singleton_Lazy::getInstance();cout << "thread01創(chuàng)建單例lazy1地址:" << lazy1 << endl;} } void thread02() {for (int i = 0; i < 5; i++){cout << "thread02 working...." << endl;Singleton_Lazy *lazy2 = Singleton_Lazy::getInstance();cout << "thread02創(chuàng)建單例lazy2地址:" << lazy2 << endl;} }int main(int argc, char *argv[]) {thread thread1(thread01);thread thread2(thread01);thread1.detach();thread2.detach();for (int i = 0; i < 5; i++){cout << "Main thread working..." << endl;Singleton_Lazy *main = Singleton_Lazy::getInstance();cout << "Main 創(chuàng)建單例lazy地址:" << main << endl;}return 0; }

多線程下線程不安全的懶漢式測(cè)試

下圖是將40行45行線程互斥對(duì)象操作注釋的情況,發(fā)生了內(nèi)存泄漏,創(chuàng)建了多個(gè)單例。圖中字打錯(cuò)了。。。


多線程下線程安全的懶漢式測(cè)試


總結(jié)

以上是生活随笔為你收集整理的C++单例模式:单例模式遇到多线程的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。