#ifndef _SYNC_SIMPLEQUEUE_QUEUE_HPP_
#define _SYNC_SIMPLEQUEUE_QUEUE_HPP_
#include <queue>
using namespace std;
namespace utility {
template <typename T>
class SyncSimpleQueue {
public:
void put(const T& msg) {
std::unique_lock<std::mutex> guard(m_mutex);
m_queue.push(msg);
if (m_queue.size() == 1) {
can_read.notify_one();
}
}
T take() {
std::unique_lock<std::mutex> guard(m_mutex);
if (m_queue.empty()) {
can_read.wait(guard, [this]() { return !m_queue.empty(); });
}
T msg = m_queue.front();
m_queue.pop();
return msg;
}
bool take_no_wait(T& msg) {
std::unique_lock<std::mutex> guard(m_mutex);
if (m_queue.empty()) {
return false;
}
msg = m_queue.front();
m_queue.pop();
return true;
}
private:
std::queue<T> m_queue;
std::mutex m_mutex;
std::condition_variable can_read;
};
} // namespace utility
#endif