最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

淺談c++11線程的互斥量

 更新時(shí)間:2021年06月08日 15:22:52   作者:lsgxeva  
互斥量是個(gè)類(lèi)對(duì)象,理解成一把鎖(保護(hù)共享數(shù)據(jù),其他想操作共享數(shù)據(jù)的線程必須等待解鎖),互斥量使用要小心,保護(hù)數(shù)據(jù)不多也不少,少了則沒(méi)達(dá)到保護(hù)效果,多了則影響效率。本文將介紹c++11線程的互斥量,感興趣的同學(xué),可以參考下。

為什么需要互斥量

在多任務(wù)操作系統(tǒng)中,同時(shí)運(yùn)行的多個(gè)任務(wù)可能都需要使用同一種資源。這個(gè)過(guò)程有點(diǎn)類(lèi)似于,公司部門(mén)里,我在使用著打印機(jī)打印東西的同時(shí)(還沒(méi)有打印完),別人剛好也在此刻使用打印機(jī)打印東西,如果不做任何處理的話(huà),打印出來(lái)的東西肯定是錯(cuò)亂的。

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <chrono>
#include <thread>

// 打印機(jī)
void printer(const char *str)
{
    while(*str != '\0')
    {
        std::cout << *str;
        str++;
        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    }
    std::cout << std::endl;
}

// 線程一
void func1()
{
    const char *str = "hello";
    printer(str);
}

// 線程二
void func2()
{
    const char *str = "world";
    printer(str);
}


void mytest()
{
    std::thread t1(func1);
    std::thread t2(func2);

    t1.join();
    t2.join();

    return;
}

int main()
{
    mytest();

    system("pause");
    return 0;
}

獨(dú)占互斥量std::mutex

互斥量的基本接口很相似,一般用法是通過(guò)lock()方法來(lái)阻塞線程,直到獲得互斥量的所有權(quán)為止。在線程獲得互斥量并完成任務(wù)之后,就必須使用unlock()來(lái)解除對(duì)互斥量的占用,lock()和unlock()必須成對(duì)出現(xiàn)。try_lock()嘗試鎖定互斥量,如果成功則返回true, 如果失敗則返回false,它是非阻塞的。

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <chrono>
#include <thread>
#include <mutex>

std::mutex g_lock; //全局互斥鎖對(duì)象,#include <mutex>

// 打印機(jī)
void printer(const char *str)
{
    g_lock.lock(); //上鎖
    while(*str != '\0')
    {
        std::cout << *str;
        str++;
        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    }
    std::cout << std::endl;
    g_lock.unlock(); // 解鎖
}

// 線程一
void func1()
{
    const char *str = "hello";
    printer(str);
}

// 線程二
void func2()
{
    const char *str = "world";
    printer(str);
}


void mytest()
{
    std::thread t1(func1);
    std::thread t2(func2);

    t1.join();
    t2.join();

    return;
}

int main()
{
    mytest();

    system("pause");
    return 0;
}

使用std::lock_guard可以簡(jiǎn)化lock/unlock的寫(xiě)法,同時(shí)也更安全,因?yàn)閘ock_guard在構(gòu)造時(shí)會(huì)自動(dòng)鎖定互斥量,而在退出作用域后進(jìn)行析構(gòu)時(shí)就會(huì)自動(dòng)解鎖,從而避免忘了unlock操作。

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <chrono>
#include <thread>
#include <mutex>

std::mutex g_lock; //全局互斥鎖對(duì)象,#include <mutex>

// 打印機(jī)
void printer(const char *str)
{
    std::lock_guard<std::mutex> locker(g_lock); // lock_guard 上鎖
    while(*str != '\0')
    {
        std::cout << *str;
        str++;
        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    }
    std::cout << std::endl;
    // 即將推出作用域 lock_guard 會(huì)自動(dòng)解鎖
}

// 線程一
void func1()
{
    const char *str = "hello";
    printer(str);
}

// 線程二
void func2()
{
    const char *str = "world";
    printer(str);
}


void mytest()
{
    std::thread t1(func1);
    std::thread t2(func2);

    t1.join();
    t2.join();

    return;
}

int main()
{
    mytest();

    system("pause");
    return 0;
}

原子操作

