C++无锁队列concurrentqueue的使用和性能速度测试

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

 提供了一个无锁队列。它的实现基于原子操作CAS,比大多数其它队列性能更强,而且使用更方便。本文的性能测试方法基于文章《C++计算打印函数和代码块的执行时间(支持所有类型函数)》,下面用例子讲解该队列的使用,以及跟STL队列容器queue的性能对比。

假设我们想要实现一个最简单的生产者消费者模式。生产者线程负责把数据入队列,消费者线程把数据出队列,为了保护临界资源,我们得加互斥锁。为了在队列为空的时候阻止消费者继续消费,还得加条件变量。所以用C++11的语法和容器std::queue实现的代码如下:

#include <thread>
#include <chrono>
#include <memory>
#include <future>
#include <functional>
#include <iostream>
#include <queue>

using namespace std;
#define TOTAL 1000000

std::mutex m;
std::condition_variable cv;
queue<int> gQueue;


template<class T, class... Args>
auto measure(T&& func, Args&&... args)->std::future<typename std::result_of<T(Args...)>::type>
{
    using return_type = typename std::result_of<T(Args...)>::type;
    auto task = std::make_shared<std::packaged_task<return_type()>>
        (std::bind(std::forward<T>(func), std::forward<Args>(args)...));
    std::future<return_type> res = 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<std::chrono::nanoseconds>(end - begin);
    printf("执行时间: % .3f seconds.\n", elapsed.count() * 1e-9);
    return res;
}


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

void consumer_thread()
{
    int element = 0;
    while (element != TOTAL -1)
    {
        std::unique_lock<std::mutex> lk(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;
}

运行效果如下,可以看到总执行时间为19.632秒。

 

我们把队列改为使用concurrentqueue,代码如下:

#include <thread>
#include <chrono>
#include <memory>
#include <future>
#include <functional>
#include <iostream>

#include "blockingconcurrentqueue.h"

using namespace std;
#define TOTAL 1000000

moodycamel::BlockingConcurrentQueue<int> gConcurrentQueue;


template<class T, class... Args>
auto measure(T&& func, Args&&... args)->std::future<typename std::result_of<T(Args...)>::type>
{
    using return_type = typename std::result_of<T(Args...)>::type;
    auto task = std::make_shared<std::packaged_task<return_type()>>
        (std::bind(std::forward<T>(func), std::forward<Args>(args)...));
    std::future<return_type> res = 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<std::chrono::nanoseconds>(end - begin);
    printf("执行时间: % .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;
}

运行效果如下,可以看到总执行时间为18.198秒。

 

由上面的例子可以看出,实现相同的功能,使用concurrentqueue比std::queue性能还要略强一点。而且concurrentqueue接口完善,封装程度更高,使用它后我们不需要再在代码中显式增加互斥锁和条件变量来进行同步了,使用更简单,可以使我们的代码可读性更好。

  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值