Skip to content

Instantly share code, notes, and snippets.

@phrz
Created December 18, 2017 03:28
Show Gist options
  • Select an option

  • Save phrz/c2d0c767aaf6e98f3a53c53231a8e40c to your computer and use it in GitHub Desktop.

Select an option

Save phrz/c2d0c767aaf6e98f3a53c53231a8e40c to your computer and use it in GitHub Desktop.
template<typename T>
class ConcurrentQueue {
protected:
mutable std::mutex queue_mutex;
std::queue<T> queue;
[[nodiscard]] auto getLock() {
return std::lock_guard<std::mutex>(this->queue_mutex);
}
public:
// empty
bool empty() {
auto lock = getLock();
return queue.empty();
}
// front
T& front() {
auto lock = getLock();
return queue.front();
}
// push
void push(const T& value) {
auto lock = getLock();
return queue.push(value);
}
void push(T&& value) {
auto lock = getLock();
return queue.push(value);
}
// pop
void pop() {
auto lock = getLock();
queue.pop();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment