Skip to content

Instantly share code, notes, and snippets.

@areinull
Created September 3, 2014 11:30
Show Gist options
  • Save areinull/bd5ea20f2231a557df4a to your computer and use it in GitHub Desktop.
Save areinull/bd5ea20f2231a557df4a to your computer and use it in GitHub Desktop.
simple concurrent queue with Boost
template<typename Data>
class concurrent_queue
{
private:
std::queue<Data> the_queue;
mutable boost::mutex the_mutex;
boost::condition_variable the_condition_variable;
public:
void push(Data const& data)
{
boost::mutex::scoped_lock lock(the_mutex);
the_queue.push(data);
lock.unlock();
the_condition_variable.notify_one();
}
bool empty() const
{
boost::mutex::scoped_lock lock(the_mutex);
return the_queue.empty();
}
bool try_pop(Data& popped_value)
{
boost::mutex::scoped_lock lock(the_mutex);
if(the_queue.empty())
{
return false;
}
popped_value=the_queue.front();
the_queue.pop();
return true;
}
void wait_and_pop(Data& popped_value)
{
boost::mutex::scoped_lock lock(the_mutex);
while(the_queue.empty())
{
the_condition_variable.wait(lock);
}
popped_value=the_queue.front();
the_queue.pop();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment