Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear the queue when consumer reads from it #96

Merged
merged 2 commits into from
Apr 7, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions include/ur_client_library/comm/pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ class Pipeline
}

/*!
* \brief Returns the next package in the queue. Can be used instead of registering a consumer.
* \brief Returns the most recent package in the queue. Can be used instead of registering a consumer. If the queue already contains one or more items, the queue will be flushed and the newest item will be returned. If there is no item inside the queue, the function will wait for \p timeout for a new package
*
* \param product Unique pointer to be set to the package
* \param timeout Time to wait if no package is in the queue before returning
Expand All @@ -328,7 +328,15 @@ class Pipeline
*/
bool getLatestProduct(std::unique_ptr<T>& product, std::chrono::milliseconds timeout)
{
return queue_.waitDequeTimed(product, timeout);
// If the queue has more than one package, get the latest one.
bool res = false;
while (queue_.tryDequeue(product))
{
res = true;
}

// If the queue is empty, wait for a package.
return res || queue_.waitDequeTimed(product, timeout);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally not a fan of this type of return structure, but I can accept that as subjective.

}

private:
Expand Down