所謂的原子操作,取的就是“原子是最小的、不可分割的最小個(gè)體”的意義,它表示在多個(gè)線程訪問(wèn)同一個(gè)全局資源的時(shí)候,能夠確保所有其他的線程都不在同一時(shí)間內(nèi)訪問(wèn)相同的資源。也就是他確保了在同一時(shí)刻只有唯一的線程對(duì)這個(gè)資源進(jìn)行訪問(wèn)。這有點(diǎn)類(lèi)似互斥對(duì)象對(duì)共享資源的訪問(wèn)的保護(hù),但是原子操作更加接近底層,因而效率更高。

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <chrono>
#include <thread>

//全局的結(jié)果數(shù)據(jù)
long total = 0;

//點(diǎn)擊函數(shù)
void func()
{
    for(int i = 0;  i < 1000000; ++i)
    {
        // 對(duì)全局?jǐn)?shù)據(jù)進(jìn)行無(wú)鎖訪問(wèn)
        total += 1;
    }
}


void mytest()
{
    clock_t start = clock();    // 計(jì)時(shí)開(kāi)始

    //線程
    std::thread t1(func);
    std::thread t2(func);

    t1.join();
    t2.join();

    clock_t end = clock();    // 計(jì)時(shí)結(jié)束

    std::cout << "total = " << total << std::endl;
    std::cout << "time = " << end-start << " ms" << std::endl;


    return;
}

int main()
{
    mytest();

    system("pause");
    return 0;
}

由于線程間對(duì)數(shù)據(jù)的競(jìng)爭(zhēng)而導(dǎo)致每次運(yùn)行的結(jié)果都不一樣。因此,為了防止數(shù)據(jù)競(jìng)爭(zhēng)問(wèn)題,我們需要對(duì)total進(jìn)行原子操作。

通過(guò)互斥鎖進(jìn)行原子操作:

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <chrono>
#include <thread>
#include <mutex>

std::mutex g_lock;

//全局的結(jié)果數(shù)據(jù)
long total = 0;

//點(diǎn)擊函數(shù)
void func()
{
    for(int i = 0;  i < 1000000; ++i)
    {
        g_lock.lock(); // 加鎖
        total += 1;
        g_lock.unlock(); // 加鎖
    }
}


void mytest()
{
    clock_t start = clock();    // 計(jì)時(shí)開(kāi)始

    //線程
    std::thread t1(func);
    std::thread t2(func);

    t1.join();
    t2.join();

    clock_t end = clock();    // 計(jì)時(shí)結(jié)束

    std::cout << "total = " << total << std::endl;
    std::cout << "time = " << end-start << " ms" << std::endl;


    return;
}

int main()
{
    mytest();

    system("pause");
    return 0;
}

每次運(yùn)行的結(jié)果都一樣,只是耗時(shí)長(zhǎng)點(diǎn)。

在新標(biāo)準(zhǔn)C++11,引入了原子操作的概念。

如果我們?cè)诙鄠€(gè)線程中對(duì)這些類(lèi)型的共享資源進(jìn)行操作,編譯器將保證這些操作都是原子性的,也就是說(shuō),確保任意時(shí)刻只有一個(gè)線程對(duì)這個(gè)資源進(jìn)行訪問(wèn),編譯器將保證多個(gè)線程訪問(wèn)這個(gè)共享資源的正確性。從而避免了鎖的使用,提高了效率。

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <chrono>
#include <thread>
#include <atomic>

//原子數(shù)據(jù)類(lèi)型
std::atomic<long> total(0); //需要頭文件 #include <atomic>

//點(diǎn)擊函數(shù)
void func()
{
    for(int i = 0;  i < 1000000; ++i)
    {
        // 
        total += 1;
    }
}


void mytest()
{
    clock_t start = clock();    // 計(jì)時(shí)開(kāi)始

    //線程
    std::thread t1(func);
    std::thread t2(func);

    t1.join();
    t2.join();

    clock_t end = clock();    // 計(jì)時(shí)結(jié)束

    std::cout << "total = " << total << std::endl;
    std::cout << "time = " << end-start << " ms" << std::endl;


    return;
}

int main()
{
    mytest();

    system("pause");
    return 0;
}

原子操作的實(shí)現(xiàn)跟普通數(shù)據(jù)類(lèi)型類(lèi)似,但是它能夠在保證結(jié)果正確的前提下,提供比mutex等鎖機(jī)制更好的性能。

