Open Tao-Fu opened 3 years ago
When the queue is full and used with BlockWaitStrategy, the BoundedQueue's waitEnqueue will never waked up. Beacuse when the queue is full it will going to wait_strategy_->EmptyWait()
, but Dequeue
don't have a notify, then it will wait for the condition_variable
forever.
template <typename T>
bool BoundedQueue<T>::WaitEnqueue(const T& element) {
while (!break_all_wait_) {
if (Enqueue(element)) { \\ when queue is full , then failed
return true;
}
if (wait_strategy_->EmptyWait()) { \\ wait for the condition_variable
continue;
}
// wait timeout
break;
}
return false;
}
Add notifyonce in Dequeue
as the Enqueue
does.
template <typename T>
bool BoundedQueue<T>::Dequeue(T* element) {
uint64_t new_head = 0;
uint64_t old_head = head_.load(std::memory_order_acquire);
do {
new_head = old_head + 1;
if (new_head == commit_.load(std::memory_order_acquire)) {
return false;
}
*element = pool_[GetIndex(new_head)];
} while (!head_.compare_exchange_weak(old_head, new_head,
std::memory_order_acq_rel,
std::memory_order_relaxed));
wait_strategy_->NotifyOne(); // ---------------- add notify
return true;
}
There will be a little problem add wait_strategy_->NotifyOne()
to Dequeue
. There will be an invalid notify
WaitDequeue
and the queue is empty, then we wait the condition.Enqueue
come and will add an object to the queue and then notify one WaitDequeue
WaitDequeue
will do Dequeue
and success, then it will call NotifyOne
again, but the queue is already empty.
Describe the bug When used with BlockWaitStrategy and BoundedQueue's waitEnqueue is never waked up when the queue is full. The following test code is blocked forever.
To Reproduce Steps to reproduce the behavior:
Expected behavior
Screenshots If applicable, add screenshots to help explain your problem.
Desktop (please complete the following information):
Smartphone (please complete the following information):
Additional context Add any other context about the problem here.