Created
September 3, 2014 11:30
-
-
Save areinull/bd5ea20f2231a557df4a to your computer and use it in GitHub Desktop.
simple concurrent queue with Boost
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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