diff --git a/.gitignore b/.gitignore index 3a44668..6d35477 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ build cmake-build-*/ *~ .idea/ -.vscode/ \ No newline at end of file +.vscode/ +.cache/ \ No newline at end of file diff --git a/README.md b/README.md index 971a9a6..1c674fa 100644 --- a/README.md +++ b/README.md @@ -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::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::value == true`. - `size_t size();` diff --git a/include/rigtorp/SPSCQueue.h b/include/rigtorp/SPSCQueue.h index 8b1c55a..3fb55fb 100644 --- a/include/rigtorp/SPSCQueue.h +++ b/include/rigtorp/SPSCQueue.h @@ -180,7 +180,8 @@ template > class SPSCQueue { static_assert(std::is_nothrow_destructible::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_) {