Skip to content

Commit

Permalink
Clarify pop() preconditions
Browse files Browse the repository at this point in the history
  • Loading branch information
rigtorp committed Sep 25, 2023
1 parent 945c7c5 commit 4066113
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ build
cmake-build-*/
*~
.idea/
.vscode/
.vscode/
.cache/
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ See `src/SPSCQueueExample.cpp` for the full example.
- `void pop();`
Dequeue first item of queue. Invalid to call if queue is
empty. Requires `std::is_nothrow_destructible<T>::value == true`.
Dequeue first item of queue. You must ensure that the queue is non-empty
before calling pop. This means that `front()` must have returned a
non-`nullptr` before each call to `pop()`. Requires
`std::is_nothrow_destructible<T>::value == true`.
- `size_t size();`
Expand Down
3 changes: 2 additions & 1 deletion include/rigtorp/SPSCQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ template <typename T, typename Allocator = std::allocator<T>> class SPSCQueue {
static_assert(std::is_nothrow_destructible<T>::value,
"T must be nothrow destructible");
auto const readIdx = readIdx_.load(std::memory_order_relaxed);
assert(writeIdx_.load(std::memory_order_acquire) != readIdx);
assert(writeIdx_.load(std::memory_order_acquire) != readIdx &&
"Can only call pop() after front() has returned a non-nullptr");
slots_[readIdx + kPadding].~T();
auto nextReadIdx = readIdx + 1;
if (nextReadIdx == capacity_) {
Expand Down

0 comments on commit 4066113

Please sign in to comment.