欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

C++無鎖隊列concurrentqueue的使用和性能速度測試-創(chuàng)新互聯(lián)

GitHub - cameron314/concurrentqueue: A fast multi-producer, multi-consumer lock-free concurrent queue for C++11

公司主營業(yè)務:成都網(wǎng)站設計、成都做網(wǎng)站、移動網(wǎng)站開發(fā)等業(yè)務。幫助企業(yè)客戶真正實現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。成都創(chuàng)新互聯(lián)是一支青春激揚、勤奮敬業(yè)、活力青春激揚、勤奮敬業(yè)、活力澎湃、和諧高效的團隊。公司秉承以“開放、自由、嚴謹、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團隊有機會用頭腦與智慧不斷的給客戶帶來驚喜。成都創(chuàng)新互聯(lián)推出金水免費做網(wǎng)站回饋大家。

提供了一個無鎖隊列。它的實現(xiàn)基于原子操作CAS,比大多數(shù)其它隊列性能更強,而且使用更方便。本文的性能測試方法基于文章《C++計算打印函數(shù)和代碼塊的執(zhí)行時間(支持所有類型函數(shù))》,下面用例子講解該隊列的使用,以及跟STL隊列容器queue的性能對比。

假設我們想要實現(xiàn)一個最簡單的生產(chǎn)者消費者模式。生產(chǎn)者線程負責把數(shù)據(jù)入隊列,消費者線程把數(shù)據(jù)出隊列,為了保護臨界資源,我們得加互斥鎖。為了在隊列為空的時候阻止消費者繼續(xù)消費,還得加條件變量。所以用C++11的語法和容器std::queue實現(xiàn)的代碼如下:

#include#include#include#include#include#include#includeusing namespace std;
#define TOTAL 1000000

std::mutex m;
std::condition_variable cv;
queuegQueue;


templateauto measure(T&& func, Args&&... args)->std::future::type>{
    using return_type = typename std::result_of::type;
    auto task = std::make_shared>(std::bind(std::forward(func), std::forward(args)...));
    std::futureres = task->get_future();
    auto begin = std::chrono::high_resolution_clock::now();
    (*task)();
    auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast(end - begin);
    printf("執(zhí)行時間: % .3f seconds.\n", elapsed.count() * 1e-9);
    return res;
}


void producer_thread()
{
    for (int i = 0; i< TOTAL; i++)
    {
        std::unique_locklk(m);
        gQueue.push(i);
        cv.notify_one();
    }
}

void consumer_thread()
{
    int element = 0;
    while (element != TOTAL -1)
    {
        std::unique_locklk(m);
        cv.wait(lk, [] {return !gQueue.empty(); });
        element = gQueue.front();
        gQueue.pop();
        printf("element:%d\n", element);
    }
}

int main()
{
    measure([] {
        thread a(producer_thread);
        thread b(consumer_thread);
        a.join();
        b.join();
        });

    return 0;
}

運行效果如下,可以看到總執(zhí)行時間為19.632秒。

我們把隊列改為使用concurrentqueue,代碼如下:

#include#include#include#include#include#include#include "blockingconcurrentqueue.h"

using namespace std;
#define TOTAL 1000000

moodycamel::BlockingConcurrentQueuegConcurrentQueue;


templateauto measure(T&& func, Args&&... args)->std::future::type>{
    using return_type = typename std::result_of::type;
    auto task = std::make_shared>(std::bind(std::forward(func), std::forward(args)...));
    std::futureres = task->get_future();
    auto begin = std::chrono::high_resolution_clock::now();
    (*task)();
    auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast(end - begin);
    printf("執(zhí)行時間: % .3f seconds.\n", elapsed.count() * 1e-9);
    return res;
}


void producer_thread()
{
    for (int i = 0; i< TOTAL; i++)
    {
        gConcurrentQueue.enqueue(i);
    }
}

void consumer_thread()
{
    int element = 0;
    while (element != TOTAL -1)
    {
        gConcurrentQueue.wait_dequeue(element);
        printf("element:%d\n", element);
    }
}

int main()
{
    measure([] {
        thread a(producer_thread);
        thread b(consumer_thread);
        a.join();
        b.join();
        });

    return 0;
}

運行效果如下,可以看到總執(zhí)行時間為18.198秒。

由上面的例子可以看出,實現(xiàn)相同的功能,使用concurrentqueue比std::queue性能還要略強一點。而且concurrentqueue接口完善,封裝程度更高,使用它后我們不需要再在代碼中顯式增加互斥鎖和條件變量來進行同步了,使用更簡單,可以使我們的代碼可讀性更好。

你是否還在尋找穩(wěn)定的海外服務器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調(diào)度確保服務器高可用性,企業(yè)級服務器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧

網(wǎng)站題目:C++無鎖隊列concurrentqueue的使用和性能速度測試-創(chuàng)新互聯(lián)
瀏覽地址:http://chinadenli.net/article12/diijgc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化App設計ChatGPT小程序開發(fā)品牌網(wǎng)站制作微信小程序

廣告

聲明:本網(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)

成都app開發(fā)公司