Skip to content

Instantly share code, notes, and snippets.

@kovrov
Created August 28, 2018 04:05
Show Gist options
  • Save kovrov/a2015156b0f1f8af60e67eb450209e86 to your computer and use it in GitHub Desktop.
Save kovrov/a2015156b0f1f8af60e67eb450209e86 to your computer and use it in GitHub Desktop.
template <typename T>
class producer_consumer_queue {
public:
void put(T &&data) {
std::unique_lock lock{_mtx};
_queue.push(std::move(data));
_cv.notify_one();
}
T get() {
std::unique_lock lock{_mtx};
_cv.wait(lock, [this] { return !_queue.empty(); });
auto data = std::move(_queue.front());
_queue.pop();
return data;
}
private:
std::condition_variable _cv;
std::mutex _mtx;
std::queue<T> _queue;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment