multiqueue bugfix for non-blocking pushes when queue is full

master
Francisco 4 years ago committed by Ismael Gomez
parent adcfcfe012
commit 996d8ef74d

@ -133,7 +133,14 @@ class multiqueue_handler
bool push_(T* o, bool blocking) noexcept bool push_(T* o, bool blocking) noexcept
{ {
std::unique_lock<std::mutex> lock(q_mutex); std::unique_lock<std::mutex> lock(q_mutex);
while (active_ and blocking and buffer.full()) { if (not blocking) {
// non-blocking case
if (not active_ or buffer.full()) {
return false;
}
} else {
// blocking case
while (active_ and buffer.full()) {
nof_waiting++; nof_waiting++;
cv_full.wait(lock); cv_full.wait(lock);
nof_waiting--; nof_waiting--;
@ -143,6 +150,7 @@ class multiqueue_handler
cv_exit.notify_one(); cv_exit.notify_one();
return false; return false;
} }
}
buffer.push(std::forward<T>(*o)); buffer.push(std::forward<T>(*o));
if (consumer_notify_needed) { if (consumer_notify_needed) {
// Note: The consumer thread only needs to be notified and awaken when queues transition from empty to non-empty // Note: The consumer thread only needs to be notified and awaken when queues transition from empty to non-empty

@ -240,7 +240,7 @@ int test_multiqueue_threading4()
auto qid3 = multiqueue.add_queue(); auto qid3 = multiqueue.add_queue();
auto qid4 = multiqueue.add_queue(); auto qid4 = multiqueue.add_queue();
std::mutex mutex; std::mutex mutex;
int last_number; int last_number = -1;
auto pop_blocking_func = [&multiqueue, &last_number, &mutex](bool* success) { auto pop_blocking_func = [&multiqueue, &last_number, &mutex](bool* success) {
int number = 0; int number = 0;
while (multiqueue.wait_pop(&number)) { while (multiqueue.wait_pop(&number)) {

Loading…
Cancel
Save