27 #ifndef UTIL_SIMPLE_THREAD_SAFE_QUEUE_H_ 28 #define UTIL_SIMPLE_THREAD_SAFE_QUEUE_H_ 30 #include <condition_variable> 48 template<
typename... Args>
void push(Args&&...
args) {
49 std::unique_lock<std::mutex> lock(mutex_);
50 queue_.push(std::forward<Args>(
args)...);
52 condvar_.notify_one();
56 std::lock_guard<std::mutex> lock(mutex_);
61 std::unique_lock<std::mutex> lock(mutex_);
62 condvar_.wait(lock, [&] {
return !queue_.empty(); });
63 T result = std::move(queue_.front());
65 return std::move(result);
69 std::unique_lock<std::mutex> lock(mutex_);
70 std::chrono::milliseconds wait_for_duration(wait_ms);
71 condvar_.wait_for(lock, wait_for_duration, [&] {
return !queue_.empty(); });
72 if (queue_.size() > 0) {
73 T result = std::move(queue_.front());
75 return std::move(result);
81 std::lock_guard<std::mutex> lock(mutex_);
82 if (queue_.size() > 0) {
83 T result = std::move(queue_.front());
85 return std::move(result);
91 std::lock_guard<std::mutex> lock(mutex_);
99 #endif // UTIL_SIMPLE_THREAD_SAFE_QUEUE_H_
util::StatusOr< T > pop()
util::StatusOr< T > blocking_pop(int wait_ms)
void push(Args &&...args)
std::condition_variable condvar_