[reply]#ifndef CONCURRENTQUEUE_H
#define CONCURRENTQUEUE_H
#include <queue>
#include <mutex>
#include <condition_variable>
#include <utility>
template<typename T, typename Queue = std::queue<T>>
class ConcurrentQueue {
public:
template<typename Element>
void push(Element&& element) {
{
std::lock_guard<std::mutex> guard(m_mutex);
m_queue.push(std::forward<Element>(element));
}
m_condition_variable.notify_one();
}
T pop() {
std::unique_lock<std::mutex> lock(m_mutex);
m_condition_variable.wait(lock, [this] { return (m_queue.empty()==false); });
auto element = std::move(m_queue.front());
m_queue.pop();
return element;
}
private:
Queue m_queue = {};
std::mutex m_mutex = {};
std::condition_variable m_condition_variable = {};
};
#endif // CONCURRENTQUEUE_H
[/reply]
#ifndef CONCURRENTQUEUE_H
#define CONCURRENTQUEUE_H
#include <queue>
#include <mutex>
#include <condition_variable>
#include <utility>
template<typename T, typename Queue = std::queue<T>>
class ConcurrentQueue {
public:
template<typename Element>
void push(Element&& element) {
{
std::lock_guard<std::mutex> guard(m_mutex);
m_queue.push(std::forward<Element>(element));
}
m_condition_variable.notify_one();
}
T pop() {
std::unique_lock<std::mutex> lock(m_mutex);
m_condition_variable.wait(lock, [this] { return (m_queue.empty()==false); });
auto element = std::move(m_queue.front());
m_queue.pop();
return element;
}
private:
Queue m_queue = {};
std::mutex m_mutex = {};
std::condition_variable m_condition_variable = {};
};
#endif // CONCURRENTQUEUE_H