git-subtree-dir: libs/CommService git-subtree-split: 7ccc0fce88bbc5969df060058cf0fb57abe3bcf9
47 lines
1.0 KiB
C++
47 lines
1.0 KiB
C++
#include <optional>
|
|
#include <queue>
|
|
#include <mutex>
|
|
|
|
template<typename T>
|
|
class ThreadsafeQueue {
|
|
std::queue<T> queue_;
|
|
mutable std::mutex mutex_;
|
|
|
|
// Moved out of public interface to prevent races between this
|
|
// and pop().
|
|
bool empty() const {
|
|
return queue_.empty();
|
|
}
|
|
|
|
public:
|
|
ThreadsafeQueue() = default;
|
|
ThreadsafeQueue(const ThreadsafeQueue<T> &) = delete ;
|
|
ThreadsafeQueue& operator=(const ThreadsafeQueue<T> &) = delete ;
|
|
|
|
ThreadsafeQueue(ThreadsafeQueue<T>&& other) {
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
queue_ = std::move(other.queue_);
|
|
}
|
|
|
|
virtual ~ThreadsafeQueue() { }
|
|
|
|
unsigned long size() const {
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
return queue_.size();
|
|
}
|
|
|
|
std::optional<T> pop() {
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
if (queue_.empty()) {
|
|
return {};
|
|
}
|
|
T tmp = queue_.front();
|
|
queue_.pop();
|
|
return tmp;
|
|
}
|
|
|
|
void push(const T &item) {
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
queue_.push(item);
|
|
}
|
|
}; |