extern_interface/include/dataqueue.hpp

57 lines
1.0 KiB
C++
Raw Normal View History

2024-11-28 08:31:00 +00:00
#ifndef _INCLUDED_QUEUE_
#define _INCLUDED_QUEUE_
#include <list>
#include <mutex>
#include <condition_variable>
template <typename T>
class DataQueue {
public:
2024-11-28 10:14:00 +00:00
DataQueue() = default;
2024-11-28 08:31:00 +00:00
DataQueue(DataQueue&) = delete;
void Push(T data)
{
std::lock_guard<std::mutex> lock(m_Mutex);
m_Queue.push_back(data);
m_Cond.notify_one();
}
T Pop()
{
std::unique_lock<std::mutex> lock(m_Mutex);
while (m_Queue.empty())
{
m_Cond.wait(lock);
}
T data = m_Queue.front();
m_Queue.pop_front();
return data;
}
bool Empty()
{
std::lock_guard<std::mutex> lock(m_Mutex);
return m_Queue.empty();
}
size_t Size()
{
std::lock_guard<std::mutex> lock(m_Mutex);
return m_Queue.size();
}
void Clear()
{
std::lock_guard<std::mutex> lock(m_Mutex);
m_Queue.clear();
}
protected:
std::list<T> m_Queue;
std::mutex m_Mutex;
std::condition_variable m_Cond;
};
#endif