C++并发之环形队列(ring,queue)

1 概述

最近研究了C++11的并发编程的线程/互斥/锁/条件变量,利用互斥/锁/条件变量实现一个支持多线程并发的环形队列,队列大小通过模板参数传递。
环形队列是一个模板类,有两个模块参数,参数1是元素类型,参数2是队列大小,默认是10。入队操作如果队列满阻塞,出队操作如果队列为空则阻塞。
其类图为:
类图

2 实现

#ifndef RING_QUEUE_H
#define RING_QUEUE_H
#include <mutex>
#include <condition_variable>
template<typename T, std::size_t N = 10>
class ring_queue
{
public:
    typedef T           value_type;
    typedef std::size_t size_type;
    typedef std::size_t pos_type;
    typedef typename std::unique_lock<std::mutex> lock_type;
    ring_queue() { static_assert(N != 0); }
    ring_queue(ring_queue const&) = delete;
    ring_queue(ring_queue&& ) = delete;
    ring_queue& operator = (ring_queue const&) = delete;
    ring_queue& operator = (ring_queue &&) = delete;

    size_type spaces() const { return N; }
    bool empty() const
    {
        lock_type lock(mutex_);
        return read_pos_ == write_pos_;
    }

    size_type size() const
    {
        lock_type lock(mutex_);
        return N - space_size_;
    }

    void push(value_type const& value)
    {
        {
            lock_type lock(mutex_);
            while(!space_size_)
                write_cv_.wait(lock);

            queue_[write_pos_] = value;
            --space_size_;
            write_pos_ = next_pos(write_pos_);
        }
        read_cv_.notify_one();
    }

    void push(value_type && value)
    {
        {
            lock_type lock(mutex_);
            while(!space_size_)
                write_cv_.wait(lock);
            
            queue_[write_pos_] = std::move(value);
            --space_size_;
            write_pos_ = next_pos(write_pos_);
        }
        read_cv_.notify_one();
    }

    value_type pop()
    {
        value_type value;
        {
            lock_type lock(mutex_);
            while(N == space_size_)
                read_cv_.wait(lock);
            
            value = std::move(queue_[read_pos_]);
            ++space_size_;
            read_pos_ = next_pos(read_pos_);
        }
        write_cv_.notify_one();
        return value;
    }

private:
    pos_type next_pos(pos_type pos) { return (pos + 1) % N; }
private:
    value_type queue_[N];
    pos_type read_pos_ = 0;
    pos_type write_pos_ = 0;
    size_type space_size_ = N;
    std::mutex mutex_;
    std::condition_variable write_cv_;
    std::condition_variable read_cv_;
};
#endif

说明:

  • 实现利用了一个固定大小数组/一个读位置/一个写位置/互斥/写条件变量/读条件变量/空间大小变量。
  • 两个入队接口:
    • push(T const&) 左值入队
    • push(T &&) 左值入队
  • 一个出队接口
    • pop()

3 测试

基于cpptest的测试代码如下:

struct Function4RingQueue
{
    ring_queue<std::string, 2> queue;
    std::mutex mutex;
    int counter = 0;
    void consume1(size_t n)
    {
        std::cerr << "\n";
        for(size_t i = 0; i < n; ++i)
        {
            std::cerr << "I get a " << queue.pop() << std::endl;
            counter++;
        }
    }
    void consume2(size_t id)
    {
        std::string fruit = queue.pop();
        {
            std::unique_lock<std::mutex> lock(mutex);
            std::cerr << "\nI get a " << fruit << " in thread(" << id << ")" << std::endl;
            counter++;
        }
    }
    void product1(std::vector<std::string> & fruits)
    {
        for(auto const& fruit: fruits)
            queue.push(fruit + std::string(" pie"));
    }
    void product2(std::vector<std::string> & fruits)
    {
        for(auto const& fruit: fruits)
            queue.push(fruit);
    }
};
void RingQueueSuite::one_to_one()
{
    Function4RingQueue function;
    std::vector<std::string> fruits{"Apple", "Banana", "Pear", "Plum", "Pineapple"};
    std::thread threads[2];

    threads[0] = std::thread(&Function4RingQueue::product1, std::ref(function), std::ref(fruits));
    threads[1] = std::thread(&Function4RingQueue::consume1, std::ref(function), fruits.size());

    for(auto &thread : threads)
        thread.join();
    TEST_ASSERT_EQUALS(fruits.size(), function.counter)

    function.counter = 0;
    threads[0] = std::thread(&Function4RingQueue::product2, std::ref(function), std::ref(fruits));
    threads[1] = std::thread(&Function4RingQueue::consume1, std::ref(function), fruits.size());

    for(auto &thread : threads)
        thread.join();
    TEST_ASSERT_EQUALS(fruits.size(), function.counter)
}

void RingQueueSuite::one_to_multi()
{
    Function4RingQueue function;
    std::vector<std::string> fruits{"Apple", "Banana", "Pear", "Plum", "Pineapple"};
    std::thread product;
    std::vector<std::thread> consumes(fruits.size());

    for(size_t i = 0; i < consumes.size(); ++i)
        consumes[i] = std::thread(&Function4RingQueue::consume2, std::ref(function), i);
    product = std::thread(&Function4RingQueue::product1, std::ref(function), std::ref(fruits));
    
    product.join();
    for(auto &thread : consumes)
        thread.join();
    TEST_ASSERT_EQUALS(fruits.size(), function.counter)

    function.counter = 0;
    for(size_t i = 0; i < consumes.size(); ++i)
        consumes[i] = std::thread(&Function4RingQueue::consume2, std::ref(function), i);
    product = std::thread(&Function4RingQueue::product2, std::ref(function), std::ref(fruits));
    product.join();
    for(auto &thread : consumes)
        thread.join();
    TEST_ASSERT_EQUALS(fruits.size(), function.counter)
}
  • 函数one_to_one测试一个生成者对应一个消费者。
  • 函数one_to_multi测试一个生产者对应多个消费者。

