5 #ifndef RMCV_PARALLEQUEUE_HPP
6 #define RMCV_PARALLEQUEUE_HPP
9 #include <condition_variable>
15 template<
typename DATATYPE,
typename SEQUENCE = std::deque<DATATYPE>>
18 std::condition_variable m_cond;
19 std::queue<DATATYPE, SEQUENCE> m_data;
20 mutable std::mutex m_mutex;
32 std::lock_guard<std::mutex> lg(m_mutex);
33 return m_data.empty();
37 std::lock_guard<std::mutex> lg(other.m_mutex);
38 m_data = other.m_data;
41 void push(
const DATATYPE &data) {
42 std::lock_guard<std::mutex> lg(m_mutex);
47 void push(DATATYPE &&data) {
48 std::lock_guard<std::mutex> lg(m_mutex);
49 m_data.push(std::move(data));
53 std::shared_ptr<DATATYPE> tryPop() {
54 std::lock_guard<std::mutex> lg(m_mutex);
55 if (m_data.empty())
return {};
56 auto res = std::make_shared<DATATYPE>(m_data.front());
61 std::shared_ptr<DATATYPE> pop() {
62 std::unique_lock<std::mutex> lg(m_mutex);
63 m_cond.wait(lg, [
this] {
return !m_data.empty(); });
64 auto res = std::make_shared<DATATYPE>(std::move(m_data.front()));
71 #endif //RMCV_PARALLEQUEUE_HPP