When I read boost atomics about an example wait-free ring buffer implementation:
I am wondering if the memory_order_acquire is necessary at
if (next_head == tail_.load(boost::memory_order_acquire))
seems memory_order_relaxed should work as well. My argument is that
value = ring_[tail];
happens-before
tail_.store(next(tail), boost::memory_order_release)
in pop() call. so we are sure data has been read before we store in push() call as
ring_[head] = value;
I pasted the whole boost example code below for easy reference. Thanks!
#include <boost/atomic.hpp>
template<typename T, size_t Size>
class ringbuffer {
public:
ringbuffer() : head_(0), tail_(0) {}
bool push(const T & value)
{
size_t head = head_.load(boost::memory_order_relaxed);
size_t next_head = next(head);
if (next_head == tail_.load(boost::memory_order_acquire))
//Could tail_.load above use boost::memory_order_relaxed?
return false;
ring_[head] = value;
head_.store(next_head, boost::memory_order_release);
return true;
}
bool pop(T & value)
{
size_t tail = tail_.load(boost::memory_order_relaxed);
if (tail == head_.load(boost::memory_order_acquire))
return false;
value = ring_[tail];
tail_.store(next(tail), boost::memory_order_release);
return true;
}
private:
size_t next(size_t current)
{
return (current + 1) % Size;
}
T ring_[Size];
boost::atomic<size_t> head_, tail_;
};