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

How can the queue be cleared? #150

Closed
deiflou opened this issue Feb 24, 2019 · 2 comments
Closed

How can the queue be cleared? #150

deiflou opened this issue Feb 24, 2019 · 2 comments

Comments

@deiflou
Copy link

deiflou commented Feb 24, 2019

Hello.
At some point in the code I want to clear the queue for potential reuse. I'm using:

void clear_tasks_()
{
    std::vector<task_type> tasks(10);
    while(task_queue_.try_dequeue_bulk(consumer_token_, tasks.begin(), 10));
}

Is there any other (simpler, more correct) way to clear the queue or discard the elements currently enqueued?
Thanks.

@cameron314
Copy link
Owner

There is no simpler way, I'm afraid. Adding a clear() method would require coordination between all producers, which can't be done efficiently with the current design; so, it's up to the user to do such coordination.

Your loop is efficient, and correct assuming that no more tasks are being added in parallel, and that the thread it's running on has been synchronized with all the producer threads after their last enqueue operation (e.g. via a memory barrier or mutex).

@deiflou
Copy link
Author

deiflou commented Feb 26, 2019

Ok, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants