mirror of
https://github.com/MaskRay/ccls.git
synced 2025-02-07 17:32:14 +00:00
54 lines
1.1 KiB
C++
54 lines
1.1 KiB
C++
#pragma once
|
|
|
|
#include <optional.h>
|
|
|
|
#include <algorithm>
|
|
#include <queue>
|
|
#include <mutex>
|
|
#include <condition_variable>
|
|
|
|
// TODO: cleanup includes.
|
|
|
|
|
|
// A threadsafe-queue. http://stackoverflow.com/a/16075550
|
|
template <class T>
|
|
class ThreadedQueue {
|
|
public:
|
|
// Add an element to the queue.
|
|
void Enqueue(T t) {
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
queue_.push(t);
|
|
cv_.notify_one();
|
|
}
|
|
|
|
// Get the "front"-element.
|
|
// If the queue is empty, wait untill an element is avaiable.
|
|
T Dequeue() {
|
|
std::unique_lock<std::mutex> lock(mutex_);
|
|
while (queue_.empty()) {
|
|
// release lock as long as the wait and reaquire it afterwards.
|
|
cv_.wait(lock);
|
|
}
|
|
T val = queue_.front();
|
|
queue_.pop();
|
|
return val;
|
|
}
|
|
|
|
// Get the first element from the queue without blocking. Returns a null
|
|
// value if the queue is empty.
|
|
optional<T> TryDequeue() {
|
|
std::unique_lock<std::mutex> lock(mutex_);
|
|
if (queue_.empty())
|
|
return nullopt;
|
|
|
|
T val = queue_.front();
|
|
queue_.pop();
|
|
return val;
|
|
}
|
|
|
|
private:
|
|
std::queue<T> queue_;
|
|
mutable std::mutex mutex_;
|
|
std::condition_variable cv_;
|
|
};
|