内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

c++11線程需要互斥量的原因是什么-創(chuàng)新互聯(lián)

本篇內(nèi)容介紹了“c++11線程需要互斥量的原因是什么”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!

創(chuàng)新互聯(lián)自2013年起,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務公司,擁有項目成都網(wǎng)站建設(shè)、成都網(wǎng)站制作網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元仲巴做網(wǎng)站,已為上家服務,為仲巴各地企業(yè)和個人服務,聯(lián)系電話:028-86922220

為什么需要互斥量

在多任務操作系統(tǒng)中,同時運行的多個任務可能都需要使用同一種資源。這個過程有點類似于,公司部門里,我在使用著打印機打印東西的同時(還沒有打印完),別人剛好也在此刻使用打印機打印東西,如果不做任何處理的話,打印出來的東西肯定是錯亂的。

#define _CRT_SECURE_NO_WARNINGS

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

// 打印機
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;
}

獨占互斥量std::mutex

互斥量的基本接口很相似,一般用法是通過lock()方法來阻塞線程,直到獲得互斥量的所有權(quán)為止。在線程獲得互斥量并完成任務之后,就必須使用unlock()來解除對互斥量的占用,lock()和unlock()必須成對出現(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; //全局互斥鎖對象,#include <mutex>

// 打印機
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可以簡化lock/unlock的寫法,同時也更安全,因為lock_guard在構(gòu)造時會自動鎖定互斥量,而在退出作用域后進行析構(gòu)時就會自動解鎖,從而避免忘了unlock操作。

#define _CRT_SECURE_NO_WARNINGS

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

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

// 打印機
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 會自動解鎖
}

// 線程一
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;
}

原子操作

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

#define _CRT_SECURE_NO_WARNINGS

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

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

//點擊函數(shù)
void func()
{
    for(int i = 0;  i < 1000000; ++i)
    {
        // 對全局數(shù)據(jù)進行無鎖訪問
        total += 1;
    }
}


void mytest()
{
    clock_t start = clock();    // 計時開始

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

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

    clock_t end = clock();    // 計時結(jié)束

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


    return;
}

int main()
{
    mytest();

    system("pause");
    return 0;
}

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

通過互斥鎖進行原子操作:

#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;

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


void mytest()
{
    clock_t start = clock();    // 計時開始

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

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

    clock_t end = clock();    // 計時結(jié)束

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


    return;
}

int main()
{
    mytest();

    system("pause");
    return 0;
}

每次運行的結(jié)果都一樣,只是耗時長點。

在新標準C++11,引入了原子操作的概念。

如果我們在多個線程中對這些類型的共享資源進行操作,編譯器將保證這些操作都是原子性的,也就是說,確保任意時刻只有一個線程對這個資源進行訪問,編譯器將保證多個線程訪問這個共享資源的正確性。從而避免了鎖的使用,提高了效率。

#define _CRT_SECURE_NO_WARNINGS

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

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

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


void mytest()
{
    clock_t start = clock();    // 計時開始

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

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

    clock_t end = clock();    // 計時結(jié)束

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


    return;
}

int main()
{
    mytest();

    system("pause");
    return 0;
}

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

“c++11線程需要互斥量的原因是什么”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

網(wǎng)站標題:c++11線程需要互斥量的原因是什么-創(chuàng)新互聯(lián)
鏈接URL:http://www.rwnh.cn/article20/hdjjo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃商城網(wǎng)站、網(wǎng)站導航網(wǎng)站內(nèi)鏈、電子商務、營銷型網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應式網(wǎng)站建設(shè)
弥渡县| 分宜县| 阜阳市| 桂林市| 新野县| 宝清县| 镶黄旗| 枞阳县| 垫江县| 神农架林区| 荣成市| 南平市| 清流县| 汝阳县| 启东市| 安泽县| 六枝特区| 托克托县| 金阳县| 望谟县| 桂林市| 慈利县| 湄潭县| 阿拉善右旗| 平和县| 吴堡县| 邳州市| 东乡| 康保县| 麟游县| 沙洋县| 犍为县| 江山市| 定结县| 宣武区| 大丰市| 玉环县| 彩票| 抚顺县| 玛沁县| 枣阳市|