4 运行

RingQueueSuite: 0/2
I get a Apple pie
I get a Banana pie
I get a Pear pie
I get a Plum pie
I get a Pineapple pie

I get a Apple
I get a Banana
I get a Pear
I get a Plum
I get a Pineapple
RingQueueSuite: 1/2
I get a Apple pie in thread(1)

I get a Banana pie in thread(0)

I get a Pear pie in thread(2)

I get a Plum pie in thread(4)

I get a Pineapple pie in thread(3)

I get a Apple in thread(0)

I get a Banana in thread(1)

I get a Plum in thread(3)

I get a Pear in thread(2)

I get a Pineapple in thread(4)
RingQueueSuite: 2/2, 100% correct in 0.007452 seconds
Total: 2 tests, 100% correct in 0.007452 seconds

分析:

  • 从结果看入队顺序和出队顺序是一致的。
  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: C语言中提供了STL(标准模板库)的环形队列类库,称为"queue"。STL提供了一个通用的、可重用的环形队列实现,可以使用该类库来处理各种类型的数据。 该环形队列类库提供了以下功能: 1. 入队和出队操作:可以使用"push"函数将元素添加到队列的尾部,并使用"pop"函数将队列首部的元素移除; 2. 访问队列首部和尾部元素:可以使用"front"函数来访问队列首部的元素,使用"back"函数来访问队列尾部的元素; 3. 判断队列是否为空:可以使用"empty"函数来判断队列是否为空; 4. 获取队列中元素的个数:可以使用"size"函数来获取队列中元素的个数; 5. 清空队列:可以使用"clear"函数来清空队列中的所有元素。 环形队列的特点是可以循环利用底层数据结构的空间,因此在一定程度上可以提高存储空间的利用率。当队列的尾部指针已到达数组的尾部,而队列仍有空间可用时,头指针可以返回数组的起始位置,从而形成循环。 使用STL的环形队列类库时,需要包含相应的头文件,如"#include <queue>"。然后可以声明一个队列对象,并通过调用相应的函数来操作队列。 总之,C语言中提供的STL环形队列类库提供了方便、高效的队列操作功能,可用于解决各种实际问题中对队列的需求。 ### 回答2: C++ STL中提供了queue类模板,它可以使用数组实现环形队列。使用环形队列可以实现在队列满时,队首元素被删除的同时,新元素可以插入到队列的队尾。 环形队列的实现主要包含两个关键问题:队列的大小和队列的头尾指针。使用一个数组来存储队列元素,同时使用两个指针front和rear来指示队列的队首和队尾位置。 当插入元素时,rear指针向后移动,并将元素插入到rear指针指向的位置。当删除元素时,front指针向后移动,并将队首元素删除。若rear指针到达数组的末尾,且队列还有空闲位置时,rear指针将被置为0,重新指向队列的起始位置。 对于环形队列的实现,需要处理一些特殊情况。比如判断队列是否为空,可以通过front和rear指针的相等来判断;判断队列是否已满,可以通过front和rear指针相差的绝对值为队列大小来判断。 通过使用STL中的queue类模板,可以简化环形队列的实现,同时提供了一些常用的操作接口,比如入队(push)、出队(pop)、访问队首元素(front)等。使用STL中的queue类模板,可以快速、简便地实现环形队列,提高代码的可读性和可维护性。由于STL中的queue类模板已经对环形队列进行了封装,因此使用者无需关心具体的实现细节,直接使用相应的类模板即可。 综上所述,C++ STL中提供了queue类模板,可以方便地实现环形队列,提供了常用的操作接口,支持快速的队列操作。使用STL中的queue类模板可以提高代码的可读性和可维护性,减少错误发生的可能性。 ### 回答3: C++的标准模板库(Standard Template Library,STL)提供了许多容器类,其中包括环形队列类库。 环形队列是一种特殊的队列,它的首尾相接,形成一个循环的结构。STL中的环形队列类库是通过std::queue和std::deque来实现的。 std::queue是一个基于队列的容器适配器,用于封装底层容器的特性。在STL中,我们可以使用std::deque作为底层容器来实现环形队列。std::deque是一种双端队列,它可以在队列的首尾进行快速插入和删除操作。 环形队列类库提供了一系列的成员函数和操作符重载,用于对队列进行访问、插入和删除操作。例如,可以使用front()函数来访问队列的第一个元素,可以使用push()函数来在队列的尾部插入一个元素,可以使用pop()函数来删除队列的第一个元素。 使用环形队列类库可以提高代码的效率和可读性。它可以很方便地实现先进先出(FIFO)的数据结构,例如队列、缓冲区等。另外,由于底层是循环结构,插入和删除操作的时间复杂度是常数级别,因此在对时间效率要求较高的场景中,环形队列是一个很好的选择。 总之,STL中的环形队列类库提供了方便、高效的操作接口,能够很好地满足先进先出数据结构的需求。在C++编程中,我们可以使用这个类库来处理队列相关的任务。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

flysnow010

你的鼓励就是我最大的创作动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值