# My Project: include/utils/ThreadSafeQueue.hh Source File

Loading...
Searching...
No Matches
ThreadSafeQueue.hh
Go to the documentation of this file.
// C++ Includes
#include <queue>
#include <thread>
#include <functional>
#include <mutex>
#include <condition_variable>
namespace utils {
template<typename T>
class ThreadSafeQueue {
private:
std::queue<T> _queue;
std::mutex _mutex;
std::condition_variable _cv;
public:
void push(T value) {
{
std::lock_guard<std::mutex> lock(_mutex);
_queue.push(std::move(value));
}
_cv.notify_one();
}
T wait_and_pop() {
std::unique_lock<std::mutex> lock(_mutex);
_cv.wait(lock, [&] { return !_queue.empty(); });
T val = std::move(_queue.front());
_queue.pop();
return val;
}
};
} // End utils namespace
utils::ThreadSafeQueue
Definition ThreadSafeQueue.hh:11
utils::ThreadSafeQueue::push
void push(T value)
Definition ThreadSafeQueue.hh:18
utils::ThreadSafeQueue::wait_and_pop
T wait_and_pop()
Definition ThreadSafeQueue.hh:27
utils::ThreadSafeQueue::_mutex
std::mutex _mutex
Definition ThreadSafeQueue.hh:14
utils::ThreadSafeQueue::_queue
std::queue< T > _queue
Definition ThreadSafeQueue.hh:13
utils::ThreadSafeQueue::_cv
std::condition_variable _cv
Definition ThreadSafeQueue.hh:15
utils
Definition ThreadPool.cc:11