线程安全ring buffer

256 篇文章 3 订阅
149 篇文章 2 订阅
#include <iostream>
#include <string>
#include <vector>
#include <atomic>
struct SpinLock {
    SpinLock(std::atomic_flag &flag) : flag_(flag) {
        while (true == flag_.test_and_set(std::memory_order_acquire)) {
        }
    }
    ~SpinLock() {
        flag_.clear(std::memory_order_release);
    }
private:
    std::atomic_flag &flag_;
};
template <class T>
struct BufferBase {
    virtual ~BufferBase() = default;
    virtual bool try_push(const T &&) = 0;
    virtual bool try_pop(T &) = 0;
};
class StringRingBuffer : public BufferBase<std::string> {
public:
    struct Item {
        Item() : written(false) , flag{ATOMIC_FLAG_INIT} {

        }
        bool written;
        std::string data;
        std::atomic_flag flag;
    };
public:
    StringRingBuffer(size_t size = 1024) : size_(size){
        if (0 == size_ || size_ > max_capacity_)  {
            size_ = max_capacity_;
        }
        try {
            ring_ = new Item[size_];
        }
        catch (std::exception &e) {
            ring_ = nullptr;
            std::cerr << e.what() << std::endl;
            ::exit(-1);
        }
    }
    virtual ~StringRingBuffer() {
        if (nullptr != ring_) {
            delete []ring_;
            ring_ = nullptr;
        }
    }
public:
    virtual bool try_push(const std::string &&data) override {
        Item &item = ring_[written_index_];
        SpinLock lock(item.flag);
        if (true == item.written) {
            return false;
        }
        item.data = std::move(data);
        written_index_ = (written_index_ + 1) % size_;
        item.written = true;
        return true;
    }
    virtual bool try_pop(std::string &data) override {
        Item &item = ring_[read_index_ % size_];
        SpinLock lock(item.flag);
        if (false == item.written) {
            return false;
        }
        data = std::move(item.data);
        item.written = false;
        read_index_ = (read_index_ + 1) % size_;
        return true;
    }
    void set_max_capacity(size_t cap) {
        max_capacity_ = cap;
    }
private:
    size_t size_;
    size_t written_index_ = 0;
    size_t read_index_ = 0;
    Item *ring_ = nullptr;
private:
    size_t max_capacity_ = 1024 * 1024;
};
int main() {
    StringRingBuffer buffer;
    std::string data;
    std::cout << buffer.try_push("hello world") << std::endl;
    std::cout << buffer.try_pop(data) << std::endl;
    std::cout << data << std::endl;
       
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值