53lock_free_spsc_ring_buffer<T>::lock_free_spsc_ring_buffer(
size_t capacity):
57 ring_size_(capacity + 1)
59 static_assert(std::atomic<size_t>::is_always_lock_free);
65lock_free_spsc_ring_buffer<T>::~lock_free_spsc_ring_buffer()
75bool lock_free_spsc_ring_buffer<T>::try_push(
const value_type& x)
77 const size_t head = head_.load(std::memory_order_relaxed);
78 const size_t tail = tail_.load(std::memory_order_acquire);
79 const size_t new_head = next_index(head);
85 head_.store(new_head, std::memory_order_release);
95bool lock_free_spsc_ring_buffer<T>::try_push(value_type&& x)
97 const size_t head = head_.load(std::memory_order_relaxed);
98 const size_t tail = tail_.load(std::memory_order_acquire);
99 const size_t new_head = next_index(head);
100 if (new_head == tail)
104 ring_[head] = std::move(x);
105 head_.store(new_head, std::memory_order_release);
115template <
typename... Args>
116bool lock_free_spsc_ring_buffer<T>::try_emplace(Args&&... args)
118 const size_t head = head_.load(std::memory_order_relaxed);
119 const size_t tail = tail_.load(std::memory_order_acquire);
120 const size_t new_head = next_index(head);
121 if (new_head == tail)
125 ring_[head] = value_type(std::forward<Args...>(args...));
126 head_.store(new_head, std::memory_order_release);
136bool lock_free_spsc_ring_buffer<T>::try_pop(value_type& x)
138 const size_t head = head_.load(std::memory_order_acquire);
139 const size_t tail = tail_.load(std::memory_order_relaxed);
144 x = std::move(ring_[tail]);
145 tail_.store(next_index(tail), std::memory_order_release);
154bool lock_free_spsc_ring_buffer<T>::empty()
const
156 const size_t head = head_.load(std::memory_order_acquire);
157 const size_t tail = tail_.load(std::memory_order_acquire);
166bool lock_free_spsc_ring_buffer<T>::full()
const
168 const size_t head = head_.load(std::memory_order_acquire);
169 const size_t tail = tail_.load(std::memory_order_acquire);
170 return next_index(head) == tail;
177size_t lock_free_spsc_ring_buffer<T>::next_index(
size_t index)
const
180 return index < ring_size_ ? index : 0;
lass extensions to the standard library
Library for Assembled Shared Sources.