1#ifndef SISCO_CONDITIONAL_QUEUE_H_
2#define SISCO_CONDITIONAL_QUEUE_H_
5#include <condition_variable>
34 void Push(T&&
value, std::unique_lock<std::mutex>& lock) {
35 assert(lock.owns_lock());
49 template <
typename... Args>
50 void Emplace(std::unique_lock<std::mutex>& lock, Args&&... args) {
51 assert(lock.owns_lock());
55 values_.emplace_back(std::forward<Args>(args)...);
71 template <
typename Condition>
72 bool PopIf(T& result, std::unique_lock<std::mutex>& lock,
73 Condition condition) {
74 assert(lock.owns_lock());
104 void Finish([[maybe_unused]] std::unique_lock<std::mutex>& lock) {
105 assert(lock.owns_lock());
110 size_t Size([[maybe_unused]] std::unique_lock<std::mutex>& lock)
const {
111 assert(lock.owns_lock());
118 template <
typename Condition>
119 std::list<T>::iterator
GetNext(Condition&& condition) {
120 for (
typename std::list<T>::iterator i =
values_.begin();
122 if (condition(*i))
return i;
size_t Size(std::unique_lock< std::mutex > &lock) const
bool PopIf(T &result, std::unique_lock< std::mutex > &lock, Condition condition)
Pop an object from the queue for which a specified condition holds.
~ConditionalQueue()=default
void Push(T &&value, std::unique_lock< std::mutex > &lock)
Place a new object at the end of the queue.
ConditionalQueue(size_t max_size)
void Emplace(std::unique_lock< std::mutex > &lock, Args &&... args)
Same as Push(), but constructs in place.
std::condition_variable pop_condition_
void Finish(std::unique_lock< std::mutex > &lock)
Makes PopIf return false once the queue is empty.
void NotifyOneChange()
Notify the queue that the condition for values might have changed.
std::condition_variable push_condition_
std::list< T >::iterator GetNext(Condition &&condition)
NewDelAllocator< T > NewDelAllocator< T >::value