以上就是淺談c++11線程的互斥量的詳細(xì)內(nèi)容,更多關(guān)于c++11線程的互斥量的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Visual?C++?6.0添加一個(gè)對(duì)話(huà)框的實(shí)現(xiàn)步驟

    Visual?C++?6.0添加一個(gè)對(duì)話(huà)框的實(shí)現(xiàn)步驟

    VC6.0是微軟公司推出的一款集成開(kāi)發(fā)環(huán)境,本文主要介紹了Visual?C++?6.0添加一個(gè)對(duì)話(huà)框的實(shí)現(xiàn)步驟,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-06-06
  • C語(yǔ)言實(shí)現(xiàn)歌手比賽系統(tǒng)

    C語(yǔ)言實(shí)現(xiàn)歌手比賽系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)歌手比賽系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • c語(yǔ)言單詞搜索的實(shí)現(xiàn)

    c語(yǔ)言單詞搜索的實(shí)現(xiàn)

    本文主要介紹了c語(yǔ)言單詞搜索的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • C++中的ilst使用以及模擬實(shí)現(xiàn)

    C++中的ilst使用以及模擬實(shí)現(xiàn)

    list是一個(gè)類(lèi)模板,加<類(lèi)型>實(shí)例化才是具體的類(lèi),可以在任意位置進(jìn)行插入和刪除的序列式容器,本文將通過(guò)代碼示例給大家介紹一下C++中的ilst使用以及模擬實(shí)現(xiàn),需要的朋友可以參考下
    2023-08-08
  • C語(yǔ)言中關(guān)于計(jì)算字符串長(zhǎng)度的幾種方式

    C語(yǔ)言中關(guān)于計(jì)算字符串長(zhǎng)度的幾種方式

    這篇文章主要介紹了C語(yǔ)言中關(guān)于計(jì)算字符串長(zhǎng)度的幾種方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • VS2019實(shí)現(xiàn)C++的第一個(gè)MFC程序

    VS2019實(shí)現(xiàn)C++的第一個(gè)MFC程序

    本文主要介紹了VS2019實(shí)現(xiàn)C++的第一個(gè)MFC程序,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-06-06
  • OpenCV實(shí)現(xiàn)圖像邊緣檢測(cè)

    OpenCV實(shí)現(xiàn)圖像邊緣檢測(cè)

    這篇文章主要為大家詳細(xì)介紹了OpenCV實(shí)現(xiàn)圖像邊緣檢測(cè),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • c++基礎(chǔ)使用STL的注意點(diǎn)詳解

    c++基礎(chǔ)使用STL的注意點(diǎn)詳解

    這篇文章主要為大家介紹了c++基礎(chǔ)使用STL的注意點(diǎn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2021-12-12
  • 深入理解C/C++中的寫(xiě)時(shí)拷貝

    深入理解C/C++中的寫(xiě)時(shí)拷貝

    這篇文章主要給大家介紹了C/C++中寫(xiě)時(shí)拷貝的相關(guān)資料,所謂寫(xiě)時(shí)拷貝也就是拖延版的深拷貝,下面文章中介紹的非常清楚,需要的朋友可以參考學(xué)習(xí),下面來(lái)一起看看吧。
    2017-03-03
  • C語(yǔ)言詳細(xì)講解二分查找用法

    C語(yǔ)言詳細(xì)講解二分查找用法

    二分查找法,又叫做折半查找法,它是一種效率較高的查找方法。但是,折半查找要求線性表必須采用順序存儲(chǔ)結(jié)構(gòu),而且表中元素按關(guān)鍵字有序排列
    2022-04-04

最新評(píng)論

海丰县| 武强县| 禄丰县| 咸宁市| 蓝田县| 子长县| 和政县| 潍坊市| 湘潭县| 海盐县| 三河市| 乌拉特后旗| 凌海市| 定兴县| 滨州市| 白银市| 广平县| 宕昌县| 庆阳市| 千阳县| 汕头市| 磴口县| 铜梁县| 墨脱县| 新昌县| 聂拉木县| 华阴市| 陕西省| 托克托县| 普格县| 来凤县| 科技| 安乡县| 南汇区| 锡林郭勒盟| 西吉县| 赤峰市| 九龙坡区| 宁明县| 南通市| 乐安县|