小编lux*_*fux的帖子

适用于大型阵列的无拷贝线程安全环形缓冲区

对于大型数组(10 ^ 7个元素)上的信号处理,我使用与环形缓冲区连接的不同线程。可悲的是,将数据复制到缓冲区或从缓冲区复制数据只需要太多时间。当前的实现基于boost::lockfree::spsc_queue

因此,我正在寻找一种解决方案,以通过在向量和向量之间使用unique_ptr来在向量和线程之间交换向量的所有权(请参见附图:在线程和队列之间交换指针)。

移动智能指针不符合我的需求,因为因此我需要在运行时不断为新的矢量元素分配内存。该开销大于复制数据的开销。

我在设计上缺少缺陷吗?

是否有线程安全或什至无锁的环形缓冲区实现允许对push和pop进行交换操作?

编辑:我修改了一个锁环缓冲区以交换unique_ptr。性能提升巨大。虽然这并不是一个优雅的解决方案。有什么建议吗?

// https://github.com/embeddedartistry/embedded-resources/blob/master/examples/cpp/circular_buffer.cpp

#include <memory>
#include <mutex>

template <typename T, int SIZE>
class RingbufferPointer {
typedef std::unique_ptr<T> TPointer;
public:
    explicit RingbufferPointer() {
        // create objects
        for (int i=0; i<SIZE; i++) {
            buf_[i] = std::make_unique<T>();
        }
    }

    bool push(TPointer &item) {
        std::lock_guard<std::mutex> lock(mutex_);
        if (full())
            return false;

        std::swap(buf_[head_], item);

        if (full_)
            tail_ = (tail_ + 1) % max_size_;

        head_ = (head_ + 1) …
Run Code Online (Sandbox Code Playgroud)

c++ vector circular-buffer lock-free move-semantics

5
推荐指数
1
解决办法
652
查看次数

标签 统计

c++ ×1

circular-buffer ×1

lock-free ×1

move-semantics ×1

vector ×1