Created
August 28, 2018 04:05
-
-
Save kovrov/a2015156b0f1f8af60e67eb450209e86 to your computer and use it in GitHub Desktop.
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 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