国产精品久久久久野外,国产精品无码久久综合,日本欧美大码aⅴ在线播放,苍井空浴缸大战猛男120分钟

編程代碼
新聞詳情

C++11多線程編程(四)——原子操作

發(fā)布時(shí)間:2021-01-05 11:48:12 瀏覽次數(shù):2226

今天和大家說(shuō)說(shuō)C++多線程中的原子操作。首先為什么會(huì)有原子操作呢?這純粹就是C++這門(mén)語(yǔ)言的特性所決定的,C++這門(mén)語(yǔ)言是為性能而生的,它對(duì)性能的追求是沒(méi)有極限的,它總是想盡一切辦法提高性能?;コ怄i是可以實(shí)現(xiàn)數(shù)據(jù)的同步,但同時(shí)是以犧牲性能為代價(jià)的。口說(shuō)無(wú)憑,我們做個(gè)實(shí)驗(yàn)就知道了。


我們將一個(gè)數(shù)加一再減一,循環(huán)一定的次數(shù),開(kāi)啟20個(gè)線程來(lái)觀察,這個(gè)正確的結(jié)果應(yīng)該是等于0的。

首先是不加任何互斥鎖同步

#include <iostream>
#include <thread>
#include <atomic>
#include <time.h>
#include <mutex>
using namespace std;
 
#define MAX 100000
#define THREAD_COUNT 20
int total = 0;
void thread_task()
{
    for (int i = 0; i < MAX; i++)
    {
        total += 1;
        total -= 1;
    }
}
 
int main()
{
    clock_t start = clock();
    thread t[THREAD_COUNT];
    for (int i = 0; i < THREAD_COUNT; ++i)
    {
        t[i] = thread(thread_task);
    }
    for (int i = 0; i < THREAD_COUNT; ++i)
    {
        t[i].join();
    }
    
    clock_t finish = clock();
    cout << "result:" << total << endl;
    cout << "duration:" << finish - start << "ms" << endl;
    return 0;
}

以上程序運(yùn)行時(shí)相關(guān)快的,但是結(jié)果卻是不正確的。

C++11多線程編程(四)——原子操作

那么我們將線程加上互斥鎖mutex再來(lái)看看。

#include <iostream>
#include <thread>
#include <atomic>
#include <time.h>
#include <mutex>
using namespace std;
 
#define MAX 100000
#define THREAD_COUNT 20
 
int total = 0;
mutex mt;
 
void thread_task()
{
    for (int i = 0; i < MAX; i++)
    {
        mt.lock();
        total += 1;
        total -= 1;
        mt.unlock();
    }
}
 
int main()
{
    clock_t start = clock();
    thread t[THREAD_COUNT];
    for (int i = 0; i < THREAD_COUNT; ++i)
    {
        t[i] = thread(thread_task);
    }
    for (int i = 0; i < THREAD_COUNT; ++i)
    {
        t[i].join();
    }
    
    clock_t finish = clock();
    // 輸出結(jié)果
    cout << "result:" << total << endl;
    cout << "duration:" << finish - start << "ms" << endl;
 
    return 0;
}

我們可以看到運(yùn)行結(jié)果是正確的,但是時(shí)間比原來(lái)慢太多了。雖然很無(wú)奈,但這也是沒(méi)有辦法的,因?yàn)橹挥性诒WC準(zhǔn)確的前提才能去追求性能。

C++11多線程編程(四)——原子操作

那有沒(méi)有什么辦法在保證準(zhǔn)確的同時(shí),又能提高性能呢?

原子操作就橫空出世了!

定義原子操作的時(shí)候必須引入頭文件

#include <atomic>

那么如何利用原子操作提交計(jì)算的性能呢?實(shí)際上很簡(jiǎn)單的。

#include <iostream>
#include <thread>
#include <atomic>
#include <time.h>
#include <mutex>
using namespace std;
 
#define MAX 100000
#define THREAD_COUNT 20
 
//原子操作
atomic_int total(0);
 
void thread_task()
{
    for (int i = 0; i < MAX; i++)
    {
        total += 1;
        total -= 1;
    }
}
 
int main()
{
    clock_t start = clock();
    thread t[THREAD_COUNT];
    for (int i = 0; i < THREAD_COUNT; ++i)
    {
        t[i] = thread(thread_task);
    }
    for (int i = 0; i < THREAD_COUNT; ++i)
    {
        t[i].join();
    }
    
    clock_t finish = clock();
    // 輸出結(jié)果
    cout << "result:" << total << endl;
    cout << "duration:" << finish - start << "ms" << endl;
 
    return 0;
}

可以看到,我們?cè)谶@里只需要定義atomic_int total(0)就可以實(shí)現(xiàn)原子操作了,就不需要互斥鎖了。而性能的提升也是非常明顯的,這就是原子操作的魅力所在。

C++11多線程編程(四)——原子操作
在線客服 雙翌客服
客服電話
  • 0755-23712116
  • 13310869691