Skip to content

Commit d9b6c5b

Browse files
committed
Refine iterator equality and update buffer usage docs
Replaces defaulted equality operators for iterators with custom implementations that compare only the current position, clarifying iterator comparison semantics. Also updates documentation to note user responsibility for destroying remaining elements in the buffer.
1 parent 14b55c7 commit d9b6c5b

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

include/LockFreeSpscQueue.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ class LockFreeSpscQueue
9696
}
9797
iterator operator++(int) { iterator tmp = *this; ++(*this); return tmp; }
9898
bool operator==(const iterator& other) const = default;
99+
bool operator==(const iterator& other) const {
100+
// For two iterators to be equal, they only need to point to the same element.
101+
// The other members define the boundaries of the range, which are guaranteed
102+
// to be the same if the iterators originate from the same WriteScope object,
103+
// a precondition for valid comparison.
104+
return m_current_iter == other.m_current_iter;
105+
}
99106

100107
private:
101108
friend struct WriteScope;
@@ -247,7 +254,10 @@ class LockFreeSpscQueue
247254
, m_block2_end(other.m_block2_end)
248255
, m_in_block1(other.m_in_block1) {}
249256

250-
bool operator==(const any_iterator& other) const = default;
257+
bool operator==(const any_iterator& other) const {
258+
// Only the current position needs to be compared.
259+
return m_current_iter == other.m_current_iter;
260+
}
251261

252262
private:
253263
friend struct ReadScope;
@@ -501,6 +511,8 @@ class LockFreeSpscQueue
501511
* The size of this buffer MUST be a power of two.
502512
* @warning The user is responsible for ensuring that the lifetime of the
503513
* provided buffer exceeds the lifetime of this queue object.
514+
* The user is also reponsible for the destruction of any elements
515+
* remaining in the buffer when it is no longer in use.
504516
*/
505517
explicit LockFreeSpscQueue(std::span<T> buffer)
506518
: m_buffer(buffer), m_capacity(buffer.size()), m_capacity_mask(m_capacity - 1)

0 commit comments

Comments
 (0)