#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;
}