C++11多線程同步方法

std::mutex

#include <iostream>
#include <vector>
#include <mutex>
#include <thread>

using namespace std;

volatile int counter = 0;

void ft1()
{
    for (int i = 0; i < 100000; ++i)
    {
        ++counter;
    }
}

int main()
{
    std::vector<std::thread> ts;
    for(int i = 0; i < 5; ++i)
    {
        ts.emplace_back(std::thread(ft1));
    }

    for (auto& t : ts)
    {
        t.join();
    }

    cout << counter << endl;
    system("pause");
    return 0;
}

輸出結(jié)果: counter 小于 5 * 100000

#include <iostream>
#include <vector>
#include <mutex>
#include <thread>

using namespace std;

volatile int counter = 0;
std::mutex mtx;

void ft1()
{
    for (int i = 0; i < 100000; ++i)
    {
        mtx.lock();
        ++counter;
        mtx.unlock();
    }
}

int main()
{
    std::vector<std::thread> ts;
    for(int i = 0; i < 5; ++i)
    {
        ts.emplace_back(std::thread(ft1));
    }

    for (auto& t : ts)
    {
        t.join();
    }

    cout << counter << endl;
    system("pause");
    return 0;
}

輸出結(jié)果: counter 等于 5 * 100000

測(cè)試發(fā)現(xiàn)在加鎖的情況下,輸出結(jié)果會(huì)慢很多,網(wǎng)上查閱c++11的mutex性能損耗比較大。

2. std::promise

#include <iostream>
#include <thread>
#include <future>

using namespace std;

volatile int counter = 0;

void ft1(std::future<int>& fut)
{
    int x = fut.get();

    cout << "x = " << x << "\n";
}

int main()
{
    std::promise<int> prom;
    std::future<int> fut = prom.get_future();

    std::thread t(ft1, std::ref(fut));
    prom.set_value(10);  // 注釋該句ft1線程阻塞,知道fut.get()獲取到set_vaule()的值
    t.join();

    system("pause");
    return 0;
}

3. std::atomic<T>

#include <iostream>
#include <vector>
#include <mutex>
#include <thread>
#include <atomic>

using namespace std;

std::atomic<int> counter(0);

void ft1()
{
    for (int i = 0; i < 100000; ++i)
    {
        ++counter;
    }
}

int main()
{
    std::vector<std::thread> ts;
    for(int i = 0; i < 5; ++i)
    {
        ts.emplace_back(std::thread(ft1));
    }

    for (auto& t : ts)
    {
        t.join();
    }

    cout << counter << endl;
    system("pause");
    return 0;
}

測(cè)試性能由于std::mutex

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 接著上節(jié) mutex,本節(jié)主要介紹atomic的內(nèi)容,練習(xí)代碼地址。本文參考http://www.cplusplu...
    jorion閱讀 74,103評(píng)論 1 14
  • 接著上節(jié) atomic,本節(jié)主要介紹condition_varible的內(nèi)容,練習(xí)代碼地址。本文參考http://...
    jorion閱讀 8,653評(píng)論 0 7
  • 接著上上節(jié) thread ,本節(jié)主要介紹mutex的內(nèi)容,練習(xí)代碼地址。<mutex>:該頭文件主要聲明了與互斥量...
    jorion閱讀 12,697評(píng)論 2 4
  • 本文主要是針對(duì)C++中多線程并發(fā)操作參見(jiàn)(cplusplus)進(jìn)行解釋,文章從下面幾個(gè)方面進(jìn)行學(xué)習(xí),分別介紹多線程...
    jorion閱讀 9,993評(píng)論 0 10
  • 不講語(yǔ)言特性,只從工程角度出發(fā),個(gè)人覺(jué)得C++標(biāo)準(zhǔn)委員會(huì)在C++11中對(duì)多線程庫(kù)的引入是有史以來(lái)做得最人道的一件事...
    stidio閱讀 13,991評(píng)論 0 11

友情鏈接更多精彩內(nèi)容