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

The enqueue function returns the number of items in the work queue #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 7 additions & 4 deletions async_queue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,23 @@ class async_queue {
///
/// \param func A function which will be enqueued on the work queue
/// \param args The argument of the funciton \p func
/// \returns The number of items in the work queue. If called from the
/// worker thread then 0 is returned
template <typename F, typename... Args>
void enqueue(const F func, Args... args) {
if(std::this_thread::get_id() == queue_thread.get_id()) {
int enqueue(const F func, Args... args) {
int count = 0;
if(is_worker()) {
func(args...);
} else {
auto no_arg_func = std::bind(func, std::forward<Args>(args)...);
{
lock_guard<mutex> lock(queue_mutex);
work_queue.push(no_arg_func);
count = work_queue.size();
}

cond.notify_one();
}
return;
return count;
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this a bit misleading ? If you enqueue from the queue you are going to get 0. Why not always return work_queue.size() ?

Copy link
Member Author

Choose a reason for hiding this comment

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

I didn't want to take the lock to get the count. It also make sense because the operations are performed immediately and they are not inserted into the queue.

}

/// \brief Check if the current thread of execution is same as the queue thread
Expand Down